-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10926.cpp
89 lines (83 loc) · 1.67 KB
/
P10926.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
typedef pair<ULL,ULL> BitVector;
int countBits(ULL x) {
int ret = 0;
while(x > 0) {
if((x & 1) == 1)
++ret;
x >>= 1;
}
return ret;
}
int cnt(BitVector v) {
return countBits(v.first) + countBits(v.second);
}
BitVector create(int pos) {
BitVector v(0,0);
if((pos & 1) == 1) {
v.second = ((ULL)1) << (pos>>1);
}
else {
v.first = ((ULL)1) << (pos>>1);
}
return v;
}
std::ostream& operator<<(std::ostream& os, const BitVector& v) {
FORI(100) {
if((i & 1) == 1) {
if(0 != (v.second & (((ULL)1) << (i>>1))))
os << " " << i;
}
else {
if(0 != (v.first & (((ULL)1) << (i>>1))))
os << " " << i;
}
}
return os;
}
int main() {
int N, T, Q, outdegree[101];
BitVector dependencies[101];
vector<int> in[101];
while(true) {
cin >> N;
if(N == 0)
return 0;
FORI(N) {
in[i].clear();
dependencies[i] = create(i);
}
stack<int> q;
FORI(N) {
cin >> T;
outdegree[i] = T;
if(outdegree[i] == 0)
q.push(i);
FORJ(T) {
cin >> Q; --Q;
in[Q].push_back(i);
} // j
} // i
while(!q.empty()) {
int x = q.top();
q.pop();
FORIT(vector<int>, in[x]) {
int y = *it;
BitVector v = dependencies[x];
dependencies[y].first |= v.first;
dependencies[y].second |= v.second;
--outdegree[y];
if(outdegree[y] == 0)
q.push(y);
}
}
int ret = 0;
FORI(N) {
/*
if(cnt(dependencies[i]) > 0)
cerr << "Dependencies of " << i+1 << ":" << dependencies[i] << " = " << cnt(dependencies[i]) << endl;//*/
if(cnt(dependencies[i]) > cnt(dependencies[ret]))
ret = i;
}
cout << ret+1 << endl;
} // while(true)
} // int main