-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse.cpp
60 lines (54 loc) · 1.28 KB
/
collapse.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
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
unordered_map<int, int> need;
unordered_map<int, int> supplied;
vector<vector<ii>> AL;
vector<int> collapsed;
queue<int> q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("collapse.in","r",stdin);
int n; cin >> n;
AL.assign(n+1, vector<ii>());
collapsed.assign(n+1, 0);
for (int i=1; i<=n; i++) {
cin >> need[i];
int t; cin >> t;
while (t--) {
int v, w; cin >> v >> w;
AL[v].push_back(ii(i,w));
supplied[i] += w;
}
}
collapsed[1] = 1;
q.push(1); n--;
while (!q.empty()) {
int curr = q.front(); q.pop();
for (auto& x:AL[curr]) {
int xi = x.first;
if (!collapsed[xi]) {
supplied[xi] -= x.second;
if (supplied[xi] < need[xi]) {
collapsed[xi] = 1;
q.push(xi);
n--;
}
}
}
}
cout << n;
}