Submission #554742

#TimeUsernameProblemLanguageResultExecution timeMemory
554742keta_tsimakuridzeCapital City (JOI20_capital_city)C++17
100 / 100
1004 ms46348 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define pii pair<int,int> #define f first #define s second #define endl "\n" const int N = 2e5 + 5, mod = 1e9 + 7; //! int t, sz[N], f[N], n, vis[N], c[N], ans, p[N], col[N]; int cnt[N]; vector<int> rem, x[N]; vector<int> V[N]; queue<int> q; void dfs0(int u,int p) { sz[u] = 1; for(int i = 0; i < V[u].size(); i++) { if(V[u][i] == p || f[V[u][i]]) continue; dfs0(V[u][i], u); sz[u] += sz[V[u][i]]; } } int find(int u, int p, int SZ) { for(int i = 0; i < V[u].size(); i++) { if(V[u][i] == p || f[V[u][i]]) continue; if(sz[V[u][i]] > SZ / 2) return find(V[u][i], u, SZ); } return u; } void dfs(int u,int C) { rem.push_back(c[u]); x[c[u]].push_back(u); for(int i = 0; i < V[u].size(); i++) { if(V[u][i] == p[u] || f[V[u][i]]) continue; p[V[u][i]] = u; dfs(V[u][i], C); } } void decompose(int U) { dfs0(U, 0); int C = find(U, 0, sz[U]); p[C] = 0; dfs(C, C); int cn = 0; q.push(C); while(q.size()) { int u = q.front(); q.pop(); if(cnt[c[u]] != x[c[u]].size()) cn = n; if(vis[u] == C) continue; if(col[c[u]] != C) { cn++; for(int j = 0; j < x[c[u]].size(); j++) { q.push(x[c[u]][j]); } } col[c[u]] = C; vis[u] = C; if(p[u]) q.push(p[u]); } while(rem.size()) x[rem.back()].clear(), rem.pop_back(); ans = min(ans, cn); f[C] = 1; for(int i = 0; i < V[C].size(); i++) { if(!f[V[C][i]]) decompose(V[C][i]); } } main() { ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0); int k; cin >> n >> k; for(int i = 2; i <= n; i++) { int u, v; cin >> u >> v; V[u].push_back(v); V[v].push_back(u); } for(int i = 1; i <= n; i++) cin >> c[i], cnt[c[i]]++; ans = k; decompose(1); cout << --ans; }

Compilation message (stderr)

capital_city.cpp: In function 'void dfs0(long long int, long long int)':
capital_city.cpp:16:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int i = 0; i < V[u].size(); i++) {
      |                 ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'long long int find(long long int, long long int, long long int)':
capital_city.cpp:23:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  for(int i = 0; i < V[u].size(); i++) {
      |                 ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'void dfs(long long int, long long int)':
capital_city.cpp:32:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |  for(int i = 0; i < V[u].size(); i++) {
      |                 ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'void decompose(long long int)':
capital_city.cpp:49:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   if(cnt[c[u]] != x[c[u]].size()) cn = n;
      |      ~~~~~~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:53:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |    for(int j = 0; j < x[c[u]].size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~
capital_city.cpp:63:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |  for(int i = 0; i < V[C].size(); i++) {
      |                 ~~^~~~~~~~~~~~~
capital_city.cpp: At global scope:
capital_city.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   67 | main() {
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...