Submission #110960

# Submission time Handle Problem Language Result Execution time Memory
110960 2019-05-13T11:54:47 Z igzi Mergers (JOI19_mergers) C++17
100 / 100
1342 ms 102372 KB
#include <bits/stdc++.h>
#define maxN 500005
 
using namespace std;
 
vector <int> v,adj[maxN],g[maxN];
long long x,y,i,n,k,c[maxN],l[maxN],d[maxN],s[maxN],a[maxN],cnt[maxN];
bool b[maxN];
 
void dfs(int n,int par){
int i;
v.push_back(c[n]);
l[n]=v.size()-1;
d[n]=v.size()-1;
for(i=0;i<adj[n].size();i++){
    if(adj[n][i]!=par){
        dfs(adj[n][i],n);
        d[n]=d[adj[n][i]];
    }
}
}
 
void dfs1(int n,int par){
long long tmp,i;
if(l[n]==0) tmp=0;
else tmp=s[l[n]-1];
if(par!=-1 && (s[d[n]]-tmp)!=0){
g[n].push_back(par);
g[par].push_back(n);
}
for(i=0;i<adj[n].size();i++){
    if(adj[n][i]!=par){
        dfs1(adj[n][i],n);
    }
}
}
 
void dfs2(int n){
int i;
b[n]=true;
x+=adj[n].size()-g[n].size();
for(i=0;i<g[n].size();i++){
    if(!b[g[n][i]]){
        dfs2(g[n][i]);
    }
}
}
 
