File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` cpp
2+ #include < bits/stdc++.h>
3+ using namespace std ;
4+
5+ int N, M;
6+ vector<vector<int >> G (200001), R(200001);
7+ bitset<200001> vis;
8+ int last, cnt = 0;
9+
10+ void dfs1(int n) {
11+ for (int i : G[ n] ) if (!vis[ i] ) {
12+ vis[ i] = 1;
13+ dfs1(i);
14+ }
15+ last = n;
16+ }
17+
18+ void dfs2(int n) {
19+ cnt++;
20+ for (int i : R[ n] ) if (!vis[ i] ) {
21+ vis[ i] = 1;
22+ dfs2(i);
23+ }
24+ }
25+
26+ int main() {
27+ cin.tie(0)->sync_with_stdio(0);
28+
29+ cin >> N >> M;
30+ for (int a, b; M--;) {
31+ cin >> a >> b;
32+ G[a].push_back(b);
33+ R[b].push_back(a);
34+ }
35+
36+ for (int i = 1; i <= N; i++) if (!vis[i]) {
37+ vis[i] = 1;
38+ dfs1(i);
39+ }
40+
41+ vis.reset();
42+ vis[last] = 1;
43+ dfs2(last);
44+ cout << (cnt == N ? "Yes" : "No");
45+
46+ }
47+ ```
You can’t perform that action at this time.
0 commit comments