답안 #332534

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332534 2020-12-02T19:49:40 Z Lawliet Mergers (JOI19_mergers) C++17
100 / 100
915 ms 84788 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 500010;

int n;
int curTime;
int qtdWrong, qtdSpecialEdges;

int v[MAXN];
int freq[MAXN];
int sizeGroup[MAXN];
int degree[MAXN];
int U[MAXN], V[MAXN];
int anc[MAXN], rnk[MAXN];

int sub[MAXN];
int nodeTime[MAXN];
int heavyChild[MAXN];
int tin[MAXN], tout[MAXN];

vector<int> adj[MAXN];

int find(int cur)
{
	if( anc[cur] == cur ) return cur;
	return anc[cur] = find( anc[cur] );
}

void join(int i, int j)
{
	i = find(i); j = find(j);

	if( i == j ) return;

	if( rnk[i] < rnk[j] )
		swap( i , j );

	anc[j] = i;
	if( rnk[i] == rnk[j] ) rnk[i]++;
}

void DFSInit(int cur, int p)
{
	sub[cur] = 1;
	tin[cur] = ++curTime;
	nodeTime[ tin[cur] ] = cur;

	for(int i = 0 ; i < (int)adj[cur].size() ; i++) 
	{
		int viz = adj[cur][i];

		if( viz == p ) continue;

		DFSInit( viz , cur );
		sub[cur] += sub[viz];

		if( sub[ heavyChild[cur] ] < sub[viz] )
			heavyChild[cur] = viz;
	}

	tout[cur] = curTime;
}

void add(int node)
{
	if( freq[ v[node] ] == 0 ) qtdWrong++;
	freq[ v[node] ]++;
	if( freq[ v[node] ] == sizeGroup[ v[node] ] ) qtdWrong--;
}

void DFSSack(int cur, int p, bool keep = false)
{
	if( cur == 0 ) return;

	for(int i = 0 ; i < (int)adj[cur].size() ; i++)
	{
		int viz = adj[cur][i];

		if( viz == p ) continue;
		if( viz == heavyChild[cur] ) continue;

		DFSSack( viz , cur );
	}

	DFSSack( heavyChild[cur] , cur , true );

	add( cur );

	for(int i = 0 ; i < (int)adj[cur].size() ; i++)
	{
		int viz = adj[cur][i];

		if( viz == p ) continue;
		if( viz == heavyChild[cur] ) continue;

		for(int j = tin[viz] ; j <= tout[viz] ; j++)
			add( nodeTime[j] );
	}

	if( qtdWrong != 0 )
	{
		join( cur , p );
		qtdSpecialEdges++;
	}

	if( keep ) return;

	qtdWrong = 0;

	for(int i = tin[cur] ; i <= tout[cur] ; i++)
	{
		int node = nodeTime[i];
		freq[ v[node] ] --;
	}
}

int main()
{
	scanf("%d %*d",&n);

	iota( anc + 1 , anc + n + 1 , 1 );

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

		adj[ U[i] ].push_back( V[i] );
		adj[ V[i] ].push_back( U[i] );
	}

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

	DFSInit( 1 , 1 );
	DFSSack( 1 , 1 );

	if( qtdSpecialEdges == n - 1 )
	{
		printf("0\n");
		return 0;
	}

	for(int i = 1 ; i < n ; i++)
	{
		U[i] = find( U[i] );
		V[i] = find( V[i] );

		if( U[i] != V[i] )
			degree[ U[i] ]++, degree[ V[i] ]++;
	}

	int ans = 0;

	for(int i = 1 ; i <= n ; i++)
		if( degree[i] == 1 ) ans++;

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

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:121:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  121 |  scanf("%d %*d",&n);
      |  ~~~~~^~~~~~~~~~~~~
