Submission #210176

#TimeUsernameProblemLanguageResultExecution timeMemory
210176arman_ferdousMergers (JOI19_mergers)C++17
0 / 100
75 ms30956 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 5e5+10; int n, k, c[N]; vector<int> g[N], node[N]; int cur, vis[N], comp[N]; set<int> st; int p[N]; void dfs(int u, int f) { p[u] = f; for(int v : g[u]) if(v != f) dfs(v, u); } void up(int u) { if(vis[u] != 0) return; vis[u] = cur; st.insert(c[u]); if(p[u] + 1) up(p[u]); } void process() { while(!st.empty()) { int x = *st.begin(); st.erase(st.begin()); for(int u : node[x]) up(u); node[x].clear(); } } int deg[N]; int main() { scanf("%d %d", &n, &k); for(int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1, -1); for(int i = 1; i <= n; i++) { scanf("%d", &c[i]); node[c[i]].push_back(i); } for(int i = 1; i <= k; i++) if(!node[i].empty()) { cur++; st.insert(i); process(); } for(int i = 2; i <= n; i++) if(vis[p[i]] != vis[i]) { deg[vis[p[i]]]++; deg[i]++; } int leaf = 0; for(int i = 1; i <= n; i++) if(deg[i] == 1) leaf++; if(leaf == 0) puts("0"); else printf("%d\n", (leaf - 1) / 2 + 1); return 0; }

Compilation message (stderr)

mergers.cpp: In function 'int main()':
mergers.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:42:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; scanf("%d %d", &u, &v);
             ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...