Submission #445494

# Submission time Handle Problem Language Result Execution time Memory
445494 2021-07-18T10:17:09 Z ivan_tudor Mergers (JOI19_mergers) C++14
0 / 100
80 ms 36496 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<int> gr[N];
int par[20][N];
int in[N], out[N];
int sum[N];
int frv[N];
void dfs_prec(int nod, int dad,int &cur){
  par[0][nod] = dad;
  in[nod] = ++cur;
  for(auto x:gr[nod]){
    if(x != dad){
      dfs_prec(x, nod, cur);
    }
  }
  out[nod] = cur;
}
void prec_lca(int n){
  for(int log = 1; log < 20; log ++){
    for(int nod = 1; nod <=n; nod++)
      par[log][nod] = par[log -1][par[log - 1][nod]];
  }
}
bool is_dad(int nod, int son){
  if(in[nod]<= in[son] && out[nod] <= out[son])
    return true;
  return false;
}
int get_lca(int a, int b){
  if(is_dad(a, b))
    return a;
  if(is_dad(b, a))
    return b;
  for(int p2 = 19; p2 >= 0; p2--){
    int oldn = par[p2][a];
    if(oldn != 0 && is_dad(oldn, b) == false)
      a = oldn;
  }
  return par[0][a];
}
void dfs_sum(int nod, int dad){
  int s = sum[nod];
  for(auto x:gr[nod]){
    if(x!= dad){
      dfs_sum(x, nod);
      s += sum[x];
    }
  }
  sum[nod] = s;
}
int last;
int dfs(int nod, int dad, int tip){
  for(auto x:gr[nod]){
    if(x != dad){
      if(sum[x] == 0){
        frv[tip]++;
        last++;
        frv[last]++;
        dfs(x, nod, last);
      }
      else{
        dfs(x, nod, tip);
      }
    }
  }
}
vector<int> col[N];
int main()
{
  //freopen(".in","r",stdin);
  ios::sync_with_stdio(false);
  cin.tie(0),cout.tie(0);
  int nrt;
  int n, k;
  cin>>n>>k;
  for(int i = 1; i <n; i++){
    int a, b;
    cin>>a>>b;
    gr[a].push_back(b);
    gr[b].push_back(a);
  }
  for(int i = 1; i<=n; i++){
    int c;
    cin>>c;
    col[c].push_back(i);
  }
  int cur = 0;
  dfs_prec(1, 0, cur);
  prec_lca(n);
  for(int i = 1; i<=k; i++){
    int lca = col[i][0];
    for(auto x:col[i]){
      lca = get_lca(lca, x);
      sum[x]++;
    }
    sum[lca] -= col[i].size();
  }
  dfs_sum(1, 0);
  last = 1;
  dfs(1, 0, last);
  int cnt = 0;
  for(int i =1 ; i<=last; i++){
    if(frv[i] == 1)
      cnt++;
  }
  cout<<(cnt + 1)/2;
  return 0;
}

Compilation message

mergers.cpp: In function 'int dfs(int, int, int)':
mergers.cpp:67:1: warning: no return statement in function returning non-void [-Wreturn-type]
   67 | }
      | ^
mergers.cpp: In function 'int main()':
mergers.cpp:74:7: warning: unused variable 'nrt' [-Wunused-variable]
   74 |   int nrt;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10188 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10188 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10188 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 80 ms 36496 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 10188 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -