Submission #210365

#TimeUsernameProblemLanguageResultExecution timeMemory
210365arman_ferdousMergers (JOI19_mergers)C++14
0 / 100
90 ms33248 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 5e5+10; int n, k, c[N], dsu[N], deg[N]; vector<int> g[N], node[N]; vector<pair<int,int>> E; int lev[N], par[N]; int dfs(int u, int f, int d) { par[u] = f; lev[u] = d; for(int v : g[u]) if(v != f) dfs(v, u, d + 1); } int find(int u) { if(dsu[u] == u) return u; return dsu[u] = find(dsu[u]); } 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); E.push_back({u, v}); } for(int i = 1; i <= n; i++) { scanf("%d", &c[i]); node[c[i]].push_back(i); } iota(dsu, dsu + 1 + n, 0); dfs(1, 0, 0); for(int i = 1; i <= k; i++) if((int)node[i].size() > 1) { for(int j = 1; j < (int)node[i].size(); j++) { int u = find(node[i][j - 1]), v = find(node[i][j]); while(u != v) { if(lev[u] > lev[v]) swap(u, v); dsu[v] = find(par[v]); v = dsu[v]; } } } for(auto e : E) { if(find(e.first) != find(e.second)) deg[e.first]++, deg[e.second]++; } int cnt = 0; for(int i = 1; i <= n; ++i) if(deg[i] == 1) cnt++; printf("%d\n", (cnt + 1) >> 1); return 0; }

Compilation message (stderr)

mergers.cpp: In function 'int dfs(int, int, int)':
mergers.cpp:17:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
mergers.cpp: In function 'int main()':
mergers.cpp:25: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:27: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:33: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...