Submission #716272

# Submission time Handle Problem Language Result Execution time Memory
716272 2023-03-29T12:52:17 Z Ahmed57 Mergers (JOI19_mergers) C++14
70 / 100
81 ms 25064 KB
#include <bits/stdc++.h>

using namespace std;
vector<int> adj[200001];
int dep[200001] , par[200001];
//DSU
int pr[200001];
int gs[200001];

int findleader(int x){
    if(pr[x]==x){
        return x;
    }
    return pr[x] = findleader(pr[x]);
}
void mergegroup(int x,int y){
    int led1 = findleader(x);
    int led2 = findleader(y);
    if(led1==led2)return;
    if(gs[led1]>gs[led2]){
        pr[led2]=led1;
        gs[led1]+=gs[led2];
    }else{
        pr[led1]=led2;
        gs[led2]+=gs[led1];
    }
}
void merge(int a,int b){
    while(a!=b){
        if(dep[a] < dep[b])swap(a, b);
        pr[a]=findleader(par[a]);
        a = pr[a];
    }
}
void dfs(int i ,int prr){
    par[i] = prr;
    dep[i] = dep[prr]+1;
    for(auto j:adj[i]){
        if(j!=prr)dfs(j,i);
    }
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,k;
    cin>>n>>k;
    for(int i = 0;i<=n;i++){
        gs[i] = 1, pr[i] = i;
    }
    vector<int> col[k+1];
    for(int i = 0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(1,0);
    for(int i = 1;i<=n;i++){
        int x;cin>>x;
        col[x].push_back(i);
    }
    for(int i = 1;i<=k;i++){
        for(int j = 1;j<col[i].size();j++){
            merge(col[i][0],col[i][j]);
        }
    }
    int deg[n+1] = {0};
    for(int i = 1;i<=n;i++){
        for(auto j:adj[i]){
            if(findleader(i)!=findleader(j)){
                deg[findleader(i)]++;deg[findleader(j)]++;
            }
        }
    }
    int cnt = 0;
    for(int i = 1;i<=n;i++){
        if(deg[i]==2){
            cnt++;
        }
    }
    cout<<(cnt+1)/2<<"\n";
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:62:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |         for(int j = 1;j<col[i].size();j++){
      |                       ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5020 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 4 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5020 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 4 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 5024 KB Output is correct
26 Correct 5 ms 5332 KB Output is correct
27 Correct 4 ms 5204 KB Output is correct
28 Correct 4 ms 5420 KB Output is correct
29 Correct 5 ms 5288 KB Output is correct
30 Correct 4 ms 5204 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 4 ms 5420 KB Output is correct
33 Correct 3 ms 4948 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 4 ms 5332 KB Output is correct
36 Correct 4 ms 5164 KB Output is correct
37 Correct 5 ms 5292 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 5 ms 5292 KB Output is correct
40 Correct 4 ms 5204 KB Output is correct
41 Correct 4 ms 5204 KB Output is correct
42 Correct 4 ms 5288 KB Output is correct
43 Correct 5 ms 5380 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 5 ms 5332 KB Output is correct
46 Correct 4 ms 5292 KB Output is correct
47 Correct 3 ms 5024 KB Output is correct
48 Correct 5 ms 5148 KB Output is correct
49 Correct 4 ms 5332 KB Output is correct
50 Correct 5 ms 5420 KB Output is correct
51 Correct 5 ms 5164 KB Output is correct
52 Correct 4 ms 5204 KB Output is correct
53 Correct 4 ms 5292 KB Output is correct
54 Correct 4 ms 5164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5020 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 4 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 5024 KB Output is correct
26 Correct 48 ms 12448 KB Output is correct
27 Correct 53 ms 12152 KB Output is correct
28 Correct 5 ms 5204 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 63 ms 12084 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 60 ms 15948 KB Output is correct
34 Correct 56 ms 12220 KB Output is correct
35 Correct 5 ms 5204 KB Output is correct
36 Correct 59 ms 12124 KB Output is correct
37 Correct 4 ms 5204 KB Output is correct
38 Correct 4 ms 5204 KB Output is correct
39 Correct 43 ms 12432 KB Output is correct
40 Correct 4 ms 5332 KB Output is correct
41 Correct 52 ms 12064 KB Output is correct
42 Correct 64 ms 13076 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 59 ms 16164 KB Output is correct
45 Correct 59 ms 13900 KB Output is correct
46 Correct 4 ms 5160 KB Output is correct
47 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 11076 KB Output is correct
2 Correct 52 ms 15988 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5164 KB Output is correct
5 Correct 3 ms 5024 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 67 ms 14696 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 60 ms 12348 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 73 ms 12208 KB Output is correct
13 Correct 71 ms 14724 KB Output is correct
14 Correct 66 ms 17424 KB Output is correct
15 Correct 39 ms 12356 KB Output is correct
16 Correct 4 ms 5288 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 62 ms 17104 KB Output is correct
19 Correct 71 ms 19320 KB Output is correct
20 Correct 4 ms 5204 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 61 ms 14796 KB Output is correct
23 Correct 4 ms 5172 KB Output is correct
24 Correct 81 ms 12820 KB Output is correct
25 Correct 67 ms 18508 KB Output is correct
26 Correct 4 ms 5332 KB Output is correct
27 Correct 5 ms 5420 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 4 ms 5164 KB Output is correct
30 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5020 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 4 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 5024 KB Output is correct
26 Correct 5 ms 5332 KB Output is correct
27 Correct 4 ms 5204 KB Output is correct
28 Correct 4 ms 5420 KB Output is correct
29 Correct 5 ms 5288 KB Output is correct
30 Correct 4 ms 5204 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 4 ms 5420 KB Output is correct
33 Correct 3 ms 4948 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 4 ms 5332 KB Output is correct
36 Correct 4 ms 5164 KB Output is correct
37 Correct 5 ms 5292 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 5 ms 5292 KB Output is correct
40 Correct 4 ms 5204 KB Output is correct
41 Correct 4 ms 5204 KB Output is correct
42 Correct 4 ms 5288 KB Output is correct
43 Correct 5 ms 5380 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 5 ms 5332 KB Output is correct
46 Correct 4 ms 5292 KB Output is correct
47 Correct 3 ms 5024 KB Output is correct
48 Correct 5 ms 5148 KB Output is correct
49 Correct 4 ms 5332 KB Output is correct
50 Correct 5 ms 5420 KB Output is correct
51 Correct 5 ms 5164 KB Output is correct
52 Correct 4 ms 5204 KB Output is correct
53 Correct 4 ms 5292 KB Output is correct
54 Correct 4 ms 5164 KB Output is correct
55 Correct 3 ms 5024 KB Output is correct
56 Correct 48 ms 12448 KB Output is correct
57 Correct 53 ms 12152 KB Output is correct
58 Correct 5 ms 5204 KB Output is correct
59 Correct 3 ms 4948 KB Output is correct
60 Correct 3 ms 4948 KB Output is correct
61 Correct 63 ms 12084 KB Output is correct
62 Correct 4 ms 5204 KB Output is correct
63 Correct 60 ms 15948 KB Output is correct
64 Correct 56 ms 12220 KB Output is correct
65 Correct 5 ms 5204 KB Output is correct
66 Correct 59 ms 12124 KB Output is correct
67 Correct 4 ms 5204 KB Output is correct
68 Correct 4 ms 5204 KB Output is correct
69 Correct 43 ms 12432 KB Output is correct
70 Correct 4 ms 5332 KB Output is correct
71 Correct 52 ms 12064 KB Output is correct
72 Correct 64 ms 13076 KB Output is correct
73 Correct 3 ms 4948 KB Output is correct
74 Correct 59 ms 16164 KB Output is correct
75 Correct 59 ms 13900 KB Output is correct
76 Correct 4 ms 5160 KB Output is correct
77 Correct 4 ms 5204 KB Output is correct
78 Correct 40 ms 11076 KB Output is correct
79 Correct 52 ms 15988 KB Output is correct
80 Correct 4 ms 5204 KB Output is correct
81 Correct 4 ms 5164 KB Output is correct
82 Correct 3 ms 5024 KB Output is correct
83 Correct 3 ms 4948 KB Output is correct
84 Correct 3 ms 5204 KB Output is correct
85 Correct 67 ms 14696 KB Output is correct
86 Correct 4 ms 5204 KB Output is correct
87 Correct 60 ms 12348 KB Output is correct
88 Correct 3 ms 4948 KB Output is correct
89 Correct 73 ms 12208 KB Output is correct
90 Correct 71 ms 14724 KB Output is correct
91 Correct 66 ms 17424 KB Output is correct
92 Correct 39 ms 12356 KB Output is correct
93 Correct 4 ms 5288 KB Output is correct
94 Correct 3 ms 4948 KB Output is correct
95 Correct 62 ms 17104 KB Output is correct
96 Correct 71 ms 19320 KB Output is correct
97 Correct 4 ms 5204 KB Output is correct
98 Correct 3 ms 4948 KB Output is correct
99 Correct 61 ms 14796 KB Output is correct
100 Correct 4 ms 5172 KB Output is correct
101 Correct 81 ms 12820 KB Output is correct
102 Correct 67 ms 18508 KB Output is correct
103 Correct 4 ms 5332 KB Output is correct
104 Correct 5 ms 5420 KB Output is correct
105 Correct 4 ms 5204 KB Output is correct
106 Correct 4 ms 5164 KB Output is correct
107 Correct 4 ms 5204 KB Output is correct
108 Runtime error 18 ms 25064 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -