-
Notifications
You must be signed in to change notification settings - Fork 0
/
StronglyConnectedComponents-1.test.cpp
54 lines (52 loc) · 1.29 KB
/
StronglyConnectedComponents-1.test.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
#define PROBLEM "https://yukicoder.me/problems/no/1023"
#include <vector>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <stack>
using namespace std;
#include "../../lib/41-union-find-tree/UnionFindTree.cpp"
#include "../../lib/40-graph/StronglyConnectedComponents.cpp"
int main(){
int N,M; cin >> N >> M;
vector<pair<int,int>> edge;
vector<pair<int,int>> bedge;
for(int i = 0; i < M; ++i) {
int a,b; cin >> a >> b;
a--,b--;
int c; cin >> c;
if(c==1){
bedge.push_back({a,b});
}
else{
edge.push_back({a,b});
}
}
UnionFindTree uf(N);
for(auto& e:bedge){
int a = e.first,b = e.second;
if(uf.connected(a,b)){
cout << "Yes" << endl;
return 0;
}
uf.merge(a,b);
}
StronglyConnectedComponents scc(N);
for(auto& e:edge){
int a = e.first,b = e.second;
if(uf.connected(a,b)){
cout << "Yes" << endl;
return 0;
}
scc.make_edge(uf[a],uf[b]);
}
scc.solve();
vector<int> cnt(N,0);
for(int i = 0; i < N; ++i) cnt[scc[i]]++;
for(int i = 0; i < N; ++i) if(cnt[i] > 1){
cout << "Yes" << endl;
return 0;
}
cout << "No" << endl;
return 0;
}