답안 #148073

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
148073 2019-08-31T13:39:16 Z WhipppedCream Mergers (JOI19_mergers) C++17
100 / 100
1336 ms 99184 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
 
const int maxn = 5e5+5;
 
int n, k;
 
vector<int> adj[maxn];
int col[maxn];
int colcnt[maxn];
int tar[maxn];
vector<int> *bst[maxn];
int cnt[maxn];
bool bad[maxn];
bool notbad[maxn];
int par[maxn];
bool vis[maxn];
int om[maxn];
int chil[maxn];
int mem[maxn];
 
void gosubtree(int u = 1, int p = 0)
{
	cnt[u] = 1;
	par[u] = p;
	for(int v : adj[u])
	{
		if(v == p) continue;
		chil[u]++;
		gosubtree(v, u);
		cnt[u] += cnt[v];
	}
}
 
void solve(int u = 1, int p = 0, bool keep = 1)
{
	ii big = {0, -1};
	tar[u] = 0;
	for(int v : adj[u])
	{
		if(v == p) continue;
		big = max(big, {cnt[v], v});
	}
	int best = big.Y;
	for(int v : adj[u])
	{
		if(v == p) continue;
		if(v == best) continue;
		solve(v, u, 0);
	}
	if(best != -1)
	{
		solve(best, u, 1); bst[u] = bst[best]; 
		tar[u] = tar[best];
	}
	else bst[u] = new vector<int>();
	bst[u]->pb(u);
	mem[col[u]]++;
	if(mem[col[u]] == 1) tar[u] += colcnt[col[u]];
	for(int v : adj[u])
	{
		if(v == p || v == best) continue;
		for(int x : *bst[v])
		{
			bst[u]->pb(x);
			mem[col[x]]++;
			if(mem[col[x]] == 1) tar[u] += colcnt[col[x]];
		}
	}
	bad[u] = (tar[u] == cnt[u]);
	// printf("%d is bad %d\n", u, bad[u]);
	if(!keep)
	{
		for(int x : *bst[u]) mem[col[x]]--;
	}
}
 
int main()
{
	scanf("%d %d", &n, &k);
	for(int i = 0; i< n-1; i++)
	{
		int u, v; scanf("%d %d", &u, &v);
		adj[u].pb(v); adj[v].pb(u);
	}
	for(int i = 1; i<= n; i++)
	{
		scanf("%d", &col[i]);
		colcnt[col[i]]++;
	}
	gosubtree(); solve();
	queue<int> q;
	for(int i = 2; i<= n; i++)
	{
		if(bad[i])
		{
			q.push(par[i]);
			// printf("%d is bad\n", i);
		}
	}
	while(!q.empty())
	{
		int u = q.front(); q.pop();
		notbad[u] = true;
		int v = par[u];
		if(!v) continue;
		if(!vis[v])
		{
			vis[v] = true;
			q.push(v);
		}
	}
	int res = 0;
	for(int i = 2; i<= n; i++)
	{
		if(bad[i] && !notbad[i])
		{
			res++;
			om[par[i]]++;
		}
	}
	for(int i = 1; i<= n; i++)
	{
		if(!chil[i]) q.push(i);
	}
	while(!q.empty())
	{
		int u = q.front(); q.pop();
		int v = par[u];
		if(!v) continue;
		om[v] += om[u];
		chil[v]--;
		if(chil[v] == 0) q.push(v);
	}
	bool superbad = false;
	for(int i = 2; i<= n; i++)
	{
		if(bad[i] && om[i] == res)
		{
			superbad = true;
			printf("%d\n", (res+superbad+1)/2);
			return 0;
		}
	}
	printf("%d\n", (res+superbad+1)/2);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:90:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; scanf("%d %d", &u, &v);
             ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &col[i]);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 13 ms 12180 KB Output is correct
