-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscc.cpp
137 lines (126 loc) · 2.89 KB
/
scc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <bits/stdc++.h>
using namespace std;
#define inf 1e18
// Structure for Graph
class Node{
public:
vector<int> edges;
int comp;
bool visited;
Node(){
visited = false;
comp = -1;
}
void add_edge(int v){
edges.push_back(v);
}
};
// Structure for SCC
class SCC{
public:
vector<Node> nodes;
vector<Node> rev_nodes;
vector<int> order;
int n;
int comp_count;
SCC(int n){
this->n = n;
nodes.resize(n);
rev_nodes.resize(n);
comp_count = 0;
}
void add_edge(int u, int v){
nodes[u].add_edge(v);
rev_nodes[v].add_edge(u);
}
void dfs1(int u){
nodes[u].visited = true;
for(int i = 0; i < nodes[u].edges.size(); i++){
int v = nodes[u].edges[i];
if(!nodes[v].visited){
dfs1(v);
}
}
order.push_back(u);
}
void dfs2(int u){
nodes[u].visited = true;
nodes[u].comp = comp_count;
for(int i = 0; i < rev_nodes[u].edges.size(); i++){
int v = rev_nodes[u].edges[i];
if(!nodes[v].visited){
dfs2(v);
}
}
}
void kosaraju(){
for(int i = 0; i < n; i++){
if(!nodes[i].visited){
dfs1(i);
}
}
reverse(order.begin(), order.end());
for(int i = 0; i < n; i++){
nodes[i].visited = false;
}
for(int i = 0; i < n; i++){
int u = order[i];
if(!nodes[u].visited){
dfs2(u);
comp_count++;
}
}
}
void print_scc(){
for(int i = 0; i < n; i++){
cout << i << " " << nodes[i].comp << endl;
}
}
};
void solve(){
int n, m;
cin >> n >> m;
SCC scc(n);
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
u--, v--;
scc.add_edge(u, v);
}
scc.kosaraju();
if(scc.comp_count == 1){
cout << "YES\n";
}
else{
cout << "NO\n";
// find a node with comp = 0 and a node with comp = 1
// print the nodes
int u = -1, v = -1;
for(int i = 0; i < n; i++){
if(scc.nodes[i].comp == 0){
u = i;
}
if(scc.nodes[i].comp == 1){
v = i;
}
if(u != -1 && v != -1){
break;
}
}
// check if node u is reachable from node v
// if yes, print v and u
// else print u and v
bool flag = false;
for(int i = 0; i < n; i++){
scc.nodes[i].visited = false;
}
scc.dfs1(u);
if(scc.nodes[v].visited){
cout << v + 1 << " " << u + 1 << endl;
}
else{
cout << u + 1 << " " << v + 1 << endl;
}
}
// scc.print_scc();
}