Submission #716274

# Submission time Handle Problem Language Result Execution time Memory
716274 2023-03-29T12:53:55 Z Ahmed57 Mergers (JOI19_mergers) C++14
100 / 100
632 ms 87040 KB
#include <bits/stdc++.h>
 
using namespace std;
vector<int> adj[500001];
int dep[500001] , par[500001];
//DSU
int pr[500001];
int gs[500001];
 
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 6 ms 11988 KB Output is correct
2 Correct 6 ms 12056 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 12100 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 12060 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 12024 KB Output is correct
14 Correct 7 ms 12096 KB Output is correct
15 Correct 7 ms 12104 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 6 ms 12060 KB Output is correct
18 Correct 6 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11976 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 12004 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 6 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12056 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 12100 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 12060 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 12024 KB Output is correct
14 Correct 7 ms 12096 KB Output is correct
15 Correct 7 ms 12104 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 6 ms 12060 KB Output is correct
18 Correct 6 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11976 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 12004 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 6 ms 11988 KB Output is correct
25 Correct 6 ms 11988 KB Output is correct
26 Correct 8 ms 12244 KB Output is correct
27 Correct 7 ms 12244 KB Output is correct
28 Correct 8 ms 12372 KB Output is correct
29 Correct 8 ms 12372 KB Output is correct
30 Correct 8 ms 12244 KB Output is correct
31 Correct 6 ms 11988 KB Output is correct
32 Correct 8 ms 12372 KB Output is correct
33 Correct 7 ms 11988 KB Output is correct
34 Correct 7 ms 12244 KB Output is correct
35 Correct 7 ms 12372 KB Output is correct
36 Correct 7 ms 12244 KB Output is correct
37 Correct 8 ms 12244 KB Output is correct
38 Correct 7 ms 11988 KB Output is correct
39 Correct 8 ms 12244 KB Output is correct
40 Correct 7 ms 12244 KB Output is correct
41 Correct 7 ms 12288 KB Output is correct
42 Correct 8 ms 12328 KB Output is correct
43 Correct 9 ms 12372 KB Output is correct
44 Correct 6 ms 11988 KB Output is correct
45 Correct 7 ms 12328 KB Output is correct
46 Correct 8 ms 12244 KB Output is correct
47 Correct 6 ms 12004 KB Output is correct
48 Correct 7 ms 12244 KB Output is correct
49 Correct 7 ms 12372 KB Output is correct
50 Correct 8 ms 12372 KB Output is correct
51 Correct 8 ms 12244 KB Output is correct
52 Correct 7 ms 12264 KB Output is correct
53 Correct 7 ms 12244 KB Output is correct
54 Correct 8 ms 12244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12056 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 12100 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 12060 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 12024 KB Output is correct
14 Correct 7 ms 12096 KB Output is correct
15 Correct 7 ms 12104 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 6 ms 12060 KB Output is correct
18 Correct 6 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11976 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 12004 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 6 ms 11988 KB Output is correct
25 Correct 6 ms 12096 KB Output is correct
26 Correct 48 ms 18104 KB Output is correct
27 Correct 64 ms 17752 KB Output is correct
28 Correct 7 ms 12244 KB Output is correct
29 Correct 6 ms 11988 KB Output is correct
30 Correct 7 ms 11988 KB Output is correct
31 Correct 60 ms 17632 KB Output is correct
32 Correct 7 ms 12288 KB Output is correct
33 Correct 58 ms 21620 KB Output is correct
34 Correct 58 ms 17812 KB Output is correct
35 Correct 7 ms 12244 KB Output is correct
36 Correct 63 ms 17640 KB Output is correct
37 Correct 8 ms 12280 KB Output is correct
38 Correct 8 ms 12212 KB Output is correct
39 Correct 42 ms 18036 KB Output is correct
40 Correct 8 ms 12372 KB Output is correct
41 Correct 52 ms 17692 KB Output is correct
42 Correct 62 ms 18752 KB Output is correct
43 Correct 7 ms 11988 KB Output is correct
44 Correct 86 ms 21976 KB Output is correct
45 Correct 99 ms 19600 KB Output is correct
46 Correct 9 ms 12244 KB Output is correct
47 Correct 10 ms 12244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 18024 KB Output is correct
2 Correct 82 ms 22912 KB Output is correct
3 Correct 12 ms 12244 KB Output is correct
4 Correct 10 ms 12196 KB Output is correct
5 Correct 8 ms 12000 KB Output is correct
6 Correct 8 ms 11988 KB Output is correct
7 Correct 10 ms 12244 KB Output is correct
8 Correct 101 ms 20020 KB Output is correct
9 Correct 9 ms 12244 KB Output is correct
10 Correct 98 ms 17860 KB Output is correct
11 Correct 9 ms 12052 KB Output is correct
12 Correct 113 ms 17788 KB Output is correct
13 Correct 111 ms 19896 KB Output is correct
14 Correct 132 ms 22668 KB Output is correct
15 Correct 51 ms 17992 KB Output is correct
16 Correct 8 ms 12244 KB Output is correct
17 Correct 6 ms 11988 KB Output is correct
18 Correct 55 ms 22464 KB Output is correct
19 Correct 67 ms 24592 KB Output is correct
20 Correct 7 ms 12244 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 55 ms 20232 KB Output is correct
23 Correct 8 ms 12244 KB Output is correct
24 Correct 64 ms 18252 KB Output is correct
25 Correct 68 ms 23812 KB Output is correct
26 Correct 7 ms 12372 KB Output is correct
27 Correct 9 ms 12356 KB Output is correct
28 Correct 8 ms 12244 KB Output is correct
29 Correct 7 ms 12192 KB Output is correct
30 Correct 7 ms 12244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12056 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 12100 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 12060 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 12024 KB Output is correct
14 Correct 7 ms 12096 KB Output is correct
15 Correct 7 ms 12104 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 6 ms 12060 KB Output is correct
18 Correct 6 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11976 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 12004 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 6 ms 11988 KB Output is correct
25 Correct 6 ms 11988 KB Output is correct
26 Correct 8 ms 12244 KB Output is correct
27 Correct 7 ms 12244 KB Output is correct
28 Correct 8 ms 12372 KB Output is correct
29 Correct 8 ms 12372 KB Output is correct
30 Correct 8 ms 12244 KB Output is correct
31 Correct 6 ms 11988 KB Output is correct
32 Correct 8 ms 12372 KB Output is correct
33 Correct 7 ms 11988 KB Output is correct
34 Correct 7 ms 12244 KB Output is correct
35 Correct 7 ms 12372 KB Output is correct
36 Correct 7 ms 12244 KB Output is correct
37 Correct 8 ms 12244 KB Output is correct
38 Correct 7 ms 11988 KB Output is correct
39 Correct 8 ms 12244 KB Output is correct
40 Correct 7 ms 12244 KB Output is correct
41 Correct 7 ms 12288 KB Output is correct
42 Correct 8 ms 12328 KB Output is correct
43 Correct 9 ms 12372 KB Output is correct
44 Correct 6 ms 11988 KB Output is correct
45 Correct 7 ms 12328 KB Output is correct
46 Correct 8 ms 12244 KB Output is correct
47 Correct 6 ms 12004 KB Output is correct
48 Correct 7 ms 12244 KB Output is correct
49 Correct 7 ms 12372 KB Output is correct
50 Correct 8 ms 12372 KB Output is correct
51 Correct 8 ms 12244 KB Output is correct
52 Correct 7 ms 12264 KB Output is correct
53 Correct 7 ms 12244 KB Output is correct
54 Correct 8 ms 12244 KB Output is correct
55 Correct 6 ms 12096 KB Output is correct
56 Correct 48 ms 18104 KB Output is correct
57 Correct 64 ms 17752 KB Output is correct
58 Correct 7 ms 12244 KB Output is correct
59 Correct 6 ms 11988 KB Output is correct
60 Correct 7 ms 11988 KB Output is correct
61 Correct 60 ms 17632 KB Output is correct
62 Correct 7 ms 12288 KB Output is correct
63 Correct 58 ms 21620 KB Output is correct
64 Correct 58 ms 17812 KB Output is correct
65 Correct 7 ms 12244 KB Output is correct
66 Correct 63 ms 17640 KB Output is correct
67 Correct 8 ms 12280 KB Output is correct
68 Correct 8 ms 12212 KB Output is correct
69 Correct 42 ms 18036 KB Output is correct
70 Correct 8 ms 12372 KB Output is correct
71 Correct 52 ms 17692 KB Output is correct
72 Correct 62 ms 18752 KB Output is correct
73 Correct 7 ms 11988 KB Output is correct
74 Correct 86 ms 21976 KB Output is correct
75 Correct 99 ms 19600 KB Output is correct
76 Correct 9 ms 12244 KB Output is correct
77 Correct 10 ms 12244 KB Output is correct
78 Correct 63 ms 18024 KB Output is correct
79 Correct 82 ms 22912 KB Output is correct
80 Correct 12 ms 12244 KB Output is correct
81 Correct 10 ms 12196 KB Output is correct
82 Correct 8 ms 12000 KB Output is correct
83 Correct 8 ms 11988 KB Output is correct
84 Correct 10 ms 12244 KB Output is correct
85 Correct 101 ms 20020 KB Output is correct
86 Correct 9 ms 12244 KB Output is correct
87 Correct 98 ms 17860 KB Output is correct
88 Correct 9 ms 12052 KB Output is correct
89 Correct 113 ms 17788 KB Output is correct
90 Correct 111 ms 19896 KB Output is correct
91 Correct 132 ms 22668 KB Output is correct
92 Correct 51 ms 17992 KB Output is correct
93 Correct 8 ms 12244 KB Output is correct
94 Correct 6 ms 11988 KB Output is correct
95 Correct 55 ms 22464 KB Output is correct
96 Correct 67 ms 24592 KB Output is correct
97 Correct 7 ms 12244 KB Output is correct
98 Correct 6 ms 11988 KB Output is correct
99 Correct 55 ms 20232 KB Output is correct
100 Correct 8 ms 12244 KB Output is correct
101 Correct 64 ms 18252 KB Output is correct
102 Correct 68 ms 23812 KB Output is correct
103 Correct 7 ms 12372 KB Output is correct
104 Correct 9 ms 12356 KB Output is correct
105 Correct 8 ms 12244 KB Output is correct
106 Correct 7 ms 12192 KB Output is correct
107 Correct 7 ms 12244 KB Output is correct
108 Correct 401 ms 60468 KB Output is correct
109 Correct 550 ms 56360 KB Output is correct
110 Correct 535 ms 63020 KB Output is correct
111 Correct 632 ms 87040 KB Output is correct
112 Correct 610 ms 81216 KB Output is correct
113 Correct 427 ms 74068 KB Output is correct
114 Correct 460 ms 47652 KB Output is correct
115 Correct 448 ms 47676 KB Output is correct
116 Correct 550 ms 54716 KB Output is correct
117 Correct 589 ms 75288 KB Output is correct
118 Correct 455 ms 49004 KB Output is correct
119 Correct 583 ms 75340 KB Output is correct
120 Correct 611 ms 81576 KB Output is correct
121 Correct 565 ms 75252 KB Output is correct
122 Correct 585 ms 64332 KB Output is correct
123 Correct 398 ms 76856 KB Output is correct
124 Correct 598 ms 73576 KB Output is correct