6 Correct 13 ms 12152 KB Output is correct
7 Correct 13 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 13 ms 12124 KB Output is correct
10 Correct 12 ms 12124 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 13 ms 12124 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 13 ms 12152 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 12 ms 12152 KB Output is correct
23 Correct 13 ms 12152 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 13 ms 12180 KB Output is correct
6 Correct 13 ms 12152 KB Output is correct
7 Correct 13 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 13 ms 12124 KB Output is correct
10 Correct 12 ms 12124 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 13 ms 12124 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 13 ms 12152 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 12 ms 12152 KB Output is correct
23 Correct 13 ms 12152 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 13 ms 12152 KB Output is correct
26 Correct 15 ms 12536 KB Output is correct
27 Correct 15 ms 12408 KB Output is correct
28 Correct 15 ms 12664 KB Output is correct
29 Correct 15 ms 12536 KB Output is correct
30 Correct 16 ms 12536 KB Output is correct
31 Correct 13 ms 12144 KB Output is correct
32 Correct 15 ms 12604 KB Output is correct
33 Correct 13 ms 12088 KB Output is correct
34 Correct 15 ms 12512 KB Output is correct
35 Correct 15 ms 12536 KB Output is correct
36 Correct 15 ms 12536 KB Output is correct
37 Correct 16 ms 12536 KB Output is correct
38 Correct 13 ms 12172 KB Output is correct
39 Correct 15 ms 12504 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 15 ms 12536 KB Output is correct
42 Correct 14 ms 12536 KB Output is correct
43 Correct 15 ms 12692 KB Output is correct
44 Correct 12 ms 12152 KB Output is correct
45 Correct 14 ms 12536 KB Output is correct
46 Correct 15 ms 12536 KB Output is correct
47 Correct 12 ms 12152 KB Output is correct
48 Correct 15 ms 12536 KB Output is correct
49 Correct 14 ms 12536 KB Output is correct
50 Correct 15 ms 12664 KB Output is correct
51 Correct 15 ms 12408 KB Output is correct
52 Correct 15 ms 12596 KB Output is correct
53 Correct 15 ms 12536 KB Output is correct
54 Correct 15 ms 12536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 13 ms 12180 KB Output is correct
6 Correct 13 ms 12152 KB Output is correct
7 Correct 13 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 13 ms 12124 KB Output is correct
10 Correct 12 ms 12124 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 13 ms 12124 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 13 ms 12152 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 12 ms 12152 KB Output is correct
23 Correct 13 ms 12152 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 12 ms 12152 KB Output is correct
26 Correct 120 ms 26732 KB Output is correct
27 Correct 155 ms 25280 KB Output is correct
28 Correct 18 ms 12408 KB Output is correct
29 Correct 12 ms 12152 KB Output is correct
30 Correct 12 ms 12152 KB Output is correct
31 Correct 151 ms 25576 KB Output is correct
32 Correct 14 ms 12508 KB Output is correct
33 Correct 138 ms 30856 KB Output is correct
34 Correct 151 ms 25528 KB Output is correct
35 Correct 16 ms 12536 KB Output is correct
36 Correct 130 ms 22584 KB Output is correct
37 Correct 15 ms 12536 KB Output is correct
38 Correct 15 ms 12536 KB Output is correct
39 Correct 115 ms 26604 KB Output is correct
40 Correct 15 ms 12664 KB Output is correct
41 Correct 146 ms 25328 KB Output is correct
42 Correct 134 ms 24304 KB Output is correct
43 Correct 12 ms 12124 KB Output is correct
44 Correct 137 ms 31072 KB Output is correct
45 Correct 149 ms 28968 KB Output is correct
46 Correct 14 ms 12628 KB Output is correct
47 Correct 15 ms 12496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 26548 KB Output is correct
2 Correct 118 ms 27760 KB Output is correct
3 Correct 15 ms 12536 KB Output is correct
4 Correct 15 ms 12412 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 15 ms 12536 KB Output is correct
8 Correct 162 ms 26036 KB Output is correct
9 Correct 15 ms 12536 KB Output is correct
10 Correct 147 ms 25584 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 153 ms 22432 KB Output is correct
13 Correct 155 ms 25816 KB Output is correct
14 Correct 166 ms 26864 KB Output is correct
15 Correct 110 ms 26604 KB Output is correct
16 Correct 18 ms 12664 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 115 ms 27720 KB Output is correct
19 Correct 136 ms 27840 KB Output is correct
20 Correct 27 ms 12536 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 111 ms 27244 KB Output is correct
23 Correct 16 ms 12536 KB Output is correct
24 Correct 140 ms 24048 KB Output is correct
25 Correct 145 ms 29048 KB Output is correct
26 Correct 14 ms 12664 KB Output is correct
27 Correct 15 ms 12664 KB Output is correct
28 Correct 16 ms 12536 KB Output is correct
29 Correct 14 ms 12536 KB Output is correct
30 Correct 14 ms 12664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 13 ms 12180 KB Output is correct
6 Correct 13 ms 12152 KB Output is correct
7 Correct 13 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 13 ms 12124 KB Output is correct
10 Correct 12 ms 12124 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 13 ms 12124 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 13 ms 12152 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 12 ms 12152 KB Output is correct
23 Correct 13 ms 12152 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 13 ms 12152 KB Output is correct
26 Correct 15 ms 12536 KB Output is correct
27 Correct 15 ms 12408 KB Output is correct
28 Correct 15 ms 12664 KB Output is correct
29 Correct 15 ms 12536 KB Output is correct
30 Correct 16 ms 12536 KB Output is correct
31 Correct 13 ms 12144 KB Output is correct
32 Correct 15 ms 12604 KB Output is correct
33 Correct 13 ms 12088 KB Output is correct
34 Correct 15 ms 12512 KB Output is correct
35 Correct 15 ms 12536 KB Output is correct
36 Correct 15 ms 12536 KB Output is correct
37 Correct 16 ms 12536 KB Output is correct
38 Correct 13 ms 12172 KB Output is correct
39 Correct 15 ms 12504 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 15 ms 12536 KB Output is correct
42 Correct 14 ms 12536 KB Output is correct
43 Correct 15 ms 12692 KB Output is correct
44 Correct 12 ms 12152 KB Output is correct
45 Correct 14 ms 12536 KB Output is correct
46 Correct 15 ms 12536 KB Output is correct
47 Correct 12 ms 12152 KB Output is correct
48 Correct 15 ms 12536 KB Output is correct
49 Correct 14 ms 12536 KB Output is correct
50 Correct 15 ms 12664 KB Output is correct
51 Correct 15 ms 12408 KB Output is correct
52 Correct 15 ms 12596 KB Output is correct
53 Correct 15 ms 12536 KB Output is correct
54 Correct 15 ms 12536 KB Output is correct
55 Correct 12 ms 12152 KB Output is correct
56 Correct 120 ms 26732 KB Output is correct
57 Correct 155 ms 25280 KB Output is correct
58 Correct 18 ms 12408 KB Output is correct
59 Correct 12 ms 12152 KB Output is correct
60 Correct 12 ms 12152 KB Output is correct
61 Correct 151 ms 25576 KB Output is correct
62 Correct 14 ms 12508 KB Output is correct
63 Correct 138 ms 30856 KB Output is correct
64 Correct 151 ms 25528 KB Output is correct
65 Correct 16 ms 12536 KB Output is correct
66 Correct 130 ms 22584 KB Output is correct
67 Correct 15 ms 12536 KB Output is correct
68 Correct 15 ms 12536 KB Output is correct
69 Correct 115 ms 26604 KB Output is correct
70 Correct 15 ms 12664 KB Output is correct
71 Correct 146 ms 25328 KB Output is correct
72 Correct 134 ms 24304 KB Output is correct
73 Correct 12 ms 12124 KB Output is correct
74 Correct 137 ms 31072 KB Output is correct
75 Correct 149 ms 28968 KB Output is correct
76 Correct 14 ms 12628 KB Output is correct
77 Correct 15 ms 12496 KB Output is correct
78 Correct 112 ms 26548 KB Output is correct
79 Correct 118 ms 27760 KB Output is correct
80 Correct 15 ms 12536 KB Output is correct
81 Correct 15 ms 12412 KB Output is correct
82 Correct 12 ms 12152 KB Output is correct
83 Correct 12 ms 12152 KB Output is correct
84 Correct 15 ms 12536 KB Output is correct
85 Correct 162 ms 26036 KB Output is correct
86 Correct 15 ms 12536 KB Output is correct
87 Correct 147 ms 25584 KB Output is correct
88 Correct 13 ms 12152 KB Output is correct
89 Correct 153 ms 22432 KB Output is correct
90 Correct 155 ms 25816 KB Output is correct
91 Correct 166 ms 26864 KB Output is correct
92 Correct 110 ms 26604 KB Output is correct
93 Correct 18 ms 12664 KB Output is correct
94 Correct 13 ms 12152 KB Output is correct
95 Correct 115 ms 27720 KB Output is correct
96 Correct 136 ms 27840 KB Output is correct
97 Correct 27 ms 12536 KB Output is correct
98 Correct 13 ms 12152 KB Output is correct
99 Correct 111 ms 27244 KB Output is correct
100 Correct 16 ms 12536 KB Output is correct
101 Correct 140 ms 24048 KB Output is correct
102 Correct 145 ms 29048 KB Output is correct
103 Correct 14 ms 12664 KB Output is correct
104 Correct 15 ms 12664 KB Output is correct
105 Correct 16 ms 12536 KB Output is correct
106 Correct 14 ms 12536 KB Output is correct
107 Correct 14 ms 12664 KB Output is correct
108 Correct 820 ms 88800 KB Output is correct
109 Correct 931 ms 82660 KB Output is correct
110 Correct 930 ms 95860 KB Output is correct
111 Correct 1047 ms 96492 KB Output is correct
112 Correct 1336 ms 97376 KB Output is correct
113 Correct 841 ms 90956 KB Output is correct
114 Correct 985 ms 79836 KB Output is correct
115 Correct 969 ms 79844 KB Output is correct
116 Correct 1004 ms 83424 KB Output is correct
117 Correct 1072 ms 88424 KB Output is correct
118 Correct 979 ms 79816 KB Output is correct
119 Correct 1104 ms 87956 KB Output is correct
120 Correct 1111 ms 99184 KB Output is correct
121 Correct 1112 ms 88024 KB Output is correct
122 Correct 1078 ms 85220 KB Output is correct
123 Correct 840 ms 91304 KB Output is correct
124 Correct 1004 ms 68200 KB Output is correct