USACO Controlling Companies

Code

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
/*
ID: wcr19961
PROG: concom
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>
using namespace std;

typedef long long LL;
const int maxn = 110;
int d[maxn][maxn], c[maxn];
bool vis[maxn];

void dfs(int u) {
vis[u] = true;
for (int i = 1; i < maxn; ++i) {
c[i] += d[u][i];
if (c[i] > 50 && !vis[i]) {
dfs(i);
}
}
}

int main() {
freopen("concom.in", "r", stdin);
freopen("concom.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int u, v, x;
scanf("%d%d%d", &u, &v, &x);
d[u][v] = x;
}
for (int i = 1; i < maxn; ++i) {
memset(c, 0, sizeof c);
memset(vis, 0, sizeof vis);
dfs(i);
for (int j = 1; j < maxn; ++j) {
if (i != j && c[j] > 50) {
printf("%d %d\n", i, j);
}
}
}
return 0;
}