|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <cstring> |
| 4 | +#include <vector> |
| 5 | +#include <cstdlib> |
| 6 | +#include <cmath> |
| 7 | +#include <queue> |
| 8 | +#include <iomanip> |
| 9 | +#include <algorithm> |
| 10 | +#ifdef WINDOWS |
| 11 | +#include <direct.h> |
| 12 | +#define GetCurrentDir _getcwd |
| 13 | +#else |
| 14 | +#include <unistd.h> |
| 15 | +#define GetCurrentDir getcwd |
| 16 | +#endif |
| 17 | + |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +#ifndef ONLINE_JUDGE |
| 21 | +std::string GetCurrentWorkingDir( void ) { |
| 22 | + char buff[10000]; |
| 23 | + GetCurrentDir( buff, 10000 ); |
| 24 | + std::string current_working_dir(buff); |
| 25 | + return current_working_dir; |
| 26 | +} |
| 27 | +#endif |
| 28 | + |
| 29 | +#define REP(i,n) for(int i = 0; i < (n); i++) |
| 30 | +#define REPS(i,s,n) for(int i = (s); i < (n); i++) |
| 31 | + |
| 32 | +const int MAXN = 1000 + 50; |
| 33 | +int vis[MAXN]; |
| 34 | +int c[MAXN][MAXN]; |
| 35 | + |
| 36 | +bool dfs(int course, int numCourses) { |
| 37 | + vis[course] = -1; |
| 38 | + for (int i = 0; i < numCourses; i++) { |
| 39 | + if (!c[course][i]) continue; |
| 40 | + if (vis[i] < 0) return false; |
| 41 | + if (!vis[i] && !dfs(i, numCourses)) return false; |
| 42 | + } |
| 43 | + vis[course] = 1; |
| 44 | + return true; |
| 45 | +} |
| 46 | + |
| 47 | +bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { |
| 48 | + memset(vis, 0, sizeof(vis)); |
| 49 | + memset(c, 0, sizeof(c)); |
| 50 | + for (auto dependencies : prerequisites) { |
| 51 | + c[dependencies.first][dependencies.second] = 1; |
| 52 | + } |
| 53 | + REP(i, numCourses) { |
| 54 | + if (!vis[i] && !dfs(i, numCourses)) return false; |
| 55 | + } |
| 56 | + return true; |
| 57 | +} |
| 58 | + |
| 59 | +int main(int argc, char const *argv[]) { |
| 60 | + #ifndef ONLINE_JUDGE |
| 61 | + // string cwd = GetCurrentWorkingDir() + "/test.in"; |
| 62 | + // freopen(cwd.c_str(), "r", stdin); |
| 63 | + #endif |
| 64 | + vector<pair<int, int>> s{ |
| 65 | + make_pair(1, 0), |
| 66 | + make_pair(0, 1), |
| 67 | + make_pair(2, 3), |
| 68 | + make_pair(3, 4) |
| 69 | + }; |
| 70 | + cout << canFinish(3, s) << endl; |
| 71 | +} |
0 commit comments