-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconcom.cpp
86 lines (78 loc) · 2.2 KB
/
concom.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <typename T1, typename T2>
ostream &operator <<(ostream &os, pair<T1, T2> p){os << p.first << " " << p.second; return os;}
template <typename T>
ostream &operator <<(ostream &os, vector<T> &v){for(T i : v)os << i << ", "; return os;}
template <typename T>
ostream &operator <<(ostream &os, set<T> s){for(T i : s) os << i << ", "; return os;}
template <typename T1, typename T2>
ostream &operator <<(ostream &os, map<T1, T2> m){for(pair<T1, T2> i : m) os << i << endl; return os;}
set<int> been;
bool dfs(int n, int c[101][101], int c1, int c2){
int curr[101][101];
copy(c[0], c[100] + 101, curr[0]);
been.insert(c1);
for(int i = 1; i < 101; i++){
if(c[c1][i] > 50){
if(i == c2){
return true;
}
if(been.find(i) != been.end()){
continue;
}
for(int j = 1; j < 101; j++){
curr[i][j] += curr[c1][j];
}
if(dfs(n, curr, i, c2)){
return true;
}
}
}
return false;
}
bool prelimbfs(int n, set<int> pown[101], int c1, int c2){
set<int> cbn;
cbn.insert(c1);
queue<int> bfs;
bfs.push(c1);
while(!bfs.empty()){
int idx = bfs.front();
bfs.pop();
if(idx == c2) return true;
for(int x : pown[idx]){
if(cbn.find(x) != cbn.end()) continue;
cbn.insert(x);
bfs.push(x);
}
}
return false;
}
int main(){
freopen("concom.in", "r", stdin);
freopen("concom.out", "w", stdout);
int n; cin >> n;
int c[101][101];
fill(c[0], c[100] + 101, 0);
set<int> pown[101];
for(int i = 0; i < n; i++){
int a, b, x; cin >> a >> b >> x;
c[a][b] = x;
if(c[a][b] > 50){
pown[a].insert(b);
}
}
for(int i = 1; i < 101; i++){
for(int j = 1; j < 101; j++){
been.clear();
if(j == i) continue;
if(prelimbfs(n, pown, i, j) || dfs(n, c, i, j)){
cout << i << " " << j << endl;
}
}
}
return 0;
}