Submission #332587

# Submission time Handle Problem Language Result Execution time Memory
332587 2020-12-02T22:25:56 Z CaroLinda Mergers (JOI19_mergers) C++14
100 / 100
1231 ms 98524 KB
#include <bits/stdc++.h>

#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size() )
#define ll long long

const int MAXN = 5e5+10 ;

using namespace std ;

int n , k ;
int state[MAXN] , deg[MAXN] ;
int dsu[MAXN] , qtdState[MAXN] ;
vector<int> adj[MAXN] ;
map<int,int> *ptr[MAXN] ;


int find(int x) 
{
	if(x == dsu[x] ) return x ;
	return dsu[x] = find(dsu[x] ) ;
}

void join(int x, int y )
{
	x = find(x) ;
	y = find(y) ;

	if(x  == y ) return ;

	if(rand() % 2 ) swap(x,y) ;

	deg[x] += deg[y] ;
	dsu[y] = x ;

}

int dfs(int x, int father)
{
	int bc = -1 ;
	int subBc = 0 ;
	int sub = 1 ;

	for(auto e : adj[x] )
	{

		if(e == father ) continue ;

		int subChild = dfs(e,x) ;
		sub += subChild ;

		if(subChild > subBc )
		{
			subBc = subChild ;
			bc = e ;			
		}

	}

	if(bc == -1 ) ptr[x] = new map<int,int> ;
	else ptr[x] = ptr[bc] ;

	if( sz( (*ptr[x]) ) == 0 && bc != -1 )
	{
		deg[ find(bc) ]++ ;
		deg[ find(x) ]++ ;
	}
	else if(bc != -1 ) join(x, bc) ;

	for(auto e : adj[x] )
	{
		if(e == father || e == bc ) continue ;
		if( sz( (*ptr[e] ) ) == 0 ) 
		{
			deg[ find(x) ]++ ;
			deg[ find(e) ]++ ;
			continue ;
		}

		join(x, e) ;

		for(auto p : (*ptr[e] ) )
		{
			(*ptr[x])[p.first] += p.second ;
			if( (*ptr[x])[p.first]  == qtdState[p.first] )	
				(*ptr[x]).erase( (*ptr[x]).find(p.first) ) ;		
		}

		(*ptr[e]).clear() ;
	}

	(*ptr[x])[ state[x] ]++ ;

	if( (*ptr[x])[ state[x] ] == qtdState[ state[x] ] )
		(*ptr[x]).erase( (*ptr[x]).find( state[x] ) ) ;

	return sub ;

}

