Submission #445498

# Submission time Handle Problem Language Result Execution time Memory
445498 2021-07-18T10:26:03 Z ivan_tudor Mergers (JOI19_mergers) C++14
100 / 100
1029 ms 137448 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 500005;
vector<int> gr[N];
int par[20][N];
int in[N], out[N];
int sum[N];
int frv[N];
int tp[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[son] <= out[nod])
    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;
void dfs(int nod, int dad, int tip){
  tp[nod] = 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 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;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23884 KB Output is correct
2 Correct 13 ms 23992 KB Output is correct
3 Correct 13 ms 24012 KB Output is correct
4 Correct 13 ms 23980 KB Output is correct
5 Correct 13 ms 23944 KB Output is correct
6 Correct 16 ms 24012 KB Output is correct
7 Correct 14 ms 23960 KB Output is correct
8 Correct 13 ms 23936 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 13 ms 23960 KB Output is correct
12 Correct 13 ms 23928 KB Output is correct
13 Correct 13 ms 24000 KB Output is correct
14 Correct 13 ms 23900 KB Output is correct
15 Correct 14 ms 23936 KB Output is correct
16 Correct 13 ms 23936 KB Output is correct
17 Correct 13 ms 23912 KB Output is correct
18 Correct 13 ms 23884 KB Output is correct
19 Correct 13 ms 23928 KB Output is correct
20 Correct 13 ms 23932 KB Output is correct
21 Correct 13 ms 23964 KB Output is correct
22 Correct 13 ms 24012 KB Output is correct
23 Correct 13 ms 23884 KB Output is correct
24 Correct 13 ms 23884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23884 KB Output is correct
2 Correct 13 ms 23992 KB Output is correct
3 Correct 13 ms 24012 KB Output is correct
4 Correct 13 ms 23980 KB Output is correct
5 Correct 13 ms 23944 KB Output is correct
6 Correct 16 ms 24012 KB Output is correct
7 Correct 14 ms 23960 KB Output is correct
8 Correct 13 ms 23936 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 13 ms 23960 KB Output is correct
12 Correct 13 ms 23928 KB Output is correct
13 Correct 13 ms 24000 KB Output is correct
14 Correct 13 ms 23900 KB Output is correct
15 Correct 14 ms 23936 KB Output is correct
16 Correct 13 ms 23936 KB Output is correct
17 Correct 13 ms 23912 KB Output is correct
18 Correct 13 ms 23884 KB Output is correct
19 Correct 13 ms 23928 KB Output is correct
20 Correct 13 ms 23932 KB Output is correct
21 Correct 13 ms 23964 KB Output is correct
22 Correct 13 ms 24012 KB Output is correct
23 Correct 13 ms 23884 KB Output is correct
24 Correct 13 ms 23884 KB Output is correct
25 Correct 13 ms 23988 KB Output is correct
26 Correct 15 ms 24456 KB Output is correct
27 Correct 16 ms 24368 KB Output is correct
28 Correct 17 ms 24628 KB Output is correct
29 Correct 15 ms 24532 KB Output is correct
30 Correct 16 ms 24432 KB Output is correct
31 Correct 15 ms 24012 KB Output is correct
32 Correct 16 ms 24636 KB Output is correct
33 Correct 13 ms 23936 KB Output is correct
34 Correct 15 ms 24424 KB Output is correct
35 Correct 15 ms 24464 KB Output is correct
36 Correct 15 ms 24328 KB Output is correct
37 Correct 16 ms 24452 KB Output is correct
38 Correct 16 ms 24012 KB Output is correct
39 Correct 16 ms 24436 KB Output is correct
40 Correct 15 ms 24336 KB Output is correct
41 Correct 15 ms 24396 KB Output is correct
42 Correct 15 ms 24396 KB Output is correct
43 Correct 15 ms 24576 KB Output is correct
44 Correct 15 ms 24128 KB Output is correct
45 Correct 15 ms 24460 KB Output is correct
46 Correct 16 ms 24324 KB Output is correct
47 Correct 14 ms 23896 KB Output is correct
48 Correct 16 ms 24332 KB Output is correct
49 Correct 15 ms 24528 KB Output is correct
50 Correct 15 ms 24492 KB Output is correct
51 Correct 15 ms 24452 KB Output is correct
52 Correct 15 ms 24420 KB Output is correct
53 Correct 16 ms 24408 KB Output is correct
54 Correct 15 ms 24384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23884 KB Output is correct
2 Correct 13 ms 23992 KB Output is correct
3 Correct 13 ms 24012 KB Output is correct
4 Correct 13 ms 23980 KB Output is correct
5 Correct 13 ms 23944 KB Output is correct
6 Correct 16 ms 24012 KB Output is correct
7 Correct 14 ms 23960 KB Output is correct
8 Correct 13 ms 23936 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 13 ms 23960 KB Output is correct
12 Correct 13 ms 23928 KB Output is correct
13 Correct 13 ms 24000 KB Output is correct
14 Correct 13 ms 23900 KB Output is correct
15 Correct 14 ms 23936 KB Output is correct
16 Correct 13 ms 23936 KB Output is correct
17 Correct 13 ms 23912 KB Output is correct
18 Correct 13 ms 23884 KB Output is correct
19 Correct 13 ms 23928 KB Output is correct
20 Correct 13 ms 23932 KB Output is correct
21 Correct 13 ms 23964 KB Output is correct
22 Correct 13 ms 24012 KB Output is correct
23 Correct 13 ms 23884 KB Output is correct
24 Correct 13 ms 23884 KB Output is correct
25 Correct 14 ms 23936 KB Output is correct
26 Correct 67 ms 38840 KB Output is correct
27 Correct 99 ms 38504 KB Output is correct
28 Correct 15 ms 24332 KB Output is correct
29 Correct 13 ms 23992 KB Output is correct
30 Correct 14 ms 23884 KB Output is correct
31 Correct 89 ms 38408 KB Output is correct
32 Correct 17 ms 24376 KB Output is correct
33 Correct 110 ms 45700 KB Output is correct
34 Correct 96 ms 38596 KB Output is correct
35 Correct 15 ms 24396 KB Output is correct
36 Correct 101 ms 39240 KB Output is correct
37 Correct 16 ms 24400 KB Output is correct
38 Correct 16 ms 24396 KB Output is correct
39 Correct 69 ms 38812 KB Output is correct
40 Correct 18 ms 24672 KB Output is correct
41 Correct 118 ms 38380 KB Output is correct
42 Correct 119 ms 41040 KB Output is correct
43 Correct 13 ms 23928 KB Output is correct
44 Correct 110 ms 45880 KB Output is correct
45 Correct 105 ms 42268 KB Output is correct
46 Correct 15 ms 24388 KB Output is correct
47 Correct 15 ms 24416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 37452 KB Output is correct
2 Correct 83 ms 40828 KB Output is correct
3 Correct 17 ms 24396 KB Output is correct
4 Correct 15 ms 24396 KB Output is correct
5 Correct 14 ms 23888 KB Output is correct
6 Correct 13 ms 24012 KB Output is correct
7 Correct 15 ms 24420 KB Output is correct
8 Correct 156 ms 39864 KB Output is correct
9 Correct 15 ms 24340 KB Output is correct
10 Correct 90 ms 38644 KB Output is correct
11 Correct 13 ms 24012 KB Output is correct
12 Correct 97 ms 38616 KB Output is correct
13 Correct 128 ms 39872 KB Output is correct
14 Correct 114 ms 41844 KB Output is correct
15 Correct 66 ms 38716 KB Output is correct
16 Correct 16 ms 24436 KB Output is correct
17 Correct 14 ms 24016 KB Output is correct
18 Correct 98 ms 41680 KB Output is correct
19 Correct 136 ms 45712 KB Output is correct
20 Correct 15 ms 24396 KB Output is correct
21 Correct 13 ms 23952 KB Output is correct
22 Correct 84 ms 40148 KB Output is correct
23 Correct 15 ms 24396 KB Output is correct
24 Correct 103 ms 39004 KB Output is correct
25 Correct 133 ms 44408 KB Output is correct
26 Correct 17 ms 24460 KB Output is correct
27 Correct 15 ms 24524 KB Output is correct
28 Correct 16 ms 24368 KB Output is correct
29 Correct 15 ms 24456 KB Output is correct
30 Correct 15 ms 24452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23884 KB Output is correct
2 Correct 13 ms 23992 KB Output is correct
3 Correct 13 ms 24012 KB Output is correct
4 Correct 13 ms 23980 KB Output is correct
5 Correct 13 ms 23944 KB Output is correct
6 Correct 16 ms 24012 KB Output is correct
7 Correct 14 ms 23960 KB Output is correct
8 Correct 13 ms 23936 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 13 ms 23960 KB Output is correct
12 Correct 13 ms 23928 KB Output is correct
13 Correct 13 ms 24000 KB Output is correct
14 Correct 13 ms 23900 KB Output is correct
15 Correct 14 ms 23936 KB Output is correct
16 Correct 13 ms 23936 KB Output is correct
17 Correct 13 ms 23912 KB Output is correct
18 Correct 13 ms 23884 KB Output is correct
19 Correct 13 ms 23928 KB Output is correct
20 Correct 13 ms 23932 KB Output is correct
21 Correct 13 ms 23964 KB Output is correct
22 Correct 13 ms 24012 KB Output is correct
23 Correct 13 ms 23884 KB Output is correct
24 Correct 13 ms 23884 KB Output is correct
25 Correct 13 ms 23988 KB Output is correct
26 Correct 15 ms 24456 KB Output is correct
27 Correct 16 ms 24368 KB Output is correct
28 Correct 17 ms 24628 KB Output is correct
29 Correct 15 ms 24532 KB Output is correct
30 Correct 16 ms 24432 KB Output is correct
31 Correct 15 ms 24012 KB Output is correct
32 Correct 16 ms 24636 KB Output is correct
33 Correct 13 ms 23936 KB Output is correct
34 Correct 15 ms 24424 KB Output is correct
35 Correct 15 ms 24464 KB Output is correct
36 Correct 15 ms 24328 KB Output is correct
37 Correct 16 ms 24452 KB Output is correct
38 Correct 16 ms 24012 KB Output is correct
39 Correct 16 ms 24436 KB Output is correct
40 Correct 15 ms 24336 KB Output is correct
41 Correct 15 ms 24396 KB Output is correct
42 Correct 15 ms 24396 KB Output is correct
43 Correct 15 ms 24576 KB Output is correct
44 Correct 15 ms 24128 KB Output is correct
45 Correct 15 ms 24460 KB Output is correct
46 Correct 16 ms 24324 KB Output is correct
47 Correct 14 ms 23896 KB Output is correct
48 Correct 16 ms 24332 KB Output is correct
49 Correct 15 ms 24528 KB Output is correct
50 Correct 15 ms 24492 KB Output is correct
51 Correct 15 ms 24452 KB Output is correct
52 Correct 15 ms 24420 KB Output is correct
53 Correct 16 ms 24408 KB Output is correct
54 Correct 15 ms 24384 KB Output is correct
55 Correct 14 ms 23936 KB Output is correct
56 Correct 67 ms 38840 KB Output is correct
57 Correct 99 ms 38504 KB Output is correct
58 Correct 15 ms 24332 KB Output is correct
59 Correct 13 ms 23992 KB Output is correct
60 Correct 14 ms 23884 KB Output is correct
61 Correct 89 ms 38408 KB Output is correct
62 Correct 17 ms 24376 KB Output is correct
63 Correct 110 ms 45700 KB Output is correct
64 Correct 96 ms 38596 KB Output is correct
65 Correct 15 ms 24396 KB Output is correct
66 Correct 101 ms 39240 KB Output is correct
67 Correct 16 ms 24400 KB Output is correct
68 Correct 16 ms 24396 KB Output is correct
69 Correct 69 ms 38812 KB Output is correct
70 Correct 18 ms 24672 KB Output is correct
71 Correct 118 ms 38380 KB Output is correct
72 Correct 119 ms 41040 KB Output is correct
73 Correct 13 ms 23928 KB Output is correct
74 Correct 110 ms 45880 KB Output is correct
75 Correct 105 ms 42268 KB Output is correct
76 Correct 15 ms 24388 KB Output is correct
77 Correct 15 ms 24416 KB Output is correct
78 Correct 76 ms 37452 KB Output is correct
79 Correct 83 ms 40828 KB Output is correct
80 Correct 17 ms 24396 KB Output is correct
81 Correct 15 ms 24396 KB Output is correct
82 Correct 14 ms 23888 KB Output is correct
83 Correct 13 ms 24012 KB Output is correct
84 Correct 15 ms 24420 KB Output is correct
85 Correct 156 ms 39864 KB Output is correct
86 Correct 15 ms 24340 KB Output is correct
87 Correct 90 ms 38644 KB Output is correct
88 Correct 13 ms 24012 KB Output is correct
89 Correct 97 ms 38616 KB Output is correct
90 Correct 128 ms 39872 KB Output is correct
91 Correct 114 ms 41844 KB Output is correct
92 Correct 66 ms 38716 KB Output is correct
93 Correct 16 ms 24436 KB Output is correct
94 Correct 14 ms 24016 KB Output is correct
95 Correct 98 ms 41680 KB Output is correct
96 Correct 136 ms 45712 KB Output is correct
97 Correct 15 ms 24396 KB Output is correct
98 Correct 13 ms 23952 KB Output is correct
99 Correct 84 ms 40148 KB Output is correct
100 Correct 15 ms 24396 KB Output is correct
101 Correct 103 ms 39004 KB Output is correct
102 Correct 133 ms 44408 KB Output is correct
103 Correct 17 ms 24460 KB Output is correct
104 Correct 15 ms 24524 KB Output is correct
105 Correct 16 ms 24368 KB Output is correct
106 Correct 15 ms 24456 KB Output is correct
107 Correct 15 ms 24452 KB Output is correct
108 Correct 649 ms 104876 KB Output is correct
109 Correct 802 ms 114656 KB Output is correct
110 Correct 843 ms 125864 KB Output is correct
111 Correct 1029 ms 137448 KB Output is correct
112 Correct 920 ms 126916 KB Output is correct
113 Correct 627 ms 113988 KB Output is correct
114 Correct 675 ms 96636 KB Output is correct
115 Correct 684 ms 96624 KB Output is correct
116 Correct 946 ms 101376 KB Output is correct
117 Correct 889 ms 114460 KB Output is correct
118 Correct 694 ms 97384 KB Output is correct
119 Correct 889 ms 114564 KB Output is correct
120 Correct 933 ms 128536 KB Output is correct
121 Correct 941 ms 114500 KB Output is correct
122 Correct 949 ms 106684 KB Output is correct
123 Correct 618 ms 115964 KB Output is correct
124 Correct 990 ms 111944 KB Output is correct