mergers.cpp:127:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  127 |   scanf("%d %d",&U[i],&V[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
mergers.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  135 |   scanf("%d",&v[i]);
      |   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 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 8 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 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 8 ms 12140 KB Output is correct
23 Correct 8 ms 12140 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 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 8 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 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 8 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 12 ms 12396 KB Output is correct
27 Correct 10 ms 12396 KB Output is correct
28 Correct 12 ms 12524 KB Output is correct
29 Correct 12 ms 12396 KB Output is correct
30 Correct 12 ms 12396 KB Output is correct
31 Correct 9 ms 12140 KB Output is correct
32 Correct 12 ms 12652 KB Output is correct
33 Correct 9 ms 12140 KB Output is correct
34 Correct 14 ms 12396 KB Output is correct
35 Correct 10 ms 12396 KB Output is correct
36 Correct 10 ms 12416 KB Output is correct
37 Correct 10 ms 12396 KB Output is correct
38 Correct 8 ms 12140 KB Output is correct
39 Correct 11 ms 12396 KB Output is correct
40 Correct 10 ms 12396 KB Output is correct
41 Correct 10 ms 12396 KB Output is correct
42 Correct 10 ms 12396 KB Output is correct
43 Correct 10 ms 12652 KB Output is correct
44 Correct 8 ms 12140 KB Output is correct
45 Correct 10 ms 12524 KB Output is correct
46 Correct 11 ms 12396 KB Output is correct
47 Correct 9 ms 12140 KB Output is correct
48 Correct 10 ms 12396 KB Output is correct
49 Correct 10 ms 12396 KB Output is correct
50 Correct 10 ms 12524 KB Output is correct
51 Correct 12 ms 12396 KB Output is correct
52 Correct 10 ms 12396 KB Output is correct
53 Correct 10 ms 12396 KB Output is correct
54 Correct 10 ms 12396 KB Output is correct
# 결과 실행 시간 메모리 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 8 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 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 8 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 73 ms 20196 KB Output is correct
27 Correct 111 ms 20844 KB Output is correct
28 Correct 11 ms 12396 KB Output is correct
29 Correct 8 ms 12140 KB Output is correct
30 Correct 10 ms 12140 KB Output is correct
31 Correct 140 ms 20844 KB Output is correct
32 Correct 10 ms 12396 KB Output is correct
33 Correct 117 ms 27500 KB Output is correct
34 Correct 121 ms 20844 KB Output is correct
35 Correct 10 ms 12396 KB Output is correct
36 Correct 107 ms 21484 KB Output is correct
37 Correct 10 ms 12396 KB Output is correct
38 Correct 11 ms 12396 KB Output is correct
39 Correct 75 ms 20192 KB Output is correct
40 Correct 10 ms 12652 KB Output is correct
41 Correct 139 ms 20588 KB Output is correct
42 Correct 112 ms 22636 KB Output is correct
43 Correct 8 ms 12140 KB Output is correct
44 Correct 120 ms 27776 KB Output is correct
45 Correct 125 ms 24428 KB Output is correct
46 Correct 10 ms 12396 KB Output is correct
47 Correct 11 ms 12396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 20196 KB Output is correct
2 Correct 80 ms 21732 KB Output is correct
3 Correct 10 ms 12544 KB Output is correct
4 Correct 10 ms 12396 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 10 ms 12396 KB Output is correct
8 Correct 131 ms 21868 KB Output is correct
9 Correct 10 ms 12396 KB Output is correct
10 Correct 119 ms 21356 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 116 ms 21228 KB Output is correct
13 Correct 124 ms 21740 KB Output is correct
14 Correct 119 ms 21868 KB Output is correct
15 Correct 94 ms 20332 KB Output is correct
16 Correct 11 ms 12396 KB Output is correct
17 Correct 9 ms 12140 KB Output is correct
18 Correct 81 ms 21604 KB Output is correct
19 Correct 114 ms 25708 KB Output is correct
20 Correct 10 ms 12396 KB Output is correct
21 Correct 8 ms 12140 KB Output is correct
22 Correct 82 ms 21348 KB Output is correct
23 Correct 11 ms 12396 KB Output is correct
24 Correct 121 ms 21356 KB Output is correct
25 Correct 123 ms 24428 KB Output is correct
26 Correct 10 ms 12396 KB Output is correct
27 Correct 10 ms 12524 KB Output is correct
28 Correct 10 ms 12396 KB Output is correct
29 Correct 10 ms 12396 KB Output is correct
30 Correct 10 ms 12396 KB Output is correct
# 결과 실행 시간 메모리 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 8 ms 12140 KB Output is correct
9 Correct 8 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 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 8 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 12 ms 12396 KB Output is correct
27 Correct 10 ms 12396 KB Output is correct
28 Correct 12 ms 12524 KB Output is correct
29 Correct 12 ms 12396 KB Output is correct
30 Correct 12 ms 12396 KB Output is correct
31 Correct 9 ms 12140 KB Output is correct
32 Correct 12 ms 12652 KB Output is correct
33 Correct 9 ms 12140 KB Output is correct
34 Correct 14 ms 12396 KB Output is correct
35 Correct 10 ms 12396 KB Output is correct
36 Correct 10 ms 12416 KB Output is correct
37 Correct 10 ms 12396 KB Output is correct
38 Correct 8 ms 12140 KB Output is correct
39 Correct 11 ms 12396 KB Output is correct
40 Correct 10 ms 12396 KB Output is correct
41 Correct 10 ms 12396 KB Output is correct
42 Correct 10 ms 12396 KB Output is correct
43 Correct 10 ms 12652 KB Output is correct
44 Correct 8 ms 12140 KB Output is correct
45 Correct 10 ms 12524 KB Output is correct
46 Correct 11 ms 12396 KB Output is correct
47 Correct 9 ms 12140 KB Output is correct
48 Correct 10 ms 12396 KB Output is correct
49 Correct 10 ms 12396 KB Output is correct
50 Correct 10 ms 12524 KB Output is correct
51 Correct 12 ms 12396 KB Output is correct
52 Correct 10 ms 12396 KB Output is correct
53 Correct 10 ms 12396 KB Output is correct
54 Correct 10 ms 12396 KB Output is correct
55 Correct 8 ms 12140 KB Output is correct
56 Correct 73 ms 20196 KB Output is correct
57 Correct 111 ms 20844 KB Output is correct
58 Correct 11 ms 12396 KB Output is correct
59 Correct 8 ms 12140 KB Output is correct
60 Correct 10 ms 12140 KB Output is correct
61 Correct 140 ms 20844 KB Output is correct
62 Correct 10 ms 12396 KB Output is correct
63 Correct 117 ms 27500 KB Output is correct
64 Correct 121 ms 20844 KB Output is correct
65 Correct 10 ms 12396 KB Output is correct
66 Correct 107 ms 21484 KB Output is correct
67 Correct 10 ms 12396 KB Output is correct
68 Correct 11 ms 12396 KB Output is correct
69 Correct 75 ms 20192 KB Output is correct
70 Correct 10 ms 12652 KB Output is correct
71 Correct 139 ms 20588 KB Output is correct
72 Correct 112 ms 22636 KB Output is correct
73 Correct 8 ms 12140 KB Output is correct
74 Correct 120 ms 27776 KB Output is correct
75 Correct 125 ms 24428 KB Output is correct
76 Correct 10 ms 12396 KB Output is correct
77 Correct 11 ms 12396 KB Output is correct
78 Correct 74 ms 20196 KB Output is correct
79 Correct 80 ms 21732 KB Output is correct
80 Correct 10 ms 12544 KB Output is correct
81 Correct 10 ms 12396 KB Output is correct
82 Correct 10 ms 12140 KB Output is correct
83 Correct 8 ms 12140 KB Output is correct
84 Correct 10 ms 12396 KB Output is correct
85 Correct 131 ms 21868 KB Output is correct
86 Correct 10 ms 12396 KB Output is correct
87 Correct 119 ms 21356 KB Output is correct
88 Correct 8 ms 12140 KB Output is correct
89 Correct 116 ms 21228 KB Output is correct
90 Correct 124 ms 21740 KB Output is correct
91 Correct 119 ms 21868 KB Output is correct
92 Correct 94 ms 20332 KB Output is correct
93 Correct 11 ms 12396 KB Output is correct
94 Correct 9 ms 12140 KB Output is correct
95 Correct 81 ms 21604 KB Output is correct
96 Correct 114 ms 25708 KB Output is correct
97 Correct 10 ms 12396 KB Output is correct
98 Correct 8 ms 12140 KB Output is correct
99 Correct 82 ms 21348 KB Output is correct
100 Correct 11 ms 12396 KB Output is correct
101 Correct 121 ms 21356 KB Output is correct
102 Correct 123 ms 24428 KB Output is correct
103 Correct 10 ms 12396 KB Output is correct
104 Correct 10 ms 12524 KB Output is correct
105 Correct 10 ms 12396 KB Output is correct
106 Correct 10 ms 12396 KB Output is correct
107 Correct 10 ms 12396 KB Output is correct
108 Correct 603 ms 58920 KB Output is correct
109 Correct 796 ms 71532 KB Output is correct
110 Correct 784 ms 83020 KB Output is correct
111 Correct 805 ms 84788 KB Output is correct
112 Correct 915 ms 75060 KB Output is correct
113 Correct 622 ms 61140 KB Output is correct
114 Correct 821 ms 55660 KB Output is correct
115 Correct 790 ms 55776 KB Output is correct
116 Correct 858 ms 60012 KB Output is correct
117 Correct 859 ms 61804 KB Output is correct
118 Correct 805 ms 57196 KB Output is correct
119 Correct 845 ms 61804 KB Output is correct
120 Correct 890 ms 75884 KB Output is correct
121 Correct 861 ms 62316 KB Output is correct
122 Correct 884 ms 62188 KB Output is correct
123 Correct 601 ms 61404 KB Output is correct
124 Correct 823 ms 62428 KB Output is correct