forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
74 lines (59 loc) · 1.42 KB
/
game.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
#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int,int>> edges;
int inf = 100;
vector<int> getlen(int end) {
vector<int> dist(n,inf);
dist[end] = 0;
bool improved = true;
while(improved) {
improved = false;
for(auto i : edges) {
int cost = 0;
for(int j = 0; j < n; j++) {
if(i.second & (1 << j)) {
cost = max(cost,dist[j]);
}
}
cost++;
if(dist[i.first] > cost) {
improved = true;
dist[i.first] = cost;
}
}
}
return dist;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n;
for(int i = 0; i < n; i++) {
int t;
cin >> t;
for(int j = 0; j < t; j++) {
string s;
cin >> s;
int mask = 0;
for(auto i : s) {
mask |= 1 << (i-'a');
}
if(mask & (1 << i)) {
continue;
}
edges.push_back({i,mask});
}
}
vector<vector<int>> ans(n,vector<int>(n));
for(int i = 0; i < n; i++) {
ans[i] = getlen(i);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(ans[j][i] == inf) ans[j][i] = -1;
cout << ans[j][i] << " ";
}
cout << endl;
}
}