int main()
{
	scanf("%d %d", &n, &k ) ;
	for(int i = 0 , u , v ; i < n-1 ; i++ )
	{
		scanf("%d %d", &u, &v ) ;

		adj[u].push_back(v) ;
		adj[v].push_back(u) ;

	}
	for(int i = 1 ; i <= n ; i++ ) 
	{
		scanf("%d", &state[i] ) ;

		dsu[i] = i ;
		qtdState[ state[i] ]++ ;
	}

	dfs(1,-1) ;

	int qtd = 0 ;

	for(int i = 1 ; i <= n ; i++ )
		if( find(i) == i && deg[i] == 1 )  qtd++ ;

	printf("%d\n", (qtd+1)/2 ) ;

}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  103 |  scanf("%d %d", &n, &k ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:106:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  106 |   scanf("%d %d", &u, &v ) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:114:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  114 |   scanf("%d", &state[i] ) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 8 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 8 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
14 Correct 8 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 9 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 8 ms 12140 KB Output is correct
21 Correct 8 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 8 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 8 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 8 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
14 Correct 8 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 9 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 8 ms 12140 KB Output is correct
21 Correct 8 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 8 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 8 ms 12140 KB Output is correct
26 Correct 10 ms 12396 KB Output is correct
27 Correct 10 ms 12268 KB Output is correct
28 Correct 11 ms 12652 KB Output is correct
29 Correct 10 ms 12396 KB Output is correct
30 Correct 11 ms 12400 KB Output is correct
31 Correct 7 ms 12056 KB Output is correct
32 Correct 11 ms 12652 KB Output is correct
33 Correct 8 ms 12140 KB Output is correct
34 Correct 10 ms 12652 KB Output is correct
35 Correct 10 ms 12396 KB Output is correct
36 Correct 10 ms 12396 KB Output is correct
37 Correct 11 ms 12396 KB Output is correct
38 Correct 8 ms 12140 KB Output is correct
39 Correct 11 ms 12524 KB Output is correct
40 Correct 10 ms 12396 KB Output is correct
41 Correct 10 ms 12396 KB Output is correct
42 Correct 11 ms 12652 KB Output is correct
43 Correct 10 ms 12780 KB Output is correct
44 Correct 8 ms 12160 KB Output is correct
45 Correct 12 ms 12396 KB Output is correct
46 Correct 10 ms 12396 KB Output is correct
47 Correct 10 ms 12140 KB Output is correct
48 Correct 10 ms 12396 KB Output is correct
49 Correct 9 ms 12524 KB Output is correct
50 Correct 10 ms 12524 KB Output is correct
51 Correct 10 ms 12268 KB Output is correct
52 Correct 12 ms 12652 KB Output is correct
53 Correct 10 ms 12652 KB Output is correct
54 Correct 10 ms 12396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 8 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 8 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
14 Correct 8 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 9 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 8 ms 12140 KB Output is correct
21 Correct 8 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 8 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 8 ms 12140 KB Output is correct
26 Correct 99 ms 30436 KB Output is correct
27 Correct 144 ms 21996 KB Output is correct
28 Correct 9 ms 12268 KB Output is correct
29 Correct 8 ms 12140 KB Output is correct
30 Correct 8 ms 12140 KB Output is correct
31 Correct 105 ms 21740 KB Output is correct
32 Correct 10 ms 12652 KB Output is correct
33 Correct 121 ms 33272 KB Output is correct
34 Correct 126 ms 21868 KB Output is correct
35 Correct 10 ms 12396 KB Output is correct
36 Correct 94 ms 20332 KB Output is correct
37 Correct 10 ms 12396 KB Output is correct
38 Correct 10 ms 12396 KB Output is correct
39 Correct 101 ms 29540 KB Output is correct
40 Correct 9 ms 12780 KB Output is correct
41 Correct 104 ms 21740 KB Output is correct
42 Correct 117 ms 23276 KB Output is correct
43 Correct 9 ms 12140 KB Output is correct
44 Correct 118 ms 33644 KB Output is correct
45 Correct 128 ms 30752 KB Output is correct
46 Correct 10 ms 12652 KB Output is correct
47 Correct 12 ms 12396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 28900 KB Output is correct
2 Correct 88 ms 25188 KB Output is correct
3 Correct 10 ms 12416 KB Output is correct
4 Correct 10 ms 12268 KB Output is correct
5 Correct 8 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 10 ms 12652 KB Output is correct
8 Correct 134 ms 22124 KB Output is correct
9 Correct 10 ms 12396 KB Output is correct
10 Correct 106 ms 21868 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 93 ms 18924 KB Output is correct
13 Correct 121 ms 21612 KB Output is correct
14 Correct 106 ms 22380 KB Output is correct
15 Correct 99 ms 29548 KB Output is correct
16 Correct 11 ms 12652 KB Output is correct
17 Correct 8 ms 12140 KB Output is correct
18 Correct 95 ms 26544 KB Output is correct
19 Correct 115 ms 27244 KB Output is correct
20 Correct 10 ms 12396 KB Output is correct
21 Correct 8 ms 12140 KB Output is correct
22 Correct 137 ms 30564 KB Output is correct
23 Correct 10 ms 12396 KB Output is correct
24 Correct 103 ms 19820 KB Output is correct
25 Correct 123 ms 27372 KB Output is correct
26 Correct 10 ms 12524 KB Output is correct
27 Correct 11 ms 12524 KB Output is correct
28 Correct 10 ms 12268 KB Output is correct
29 Correct 10 ms 12652 KB Output is correct
30 Correct 11 ms 12652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 8 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 8 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
14 Correct 8 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 9 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 8 ms 12140 KB Output is correct
21 Correct 8 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 8 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 8 ms 12140 KB Output is correct
26 Correct 10 ms 12396 KB Output is correct
27 Correct 10 ms 12268 KB Output is correct
28 Correct 11 ms 12652 KB Output is correct
29 Correct 10 ms 12396 KB Output is correct
30 Correct 11 ms 12400 KB Output is correct
31 Correct 7 ms 12056 KB Output is correct
32 Correct 11 ms 12652 KB Output is correct
33 Correct 8 ms 12140 KB Output is correct
34 Correct 10 ms 12652 KB Output is correct
35 Correct 10 ms 12396 KB Output is correct
36 Correct 10 ms 12396 KB Output is correct
37 Correct 11 ms 12396 KB Output is correct
38 Correct 8 ms 12140 KB Output is correct
39 Correct 11 ms 12524 KB Output is correct
40 Correct 10 ms 12396 KB Output is correct
41 Correct 10 ms 12396 KB Output is correct
42 Correct 11 ms 12652 KB Output is correct
43 Correct 10 ms 12780 KB Output is correct
44 Correct 8 ms 12160 KB Output is correct
45 Correct 12 ms 12396 KB Output is correct
46 Correct 10 ms 12396 KB Output is correct
47 Correct 10 ms 12140 KB Output is correct
48 Correct 10 ms 12396 KB Output is correct
49 Correct 9 ms 12524 KB Output is correct
50 Correct 10 ms 12524 KB Output is correct
51 Correct 10 ms 12268 KB Output is correct
52 Correct 12 ms 12652 KB Output is correct
53 Correct 10 ms 12652 KB Output is correct
54 Correct 10 ms 12396 KB Output is correct
55 Correct 8 ms 12140 KB Output is correct
56 Correct 99 ms 30436 KB Output is correct
57 Correct 144 ms 21996 KB Output is correct
58 Correct 9 ms 12268 KB Output is correct
59 Correct 8 ms 12140 KB Output is correct
60 Correct 8 ms 12140 KB Output is correct
61 Correct 105 ms 21740 KB Output is correct
62 Correct 10 ms 12652 KB Output is correct
63 Correct 121 ms 33272 KB Output is correct
64 Correct 126 ms 21868 KB Output is correct
65 Correct 10 ms 12396 KB Output is correct
66 Correct 94 ms 20332 KB Output is correct
67 Correct 10 ms 12396 KB Output is correct
68 Correct 10 ms 12396 KB Output is correct
69 Correct 101 ms 29540 KB Output is correct
70 Correct 9 ms 12780 KB Output is correct
71 Correct 104 ms 21740 KB Output is correct
72 Correct 117 ms 23276 KB Output is correct
73 Correct 9 ms 12140 KB Output is correct
74 Correct 118 ms 33644 KB Output is correct
75 Correct 128 ms 30752 KB Output is correct
76 Correct 10 ms 12652 KB Output is correct
77 Correct 12 ms 12396 KB Output is correct
78 Correct 93 ms 28900 KB Output is correct
79 Correct 88 ms 25188 KB Output is correct
80 Correct 10 ms 12416 KB Output is correct
81 Correct 10 ms 12268 KB Output is correct
82 Correct 8 ms 12140 KB Output is correct
83 Correct 8 ms 12140 KB Output is correct
84 Correct 10 ms 12652 KB Output is correct
85 Correct 134 ms 22124 KB Output is correct
86 Correct 10 ms 12396 KB Output is correct
87 Correct 106 ms 21868 KB Output is correct
88 Correct 8 ms 12140 KB Output is correct
89 Correct 93 ms 18924 KB Output is correct
90 Correct 121 ms 21612 KB Output is correct
91 Correct 106 ms 22380 KB Output is correct
92 Correct 99 ms 29548 KB Output is correct
93 Correct 11 ms 12652 KB Output is correct
94 Correct 8 ms 12140 KB Output is correct
95 Correct 95 ms 26544 KB Output is correct
96 Correct 115 ms 27244 KB Output is correct
97 Correct 10 ms 12396 KB Output is correct
98 Correct 8 ms 12140 KB Output is correct
99 Correct 137 ms 30564 KB Output is correct
100 Correct 10 ms 12396 KB Output is correct
101 Correct 103 ms 19820 KB Output is correct
102 Correct 123 ms 27372 KB Output is correct
103 Correct 10 ms 12524 KB Output is correct
104 Correct 11 ms 12524 KB Output is correct
105 Correct 10 ms 12268 KB Output is correct
106 Correct 10 ms 12652 KB Output is correct
107 Correct 11 ms 12652 KB Output is correct
108 Correct 1096 ms 97276 KB Output is correct
109 Correct 679 ms 76012 KB Output is correct
110 Correct 688 ms 98524 KB Output is correct
111 Correct 808 ms 88044 KB Output is correct
112 Correct 849 ms 83948 KB Output is correct
113 Correct 706 ms 78036 KB Output is correct
114 Correct 704 ms 55276 KB Output is correct
115 Correct 703 ms 55108 KB Output is correct
116 Correct 1055 ms 56556 KB Output is correct
117 Correct 775 ms 57068 KB Output is correct
118 Correct 690 ms 54448 KB Output is correct
119 Correct 773 ms 56940 KB Output is correct
120 Correct 836 ms 85428 KB Output is correct
121 Correct 771 ms 56812 KB Output is correct
122 Correct 1231 ms 64108 KB Output is correct
123 Correct 576 ms 73948 KB Output is correct
124 Correct 761 ms 41072 KB Output is correct