# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
563778 | 2022-05-18T06:17:40 Z | dantoh000 | Mergers (JOI19_mergers) | C++14 | 149 ms | 50336 KB |
#include <bits/stdc++.h> using namespace std; int n,k; vector<int> G[500005]; int c[500005]; int ct[500005]; int p[500005]; int sz[500005]; int sz2[500005]; set<int> s[500005]; void merg(int u, int v){ if (s[u].size() < s[v].size()) swap(s[u], s[v]); for (auto x : s[v]){ if (s[u].find(x) == s[u].end()){ sz2[u] += ct[x]; s[u].insert(x); } } } int ans = 0; void dfs(int u){ sz[u] = 1; for (auto v : G[u]){ if (v == p[u]) continue; p[v] = u; dfs(v); sz[u] += sz[v]; merg(u,v); } if (sz[u] == sz2[u]) ans++; //printf("%d %d\n",sz[u], sz2[u]); } int main(){ scanf("%d%d",&n,&k); 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",&c[i]); ct[c[i]]++; } for (int i = 1; i <= n; i++){ s[i].insert(c[i]); sz2[i] = ct[c[i]]; } for (int i = 1; i <= n; i++){ if (G[i].size() == 1){ dfs(i); break; } } if (ans == 0) printf("0"); else printf("%d",max(ans-1,1)); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 17 ms | 35540 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 17 ms | 35540 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 17 ms | 35540 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 78 ms | 45336 KB | Output is correct |
2 | Incorrect | 149 ms | 50336 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 17 ms | 35540 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |