# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
471513 |
2021-09-09T15:51:52 Z |
shrimb |
Mergers (JOI19_mergers) |
C++17 |
|
129 ms |
29028 KB |
#include"bits/stdc++.h"
// #define int long long
#define endl '\n'
#define USACO(x) ifstream cin ((string)#x + ".in"); ofstream cout((string)#x + ".out")
using namespace std;
const int maxN = 500001;
int n, k;
vector<int> adj[maxN];
int s[maxN];
int tot[maxN];
int subtree[maxN];
map<int,int>* s2b[maxN];
int cnt[maxN];
bool bad[maxN];
void inc (int a, int b, int v) {
(*s2b[a])[b] += v;
if ((*s2b[a])[b] == tot[b]) cnt[a]++;
}
int ans = INT_MAX;
void dfs (int cur, int par) {
subtree[cur] = 1;
if (adj[cur].size() == 1 and cur != 1) {
s2b[cur] = new map<int,int>();
inc(cur, s[cur], 1);
bad[cur] = (cnt[cur] == (int)((*s2b[cur]).size()));
return;
}
int best = -1;
for (int i : adj[cur]) {
if (i != par) {
dfs(i, cur);
subtree[cur] += subtree[i];
if (best == -1 or subtree[i] > subtree[best]) best = i;
}
}
s2b[cur] = s2b[best];
cnt[cur] = cnt[best];
for (int i : adj[cur]) {
if (i != par and i != best) {
for (auto j : (*s2b[i])) {
inc(cur, j.first, j.second);
}
(*s2b[i]).clear();
}
}
inc(cur, s[cur], 1);
bad[cur] = (cnt[cur] == (int)((*s2b[cur]).size()));
}
int cost = 0;
int calc_ans (int cur, int par) {
int ret = 0;
for (int i : adj[cur]) {
if (i != par) {
if (bad[cur] == 0) {
int x = calc_ans(i, cur);
cost += min(ret, x);
ret = abs(ret - x);
}
else {
ret += calc_ans(i, cur);
}
}
}
if (ret == 0 and cur != 1) ret += bad[cur];
return ret;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1 ; i < n ; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1 ; i <= n ; i++) {
cin >> s[i];
tot[s[i]]++;
}
dfs(1, 0);
cout << cost + calc_ans(1, 0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
12100 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Runtime error |
20 ms |
24204 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
12100 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Runtime error |
20 ms |
24204 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
12100 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Runtime error |
20 ms |
24204 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
28172 KB |
Output is correct |
2 |
Incorrect |
129 ms |
29028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
12100 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Runtime error |
20 ms |
24204 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |