#include<bits/stdc++.h>
using namespace std;
#define int long long
struct wi{
vector<int> Q;
int kolor=0;
int zle=0;
int gore=0;
int suma=0;
}*w;
int ile[600000];
map<int,int> mapka[600000];
int wynik=0;
int wszyscy=0;
void dfs(int x,int ojc)
{
mapka[x][w[x].kolor]++;
if(ile[w[x].kolor]==1)
mapka[x].erase(w[x].kolor);
for(int i=0;i<w[x].Q.size();i++)
{
int pom=w[x].Q[i];
if(pom==ojc)
continue;
dfs(pom,x);
if(mapka[pom].size()>mapka[x].size())
swap(mapka[pom],mapka[x]);
for(auto it=mapka[pom].begin();it!=mapka[pom].end();it++)
{
mapka[x][it->first]+=it->second;
if(mapka[x][it->first]==ile[it->first])
mapka[x].erase(it->first);
}
}
if(mapka[x].size()==0 && x!=1)
w[x].zle=1,w[ojc].zle=1;
}
void dfs2(int x,int ojc)
{
w[x].suma+=w[x].zle;
int teraz=0;
for(int i=0;i<w[x].Q.size();i++)
{
int pom=w[x].Q[i];
if(pom==ojc)
continue;
dfs2(pom,x);
w[x].suma+=w[pom].suma;
if(w[pom].suma>=1)
teraz++;
}
//cout<<x<<": "<<w[x].suma<<" "<<wszyscy<<" "<<teraz<<"\n";
if(w[x].suma==1 && w[x].zle==1)
wynik++;
else if(w[x].suma==wszyscy && teraz==1 && w[x].zle==1)
wynik++;
}
int32_t main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n,k,x,y;
cin>>n>>k;
w=new wi[n+3];
for(int i=1;i<n;i++)
{
cin>>x>>y;
w[x].Q.push_back(y);
w[y].Q.push_back(x);
}
for(int i=1;i<=n;i++)
{
cin>>x;
ile[x]++;
w[i].kolor=x;
}
dfs(1,0);
for(int i=1;i<=n;i++)
wszyscy+=w[i].zle;
dfs2(1,0);
//cout<<wynik<<"\n";
cout<<(wynik+1)/2;
return 0;
}
Compilation message
mergers.cpp: In function 'void dfs(long long int, long long int)':
mergers.cpp:20:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i=0;i<w[x].Q.size();i++)
| ~^~~~~~~~~~~~~~
mergers.cpp: In function 'void dfs2(long long int, long long int)':
mergers.cpp:42:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i=0;i<w[x].Q.size();i++)
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29272 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29276 KB |
Output is correct |
8 |
Correct |
6 ms |
29328 KB |
Output is correct |
9 |
Correct |
6 ms |
29360 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
8 ms |
29532 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29276 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29276 KB |
Output is correct |
19 |
Correct |
6 ms |
29364 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29272 KB |
Output is correct |
22 |
Correct |
6 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29276 KB |
Output is correct |
24 |
Correct |
7 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29272 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29276 KB |
Output is correct |
8 |
Correct |
6 ms |
29328 KB |
Output is correct |
9 |
Correct |
6 ms |
29360 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
8 ms |
29532 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29276 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29276 KB |
Output is correct |
19 |
Correct |
6 ms |
29364 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29272 KB |
Output is correct |
22 |
Correct |
6 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29276 KB |
Output is correct |
24 |
Correct |
7 ms |
29276 KB |
Output is correct |
25 |
Correct |
7 ms |
29276 KB |
Output is correct |
26 |
Correct |
9 ms |
29840 KB |
Output is correct |
27 |
Correct |
8 ms |
29788 KB |
Output is correct |
28 |
Correct |
7 ms |
29660 KB |
Output is correct |
29 |
Correct |
7 ms |
29532 KB |
Output is correct |
30 |
Correct |
9 ms |
30044 KB |
Output is correct |
31 |
Correct |
6 ms |
29276 KB |
Output is correct |
32 |
Correct |
10 ms |
29788 KB |
Output is correct |
33 |
Correct |
6 ms |
29280 KB |
Output is correct |
34 |
Correct |
7 ms |
29788 KB |
Output is correct |
35 |
Correct |
9 ms |
29636 KB |
Output is correct |
36 |
Correct |
8 ms |
29888 KB |
Output is correct |
37 |
Correct |
8 ms |
30044 KB |
Output is correct |
38 |
Correct |
7 ms |
29276 KB |
Output is correct |
39 |
Correct |
9 ms |
30044 KB |
Output is correct |
40 |
Correct |
7 ms |
29788 KB |
Output is correct |
41 |
Correct |
7 ms |
29880 KB |
Output is correct |
42 |
Correct |
8 ms |
29788 KB |
Output is correct |
43 |
Correct |
8 ms |
30040 KB |
Output is correct |
44 |
Correct |
7 ms |
29276 KB |
Output is correct |
45 |
Correct |
7 ms |
29532 KB |
Output is correct |
46 |
Correct |
8 ms |
29788 KB |
Output is correct |
47 |
Correct |
6 ms |
29272 KB |
Output is correct |
48 |
Correct |
8 ms |
29788 KB |
Output is correct |
49 |
Correct |
8 ms |
29532 KB |
Output is correct |
50 |
Correct |
7 ms |
29788 KB |
Output is correct |
51 |
Correct |
8 ms |
29788 KB |
Output is correct |
52 |
Correct |
8 ms |
29788 KB |
Output is correct |
53 |
Correct |
8 ms |
29788 KB |
Output is correct |
54 |
Correct |
8 ms |
30140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29272 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29276 KB |
Output is correct |
8 |
Correct |
6 ms |
29328 KB |
Output is correct |
9 |
Correct |
6 ms |
29360 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
8 ms |
29532 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29276 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29276 KB |
Output is correct |
19 |
Correct |
6 ms |
29364 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29272 KB |
Output is correct |
22 |
Correct |
6 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29276 KB |
Output is correct |
24 |
Correct |
7 ms |
29276 KB |
Output is correct |
25 |
Correct |
6 ms |
29276 KB |
Output is correct |
26 |
Correct |
67 ms |
46280 KB |
Output is correct |
27 |
Correct |
97 ms |
54988 KB |
Output is correct |
28 |
Correct |
8 ms |
29868 KB |
Output is correct |
29 |
Correct |
6 ms |
29308 KB |
Output is correct |
30 |
Correct |
6 ms |
29276 KB |
Output is correct |
31 |
Correct |
62 ms |
46164 KB |
Output is correct |
32 |
Correct |
9 ms |
29784 KB |
Output is correct |
33 |
Correct |
108 ms |
57152 KB |
Output is correct |
34 |
Correct |
81 ms |
53332 KB |
Output is correct |
35 |
Correct |
8 ms |
29820 KB |
Output is correct |
36 |
Correct |
73 ms |
46792 KB |
Output is correct |
37 |
Correct |
7 ms |
29784 KB |
Output is correct |
38 |
Correct |
7 ms |
29820 KB |
Output is correct |
39 |
Correct |
61 ms |
46312 KB |
Output is correct |
40 |
Correct |
7 ms |
30040 KB |
Output is correct |
41 |
Correct |
63 ms |
46164 KB |
Output is correct |
42 |
Correct |
91 ms |
49492 KB |
Output is correct |
43 |
Correct |
6 ms |
29272 KB |
Output is correct |
44 |
Correct |
96 ms |
57696 KB |
Output is correct |
45 |
Correct |
83 ms |
54352 KB |
Output is correct |
46 |
Correct |
7 ms |
29788 KB |
Output is correct |
47 |
Correct |
8 ms |
30044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
46280 KB |
Output is correct |
2 |
Correct |
42 ms |
42448 KB |
Output is correct |
3 |
Correct |
7 ms |
29532 KB |
Output is correct |
4 |
Correct |
8 ms |
29812 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29276 KB |
Output is correct |
7 |
Correct |
7 ms |
29892 KB |
Output is correct |
8 |
Correct |
86 ms |
49488 KB |
Output is correct |
9 |
Correct |
8 ms |
30040 KB |
Output is correct |
10 |
Correct |
71 ms |
46164 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
63 ms |
45652 KB |
Output is correct |
13 |
Correct |
71 ms |
45176 KB |
Output is correct |
14 |
Correct |
57 ms |
42324 KB |
Output is correct |
15 |
Correct |
52 ms |
46400 KB |
Output is correct |
16 |
Correct |
8 ms |
29788 KB |
Output is correct |
17 |
Correct |
6 ms |
29360 KB |
Output is correct |
18 |
Correct |
56 ms |
41860 KB |
Output is correct |
19 |
Correct |
85 ms |
48208 KB |
Output is correct |
20 |
Correct |
8 ms |
29784 KB |
Output is correct |
21 |
Correct |
6 ms |
29276 KB |
Output is correct |
22 |
Correct |
84 ms |
45352 KB |
Output is correct |
23 |
Correct |
8 ms |
29788 KB |
Output is correct |
24 |
Correct |
74 ms |
45292 KB |
Output is correct |
25 |
Correct |
88 ms |
47216 KB |
Output is correct |
26 |
Correct |
10 ms |
29788 KB |
Output is correct |
27 |
Correct |
7 ms |
29784 KB |
Output is correct |
28 |
Correct |
7 ms |
30040 KB |
Output is correct |
29 |
Correct |
7 ms |
29788 KB |
Output is correct |
30 |
Correct |
8 ms |
29784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29272 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29276 KB |
Output is correct |
8 |
Correct |
6 ms |
29328 KB |
Output is correct |
9 |
Correct |
6 ms |
29360 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
8 ms |
29532 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29276 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29276 KB |
Output is correct |
19 |
Correct |
6 ms |
29364 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29272 KB |
Output is correct |
22 |
Correct |
6 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29276 KB |
Output is correct |
24 |
Correct |
7 ms |
29276 KB |
Output is correct |
25 |
Correct |
7 ms |
29276 KB |
Output is correct |
26 |
Correct |
9 ms |
29840 KB |
Output is correct |
27 |
Correct |
8 ms |
29788 KB |
Output is correct |
28 |
Correct |
7 ms |
29660 KB |
Output is correct |
29 |
Correct |
7 ms |
29532 KB |
Output is correct |
30 |
Correct |
9 ms |
30044 KB |
Output is correct |
31 |
Correct |
6 ms |
29276 KB |
Output is correct |
32 |
Correct |
10 ms |
29788 KB |
Output is correct |
33 |
Correct |
6 ms |
29280 KB |
Output is correct |
34 |
Correct |
7 ms |
29788 KB |
Output is correct |
35 |
Correct |
9 ms |
29636 KB |
Output is correct |
36 |
Correct |
8 ms |
29888 KB |
Output is correct |
37 |
Correct |
8 ms |
30044 KB |
Output is correct |
38 |
Correct |
7 ms |
29276 KB |
Output is correct |
39 |
Correct |
9 ms |
30044 KB |
Output is correct |
40 |
Correct |
7 ms |
29788 KB |
Output is correct |
41 |
Correct |
7 ms |
29880 KB |
Output is correct |
42 |
Correct |
8 ms |
29788 KB |
Output is correct |
43 |
Correct |
8 ms |
30040 KB |
Output is correct |
44 |
Correct |
7 ms |
29276 KB |
Output is correct |
45 |
Correct |
7 ms |
29532 KB |
Output is correct |
46 |
Correct |
8 ms |
29788 KB |
Output is correct |
47 |
Correct |
6 ms |
29272 KB |
Output is correct |
48 |
Correct |
8 ms |
29788 KB |
Output is correct |
49 |
Correct |
8 ms |
29532 KB |
Output is correct |
50 |
Correct |
7 ms |
29788 KB |
Output is correct |
51 |
Correct |
8 ms |
29788 KB |
Output is correct |
52 |
Correct |
8 ms |
29788 KB |
Output is correct |
53 |
Correct |
8 ms |
29788 KB |
Output is correct |
54 |
Correct |
8 ms |
30140 KB |
Output is correct |
55 |
Correct |
6 ms |
29276 KB |
Output is correct |
56 |
Correct |
67 ms |
46280 KB |
Output is correct |
57 |
Correct |
97 ms |
54988 KB |
Output is correct |
58 |
Correct |
8 ms |
29868 KB |
Output is correct |
59 |
Correct |
6 ms |
29308 KB |
Output is correct |
60 |
Correct |
6 ms |
29276 KB |
Output is correct |
61 |
Correct |
62 ms |
46164 KB |
Output is correct |
62 |
Correct |
9 ms |
29784 KB |
Output is correct |
63 |
Correct |
108 ms |
57152 KB |
Output is correct |
64 |
Correct |
81 ms |
53332 KB |
Output is correct |
65 |
Correct |
8 ms |
29820 KB |
Output is correct |
66 |
Correct |
73 ms |
46792 KB |
Output is correct |
67 |
Correct |
7 ms |
29784 KB |
Output is correct |
68 |
Correct |
7 ms |
29820 KB |
Output is correct |
69 |
Correct |
61 ms |
46312 KB |
Output is correct |
70 |
Correct |
7 ms |
30040 KB |
Output is correct |
71 |
Correct |
63 ms |
46164 KB |
Output is correct |
72 |
Correct |
91 ms |
49492 KB |
Output is correct |
73 |
Correct |
6 ms |
29272 KB |
Output is correct |
74 |
Correct |
96 ms |
57696 KB |
Output is correct |
75 |
Correct |
83 ms |
54352 KB |
Output is correct |
76 |
Correct |
7 ms |
29788 KB |
Output is correct |
77 |
Correct |
8 ms |
30044 KB |
Output is correct |
78 |
Correct |
56 ms |
46280 KB |
Output is correct |
79 |
Correct |
42 ms |
42448 KB |
Output is correct |
80 |
Correct |
7 ms |
29532 KB |
Output is correct |
81 |
Correct |
8 ms |
29812 KB |
Output is correct |
82 |
Correct |
6 ms |
29276 KB |
Output is correct |
83 |
Correct |
7 ms |
29276 KB |
Output is correct |
84 |
Correct |
7 ms |
29892 KB |
Output is correct |
85 |
Correct |
86 ms |
49488 KB |
Output is correct |
86 |
Correct |
8 ms |
30040 KB |
Output is correct |
87 |
Correct |
71 ms |
46164 KB |
Output is correct |
88 |
Correct |
6 ms |
29276 KB |
Output is correct |
89 |
Correct |
63 ms |
45652 KB |
Output is correct |
90 |
Correct |
71 ms |
45176 KB |
Output is correct |
91 |
Correct |
57 ms |
42324 KB |
Output is correct |
92 |
Correct |
52 ms |
46400 KB |
Output is correct |
93 |
Correct |
8 ms |
29788 KB |
Output is correct |
94 |
Correct |
6 ms |
29360 KB |
Output is correct |
95 |
Correct |
56 ms |
41860 KB |
Output is correct |
96 |
Correct |
85 ms |
48208 KB |
Output is correct |
97 |
Correct |
8 ms |
29784 KB |
Output is correct |
98 |
Correct |
6 ms |
29276 KB |
Output is correct |
99 |
Correct |
84 ms |
45352 KB |
Output is correct |
100 |
Correct |
8 ms |
29788 KB |
Output is correct |
101 |
Correct |
74 ms |
45292 KB |
Output is correct |
102 |
Correct |
88 ms |
47216 KB |
Output is correct |
103 |
Correct |
10 ms |
29788 KB |
Output is correct |
104 |
Correct |
7 ms |
29784 KB |
Output is correct |
105 |
Correct |
7 ms |
30040 KB |
Output is correct |
106 |
Correct |
7 ms |
29788 KB |
Output is correct |
107 |
Correct |
8 ms |
29784 KB |
Output is correct |
108 |
Correct |
647 ms |
116632 KB |
Output is correct |
109 |
Correct |
669 ms |
140996 KB |
Output is correct |
110 |
Correct |
752 ms |
159056 KB |
Output is correct |
111 |
Correct |
634 ms |
123092 KB |
Output is correct |
112 |
Correct |
613 ms |
113488 KB |
Output is correct |
113 |
Correct |
388 ms |
95260 KB |
Output is correct |
114 |
Correct |
609 ms |
114592 KB |
Output is correct |
115 |
Correct |
532 ms |
114404 KB |
Output is correct |
116 |
Correct |
830 ms |
194700 KB |
Output is correct |
117 |
Correct |
490 ms |
88624 KB |
Output is correct |
118 |
Correct |
506 ms |
113536 KB |
Output is correct |
119 |
Correct |
504 ms |
88488 KB |
Output is correct |
120 |
Correct |
615 ms |
114588 KB |
Output is correct |
121 |
Correct |
512 ms |
88600 KB |
Output is correct |
122 |
Correct |
964 ms |
204708 KB |
Output is correct |
123 |
Correct |
307 ms |
89592 KB |
Output is correct |
124 |
Correct |
524 ms |
85420 KB |
Output is correct |