Skip to content

Commit a9fdf5d

Browse files
committed
207
1 parent 819ae01 commit a9fdf5d

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

207/main.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
- [169 Majority Element](./169)
9191
- [189 Rotate Array](./189)
9292
- [206 Reverse Linked List](./206)
93+
- [207 Course Schedule](./207)
9394
- [209 Minimum Size Subarray Sum](./209)
9495
- [217 Contains Duplicate](./217)
9596
- [219 Contains Duplicate II](./219)

0 commit comments

Comments
 (0)