-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfonseca.cpp
103 lines (87 loc) · 2.13 KB
/
fonseca.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
100
101
102
103
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
int susc[110000];
int vac[110000];
int proc[110000];
int inc[110000];
vector<int> leaf;
vector<int> adj[110000];
vector<int> contamina[110000];
int n, ans = 0;
void contaminar(int curr, int t) {
for (int vizinho : adj[curr]) if (!proc[vizinho]) {
contamina[vizinho].push_back(t+1);
if (--inc[vizinho] == 1) leaf.push_back(vizinho);
}
ans++;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &susc[i]);
scanf("%d", &vac[i]);
}
for (int i = 0; i < n-1; i++) {
int u, v;
scanf("%d %d", &u, &v); u--; v--;
adj[u].push_back(v);
adj[v].push_back(u);
inc[u]++;
inc[v]++;
}
for (int i = 0; i < n; i++) if (adj[i].size() == 1) leaf.push_back(i);
while (!leaf.empty()) {
int curr = leaf[leaf.size()-1];
leaf.pop_back();
proc[curr]=1;
bool ok = false;
if (vac[curr] == 0) {
contaminar(curr, 0);
ok = true;
}
if (!ok && contamina[curr].size() >= susc[curr]) {
nth_element(contamina[curr].begin(), contamina[curr].begin()+(susc[curr]-1), contamina[curr].end());
if (contamina[curr][susc[curr]-1] <= vac[curr]) {
contaminar(curr, contamina[curr][susc[curr]-1]);
ans--;
ok = true;
}
}
if (!ok && contamina[curr].size() >= susc[curr]-1 && inc[curr]) {
if (susc[curr] != 1) {
nth_element(contamina[curr].begin(), contamina[curr].begin()+(susc[curr]-2), contamina[curr].end());
}
if (susc[curr] == 1 || contamina[curr][susc[curr]-2] <= vac[curr]) {
for (int vizinho : adj[curr]) if (!proc[vizinho]) {
vac[vizinho] = min(vac[vizinho], vac[curr]-1);
if (--inc[vizinho] == 1) leaf.push_back(vizinho);
}
ok = true;
}
}
if (!ok) {
contaminar(curr, 0);
}
}
printf("%d\n", ans);
}