int main()
{
    srand(time(NULL));
    std::ios_base::sync_with_stdio(0);
    cin>>n>>k;
    for(i=1;i<n;i++){
        cin>>x>>y;
        x--;
        y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    for(i=0;i<n;i++){
        cin>>c[i];
        c[i]--;
    }
    dfs(0,-1);
    for(i=0;i<n;i++){
        a[i]=rand()+(rand()<<32);
    }
    for(i=0;i<v.size();i++) cnt[v[i]]++;
    for(i=0;i<v.size();i++){
        if(!b[v[i]]){
            s[i]=cnt[v[i]]*a[v[i]];
            b[v[i]]=true;
        }
        s[i]-=a[v[i]];
    }
    for(i=1;i<n;i++){
        s[i]+=s[i-1];
    }
    dfs1(0,-1);
    for(i=0;i<n;i++) b[i]=false;
    y=0;
    for(i=0;i<n;i++){
        if(!b[i]){
            x=0;
            dfs2(i);
            if(x==1) y++;
        }
    }
    cout<<(y+1)/2<<endl;
    return 0;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:15:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 for(i=0;i<adj[n].size();i++){
         ~^~~~~~~~~~~~~~
mergers.cpp: In function 'void dfs1(int, int)':
mergers.cpp:31:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 for(i=0;i<adj[n].size();i++){
         ~^~~~~~~~~~~~~~
mergers.cpp: In function 'void dfs2(int)':
mergers.cpp:42:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 for(i=0;i<g[n].size();i++){
         ~^~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:67:30: warning: left shift count >= width of type [-Wshift-count-overflow]
         a[i]=rand()+(rand()<<32);
                              ^~
mergers.cpp:69:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<v.size();i++) cnt[v[i]]++;
             ~^~~~~~~~~
mergers.cpp:70:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<v.size();i++){
             ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23936 KB Output is correct
2 Correct 24 ms 23808 KB Output is correct
3 Correct 24 ms 23808 KB Output is correct
4 Correct 23 ms 23936 KB Output is correct
5 Correct 24 ms 23808 KB Output is correct
6 Correct 23 ms 23808 KB Output is correct
7 Correct 23 ms 23928 KB Output is correct
8 Correct 22 ms 23808 KB Output is correct
9 Correct 25 ms 23808 KB Output is correct
10 Correct 24 ms 23936 KB Output is correct
11 Correct 27 ms 24064 KB Output is correct
12 Correct 25 ms 23936 KB Output is correct
13 Correct 23 ms 23908 KB Output is correct
14 Correct 24 ms 23936 KB Output is correct
15 Correct 24 ms 23936 KB Output is correct
16 Correct 23 ms 23936 KB Output is correct
17 Correct 25 ms 23936 KB Output is correct
18 Correct 27 ms 23936 KB Output is correct
19 Correct 24 ms 23808 KB Output is correct
20 Correct 25 ms 23936 KB Output is correct
21 Correct 28 ms 23936 KB Output is correct
22 Correct 25 ms 23932 KB Output is correct
23 Correct 24 ms 23808 KB Output is correct
24 Correct 25 ms 23804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23936 KB Output is correct
2 Correct 24 ms 23808 KB Output is correct
3 Correct 24 ms 23808 KB Output is correct
4 Correct 23 ms 23936 KB Output is correct
5 Correct 24 ms 23808 KB Output is correct
6 Correct 23 ms 23808 KB Output is correct
7 Correct 23 ms 23928 KB Output is correct
8 Correct 22 ms 23808 KB Output is correct
9 Correct 25 ms 23808 KB Output is correct
10 Correct 24 ms 23936 KB Output is correct
11 Correct 27 ms 24064 KB Output is correct
12 Correct 25 ms 23936 KB Output is correct
13 Correct 23 ms 23908 KB Output is correct
14 Correct 24 ms 23936 KB Output is correct
15 Correct 24 ms 23936 KB Output is correct
16 Correct 23 ms 23936 KB Output is correct
17 Correct 25 ms 23936 KB Output is correct
18 Correct 27 ms 23936 KB Output is correct
19 Correct 24 ms 23808 KB Output is correct
20 Correct 25 ms 23936 KB Output is correct
21 Correct 28 ms 23936 KB Output is correct
22 Correct 25 ms 23932 KB Output is correct
23 Correct 24 ms 23808 KB Output is correct
24 Correct 25 ms 23804 KB Output is correct
25 Correct 24 ms 23936 KB Output is correct
26 Correct 29 ms 24184 KB Output is correct
27 Correct 29 ms 24188 KB Output is correct
28 Correct 26 ms 24320 KB Output is correct
29 Correct 30 ms 24216 KB Output is correct
30 Correct 25 ms 24192 KB Output is correct
31 Correct 24 ms 23808 KB Output is correct
32 Correct 31 ms 24348 KB Output is correct
33 Correct 24 ms 23936 KB Output is correct
34 Correct 26 ms 24312 KB Output is correct
35 Correct 27 ms 24192 KB Output is correct
36 Correct 33 ms 24184 KB Output is correct
37 Correct 28 ms 24312 KB Output is correct
38 Correct 25 ms 23936 KB Output is correct
39 Correct 30 ms 24184 KB Output is correct
40 Correct 27 ms 24320 KB Output is correct
41 Correct 26 ms 24184 KB Output is correct
42 Correct 26 ms 24320 KB Output is correct
43 Correct 27 ms 24440 KB Output is correct
44 Correct 27 ms 23928 KB Output is correct
45 Correct 26 ms 24320 KB Output is correct
46 Correct 26 ms 24188 KB Output is correct
47 Correct 23 ms 23936 KB Output is correct
48 Correct 28 ms 24320 KB Output is correct
49 Correct 26 ms 24184 KB Output is correct
50 Correct 25 ms 24192 KB Output is correct
51 Correct 25 ms 24312 KB Output is correct
52 Correct 26 ms 24312 KB Output is correct
53 Correct 29 ms 24312 KB Output is correct
54 Correct 28 ms 24320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23936 KB Output is correct
2 Correct 24 ms 23808 KB Output is correct
3 Correct 24 ms 23808 KB Output is correct
4 Correct 23 ms 23936 KB Output is correct
5 Correct 24 ms 23808 KB Output is correct
6 Correct 23 ms 23808 KB Output is correct
7 Correct 23 ms 23928 KB Output is correct
8 Correct 22 ms 23808 KB Output is correct
9 Correct 25 ms 23808 KB Output is correct
10 Correct 24 ms 23936 KB Output is correct
11 Correct 27 ms 24064 KB Output is correct
12 Correct 25 ms 23936 KB Output is correct
13 Correct 23 ms 23908 KB Output is correct
14 Correct 24 ms 23936 KB Output is correct
15 Correct 24 ms 23936 KB Output is correct
16 Correct 23 ms 23936 KB Output is correct
17 Correct 25 ms 23936 KB Output is correct
18 Correct 27 ms 23936 KB Output is correct
19 Correct 24 ms 23808 KB Output is correct
20 Correct 25 ms 23936 KB Output is correct
21 Correct 28 ms 23936 KB Output is correct
22 Correct 25 ms 23932 KB Output is correct
23 Correct 24 ms 23808 KB Output is correct
24 Correct 25 ms 23804 KB Output is correct
25 Correct 26 ms 23808 KB Output is correct
26 Correct 156 ms 36636 KB Output is correct
27 Correct 183 ms 36008 KB Output is correct
28 Correct 28 ms 24284 KB Output is correct
29 Correct 25 ms 23808 KB Output is correct
30 Correct 23 ms 23936 KB Output is correct
31 Correct 209 ms 35952 KB Output is correct
32 Correct 27 ms 24312 KB Output is correct
33 Correct 231 ms 41552 KB Output is correct
34 Correct 170 ms 35952 KB Output is correct
35 Correct 25 ms 24192 KB Output is correct
36 Correct 170 ms 36604 KB Output is correct
37 Correct 29 ms 24192 KB Output is correct
38 Correct 26 ms 24320 KB Output is correct
39 Correct 125 ms 36632 KB Output is correct
40 Correct 32 ms 24440 KB Output is correct
41 Correct 186 ms 36052 KB Output is correct
42 Correct 240 ms 37760 KB Output is correct
43 Correct 23 ms 23936 KB Output is correct
44 Correct 236 ms 41840 KB Output is correct
45 Correct 227 ms 38896 KB Output is correct
46 Correct 26 ms 24312 KB Output is correct
47 Correct 29 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 35312 KB Output is correct
2 Correct 127 ms 32752 KB Output is correct
3 Correct 26 ms 24188 KB Output is correct
4 Correct 28 ms 24176 KB Output is correct
5 Correct 25 ms 23928 KB Output is correct
6 Correct 28 ms 23936 KB Output is correct
7 Correct 30 ms 24312 KB Output is correct
8 Correct 201 ms 36148 KB Output is correct
9 Correct 31 ms 24320 KB Output is correct
10 Correct 176 ms 36096 KB Output is correct
11 Correct 27 ms 23936 KB Output is correct
12 Correct 227 ms 35824 KB Output is correct
13 Correct 198 ms 35908 KB Output is correct
14 Correct 124 ms 33780 KB Output is correct
15 Correct 135 ms 36660 KB Output is correct
16 Correct 27 ms 24192 KB Output is correct
17 Correct 27 ms 23936 KB Output is correct
18 Correct 131 ms 34660 KB Output is correct
19 Correct 181 ms 36984 KB Output is correct
20 Correct 27 ms 24192 KB Output is correct
21 Correct 23 ms 23928 KB Output is correct
22 Correct 116 ms 36364 KB Output is correct
23 Correct 29 ms 24184 KB Output is correct
24 Correct 213 ms 35824 KB Output is correct
25 Correct 147 ms 35668 KB Output is correct
26 Correct 25 ms 24184 KB Output is correct
27 Correct 26 ms 24184 KB Output is correct
28 Correct 31 ms 24192 KB Output is correct
29 Correct 29 ms 24316 KB Output is correct
30 Correct 31 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23936 KB Output is correct
2 Correct 24 ms 23808 KB Output is correct
3 Correct 24 ms 23808 KB Output is correct
4 Correct 23 ms 23936 KB Output is correct
5 Correct 24 ms 23808 KB Output is correct
6 Correct 23 ms 23808 KB Output is correct
7 Correct 23 ms 23928 KB Output is correct
8 Correct 22 ms 23808 KB Output is correct
9 Correct 25 ms 23808 KB Output is correct
10 Correct 24 ms 23936 KB Output is correct
11 Correct 27 ms 24064 KB Output is correct
12 Correct 25 ms 23936 KB Output is correct
13 Correct 23 ms 23908 KB Output is correct
14 Correct 24 ms 23936 KB Output is correct
15 Correct 24 ms 23936 KB Output is correct
16 Correct 23 ms 23936 KB Output is correct
17 Correct 25 ms 23936 KB Output is correct
18 Correct 27 ms 23936 KB Output is correct
19 Correct 24 ms 23808 KB Output is correct
20 Correct 25 ms 23936 KB Output is correct
21 Correct 28 ms 23936 KB Output is correct
22 Correct 25 ms 23932 KB Output is correct
23 Correct 24 ms 23808 KB Output is correct
24 Correct 25 ms 23804 KB Output is correct
25 Correct 24 ms 23936 KB Output is correct
26 Correct 29 ms 24184 KB Output is correct
27 Correct 29 ms 24188 KB Output is correct
28 Correct 26 ms 24320 KB Output is correct
29 Correct 30 ms 24216 KB Output is correct
30 Correct 25 ms 24192 KB Output is correct
31 Correct 24 ms 23808 KB Output is correct
32 Correct 31 ms 24348 KB Output is correct
33 Correct 24 ms 23936 KB Output is correct
34 Correct 26 ms 24312 KB Output is correct
35 Correct 27 ms 24192 KB Output is correct
36 Correct 33 ms 24184 KB Output is correct
37 Correct 28 ms 24312 KB Output is correct
38 Correct 25 ms 23936 KB Output is correct
39 Correct 30 ms 24184 KB Output is correct
40 Correct 27 ms 24320 KB Output is correct
41 Correct 26 ms 24184 KB Output is correct
42 Correct 26 ms 24320 KB Output is correct
43 Correct 27 ms 24440 KB Output is correct
44 Correct 27 ms 23928 KB Output is correct
45 Correct 26 ms 24320 KB Output is correct
46 Correct 26 ms 24188 KB Output is correct
47 Correct 23 ms 23936 KB Output is correct
48 Correct 28 ms 24320 KB Output is correct
49 Correct 26 ms 24184 KB Output is correct
50 Correct 25 ms 24192 KB Output is correct
51 Correct 25 ms 24312 KB Output is correct
52 Correct 26 ms 24312 KB Output is correct
53 Correct 29 ms 24312 KB Output is correct
54 Correct 28 ms 24320 KB Output is correct
55 Correct 26 ms 23808 KB Output is correct
56 Correct 156 ms 36636 KB Output is correct
57 Correct 183 ms 36008 KB Output is correct
58 Correct 28 ms 24284 KB Output is correct
59 Correct 25 ms 23808 KB Output is correct
60 Correct 23 ms 23936 KB Output is correct
61 Correct 209 ms 35952 KB Output is correct
62 Correct 27 ms 24312 KB Output is correct
63 Correct 231 ms 41552 KB Output is correct
64 Correct 170 ms 35952 KB Output is correct
65 Correct 25 ms 24192 KB Output is correct
66 Correct 170 ms 36604 KB Output is correct
67 Correct 29 ms 24192 KB Output is correct
68 Correct 26 ms 24320 KB Output is correct
69 Correct 125 ms 36632 KB Output is correct
70 Correct 32 ms 24440 KB Output is correct
71 Correct 186 ms 36052 KB Output is correct
72 Correct 240 ms 37760 KB Output is correct
73 Correct 23 ms 23936 KB Output is correct
74 Correct 236 ms 41840 KB Output is correct
75 Correct 227 ms 38896 KB Output is correct
76 Correct 26 ms 24312 KB Output is correct
77 Correct 29 ms 24312 KB Output is correct
78 Correct 130 ms 35312 KB Output is correct
79 Correct 127 ms 32752 KB Output is correct
80 Correct 26 ms 24188 KB Output is correct
81 Correct 28 ms 24176 KB Output is correct
82 Correct 25 ms 23928 KB Output is correct
83 Correct 28 ms 23936 KB Output is correct
84 Correct 30 ms 24312 KB Output is correct
85 Correct 201 ms 36148 KB Output is correct
86 Correct 31 ms 24320 KB Output is correct
87 Correct 176 ms 36096 KB Output is correct
88 Correct 27 ms 23936 KB Output is correct
89 Correct 227 ms 35824 KB Output is correct
90 Correct 198 ms 35908 KB Output is correct
91 Correct 124 ms 33780 KB Output is correct
92 Correct 135 ms 36660 KB Output is correct
93 Correct 27 ms 24192 KB Output is correct
94 Correct 27 ms 23936 KB Output is correct
95 Correct 131 ms 34660 KB Output is correct
96 Correct 181 ms 36984 KB Output is correct
97 Correct 27 ms 24192 KB Output is correct
98 Correct 23 ms 23928 KB Output is correct
99 Correct 116 ms 36364 KB Output is correct
100 Correct 29 ms 24184 KB Output is correct
101 Correct 213 ms 35824 KB Output is correct
102 Correct 147 ms 35668 KB Output is correct
103 Correct 25 ms 24184 KB Output is correct
104 Correct 26 ms 24184 KB Output is correct
105 Correct 31 ms 24192 KB Output is correct
106 Correct 29 ms 24316 KB Output is correct
107 Correct 31 ms 24184 KB Output is correct
108 Correct 768 ms 82340 KB Output is correct
109 Correct 1340 ms 93372 KB Output is correct
110 Correct 1342 ms 102372 KB Output is correct
111 Correct 1038 ms 87012 KB Output is correct
112 Correct 1177 ms 85092 KB Output is correct
113 Correct 667 ms 71932 KB Output is correct
114 Correct 1067 ms 79544 KB Output is correct
115 Correct 1067 ms 79592 KB Output is correct
116 Correct 1135 ms 80448 KB Output is correct
117 Correct 763 ms 67444 KB Output is correct
118 Correct 1093 ms 78548 KB Output is correct
119 Correct 811 ms 67480 KB Output is correct
120 Correct 889 ms 78640 KB Output is correct
121 Correct 829 ms 67412 KB Output is correct
122 Correct 1062 ms 79240 KB Output is correct
123 Correct 503 ms 68888 KB Output is correct
124 Correct 944 ms 71632 KB Output is correct