-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva10672.cpp
99 lines (73 loc) · 1.73 KB
/
uva10672.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
90
91
92
93
94
95
96
97
98
99
#include <limits.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <bitset>
//#include <unordered_map>
using namespace std;
typedef pair<int, int> ii;
#define traverse(c, it) \
for (map<ii, int>::iterator it = c.begin(); it != c.end(); it++)
int N;
vector<int> marbles;
vector<int> parent;
vector<int> outDeg;
int countMoves() {
queue<int> q;
int count = 0;
for(int i = 0; i < N; i++) {
if(outDeg[i] == 0) {
q.push(i);
}
}
while(!q.empty()) {
int node = q.front();
q.pop();
if(parent[node] != -1) {
int mar = marbles[node];
marbles[parent[node]] += (mar - 1);
count += abs(mar - 1);
outDeg[parent[node]]--;
if(outDeg[parent[node]] == 0) {
q.push(parent[node]);
}
}
}
return count;
}
int main() {
while(true) {
cin >> N;
if(N == 0) {
break;
}
marbles.clear();
marbles.resize(N);
parent.clear();
parent.resize(N, -1);
outDeg.clear();
outDeg.resize(N, 0);
int node, count, neighbourCount, next;
for(int i = 0; i < N; i++) {
cin >> node >> count >> neighbourCount;
node--;
marbles[node] = count;
for(int j = 0; j < neighbourCount; j++) {
cin >> next;
next--;
parent[next] = node;
}
outDeg[node] = neighbourCount;
}
cout << countMoves() << endl;
}
return 0;
}