# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
127590 | 2019-07-09T15:52:20 Z | wilwxk | Mergers (JOI19_mergers) | C++14 | 133 ms | 45628 KB |
#include <bits/stdc++.h> using namespace std; const int MAXN=5e5+5; vector<int> g[MAXN]; map<int, int> mp[MAXN]; int cor[MAXN], contc[MAXN]; int n, x, raiz, respf; bool dfs(int cur, int p) { mp[cur][cor[cur]]++; bool ok=1; for(auto viz : g[cur]) { if(viz==p) continue; if(!dfs(viz, cur)) ok=0; if(mp[viz].size()>mp[cur].size()) swap(mp[cur], mp[viz]); for(auto mit : mp[viz]) { mp[cur][mit.first]+=mit.second; if(mp[cur][mit.first]==contc[mit.first]) mp[cur].erase(mit.first); } } if(mp[cur][cor[cur]]==contc[cor[cur]]) mp[cur].erase(cor[cur]); // for(auto mit : mp[cur]) printf("%d: %d %d\n", cur, mit.first, mit.second); if(!mp[cur].size()&&ok) respf++, ok=0; return ok; } int main() { scanf("%d %d", &n, &x); for(int i=1; i<n; i++) { int a, b; scanf("%d %d", &a, &b); g[a].push_back(b); g[b].push_back(a); } for(int i=1; i<=n; i++) { scanf("%d", &cor[i]); contc[cor[i]]++; } raiz=1; for(int i=1; i<=n; i++) if(g[i].size()==1) raiz=i; dfs(raiz, raiz); printf("%d\n", (respf+1)/2); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 34 ms | 35576 KB | Output is correct |
2 | Incorrect | 34 ms | 35744 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 34 ms | 35576 KB | Output is correct |
2 | Incorrect | 34 ms | 35744 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 34 ms | 35576 KB | Output is correct |
2 | Incorrect | 34 ms | 35744 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 133 ms | 45628 KB | Output is correct |
2 | Incorrect | 120 ms | 41628 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 34 ms | 35576 KB | Output is correct |
2 | Incorrect | 34 ms | 35744 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |