Submission #128951

# Submission time Handle Problem Language Result Execution time Memory
128951 2019-07-11T11:11:41 Z roseanne_pcy Mergers (JOI19_mergers) C++14
100 / 100
1163 ms 91128 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]);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12128 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 13 ms 12152 KB Output is correct
10 Correct 13 ms 12152 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 12 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 12 ms 12152 KB Output is correct
19 Correct 13 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 13 ms 12152 KB Output is correct
23 Correct 13 ms 12152 KB Output is correct
24 Correct 13 ms 12156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12128 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 13 ms 12152 KB Output is correct
10 Correct 13 ms 12152 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 12 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 12 ms 12152 KB Output is correct
19 Correct 13 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 13 ms 12152 KB Output is correct
23 Correct 13 ms 12152 KB Output is correct
24 Correct 13 ms 12156 KB Output is correct
25 Correct 12 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 12636 KB Output is correct
29 Correct 15 ms 12536 KB Output is correct
30 Correct 15 ms 12536 KB Output is correct
31 Correct 12 ms 12152 KB Output is correct
32 Correct 14 ms 12664 KB Output is correct
33 Correct 13 ms 12152 KB Output is correct
34 Correct 14 ms 12508 KB Output is correct
35 Correct 15 ms 12536 KB Output is correct
36 Correct 15 ms 12536 KB Output is correct
37 Correct 15 ms 12536 KB Output is correct
38 Correct 12 ms 12152 KB Output is correct
39 Correct 16 ms 12536 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 15 ms 12508 KB Output is correct
42 Correct 15 ms 12536 KB Output is correct
43 Correct 15 ms 12664 KB Output is correct
44 Correct 13 ms 12152 KB Output is correct
45 Correct 15 ms 12540 KB Output is correct
46 Correct 15 ms 12536 KB Output is correct
47 Correct 13 ms 12152 KB Output is correct
48 Correct 15 ms 12536 KB Output is correct
49 Correct 14 ms 12600 KB Output is correct
50 Correct 15 ms 12596 KB Output is correct
51 Correct 15 ms 12508 KB Output is correct
52 Correct 14 ms 12536 KB Output is correct
53 Correct 15 ms 12584 KB Output is correct
54 Correct 15 ms 12536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12128 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 13 ms 12152 KB Output is correct
10 Correct 13 ms 12152 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 12 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 12 ms 12152 KB Output is correct
19 Correct 13 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 13 ms 12152 KB Output is correct
23 Correct 13 ms 12152 KB Output is correct
24 Correct 13 ms 12156 KB Output is correct
25 Correct 12 ms 12112 KB Output is correct
26 Correct 104 ms 26548 KB Output is correct
27 Correct 144 ms 25328 KB Output is correct
28 Correct 15 ms 12408 KB Output is correct
29 Correct 15 ms 12152 KB Output is correct
30 Correct 15 ms 12152 KB Output is correct
31 Correct 151 ms 25444 KB Output is correct
32 Correct 15 ms 12536 KB Output is correct
33 Correct 149 ms 30736 KB Output is correct
34 Correct 158 ms 25452 KB Output is correct
35 Correct 15 ms 12536 KB Output is correct
36 Correct 144 ms 22644 KB Output is correct
37 Correct 15 ms 12536 KB Output is correct
38 Correct 15 ms 12572 KB Output is correct
39 Correct 117 ms 26548 KB Output is correct
40 Correct 15 ms 12656 KB Output is correct
41 Correct 157 ms 25272 KB Output is correct
42 Correct 165 ms 24368 KB Output is correct
43 Correct 13 ms 12124 KB Output is correct
44 Correct 148 ms 30992 KB Output is correct
45 Correct 144 ms 29024 KB Output is correct
46 Correct 14 ms 12528 KB Output is correct
47 Correct 15 ms 12536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 25224 KB Output is correct
2 Correct 120 ms 25988 KB Output is correct
3 Correct 16 ms 12516 KB Output is correct
4 Correct 15 ms 12408 KB Output is correct
5 Correct 16 ms 12124 KB Output is correct
6 Correct 14 ms 12104 KB Output is correct
7 Correct 15 ms 12536 KB Output is correct
8 Correct 166 ms 26008 KB Output is correct
9 Correct 15 ms 12508 KB Output is correct
10 Correct 161 ms 25504 KB Output is correct
11 Correct 12 ms 12092 KB Output is correct
12 Correct 154 ms 22584 KB Output is correct
13 Correct 162 ms 25864 KB Output is correct
14 Correct 167 ms 26996 KB Output is correct
15 Correct 107 ms 26596 KB Output is correct
16 Correct 15 ms 12536 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 120 ms 27604 KB Output is correct
19 Correct 155 ms 27916 KB Output is correct
20 Correct 15 ms 12564 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 109 ms 27244 KB Output is correct
23 Correct 15 ms 12536 KB Output is correct
24 Correct 156 ms 23884 KB Output is correct
25 Correct 166 ms 28992 KB Output is correct
26 Correct 15 ms 12536 KB Output is correct
27 Correct 16 ms 12536 KB Output is correct
28 Correct 19 ms 12408 KB Output is correct
29 Correct 15 ms 12560 KB Output is correct
30 Correct 15 ms 12792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12128 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 13 ms 12152 KB Output is correct
10 Correct 13 ms 12152 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 12 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 12 ms 12152 KB Output is correct
19 Correct 13 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 13 ms 12152 KB Output is correct
23 Correct 13 ms 12152 KB Output is correct
24 Correct 13 ms 12156 KB Output is correct
25 Correct 12 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 12636 KB Output is correct
29 Correct 15 ms 12536 KB Output is correct
30 Correct 15 ms 12536 KB Output is correct
31 Correct 12 ms 12152 KB Output is correct
32 Correct 14 ms 12664 KB Output is correct
33 Correct 13 ms 12152 KB Output is correct
34 Correct 14 ms 12508 KB Output is correct
35 Correct 15 ms 12536 KB Output is correct
36 Correct 15 ms 12536 KB Output is correct
37 Correct 15 ms 12536 KB Output is correct
38 Correct 12 ms 12152 KB Output is correct
39 Correct 16 ms 12536 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 15 ms 12508 KB Output is correct
42 Correct 15 ms 12536 KB Output is correct
43 Correct 15 ms 12664 KB Output is correct
44 Correct 13 ms 12152 KB Output is correct
45 Correct 15 ms 12540 KB Output is correct
46 Correct 15 ms 12536 KB Output is correct
47 Correct 13 ms 12152 KB Output is correct
48 Correct 15 ms 12536 KB Output is correct
49 Correct 14 ms 12600 KB Output is correct
50 Correct 15 ms 12596 KB Output is correct
51 Correct 15 ms 12508 KB Output is correct
52 Correct 14 ms 12536 KB Output is correct
53 Correct 15 ms 12584 KB Output is correct
54 Correct 15 ms 12536 KB Output is correct
55 Correct 12 ms 12112 KB Output is correct
56 Correct 104 ms 26548 KB Output is correct
57 Correct 144 ms 25328 KB Output is correct
58 Correct 15 ms 12408 KB Output is correct
59 Correct 15 ms 12152 KB Output is correct
60 Correct 15 ms 12152 KB Output is correct
61 Correct 151 ms 25444 KB Output is correct
62 Correct 15 ms 12536 KB Output is correct
63 Correct 149 ms 30736 KB Output is correct
64 Correct 158 ms 25452 KB Output is correct
65 Correct 15 ms 12536 KB Output is correct
66 Correct 144 ms 22644 KB Output is correct
67 Correct 15 ms 12536 KB Output is correct
68 Correct 15 ms 12572 KB Output is correct
69 Correct 117 ms 26548 KB Output is correct
70 Correct 15 ms 12656 KB Output is correct
71 Correct 157 ms 25272 KB Output is correct
72 Correct 165 ms 24368 KB Output is correct
73 Correct 13 ms 12124 KB Output is correct
74 Correct 148 ms 30992 KB Output is correct
75 Correct 144 ms 29024 KB Output is correct
76 Correct 14 ms 12528 KB Output is correct
77 Correct 15 ms 12536 KB Output is correct
78 Correct 110 ms 25224 KB Output is correct
79 Correct 120 ms 25988 KB Output is correct
80 Correct 16 ms 12516 KB Output is correct
81 Correct 15 ms 12408 KB Output is correct
82 Correct 16 ms 12124 KB Output is correct
83 Correct 14 ms 12104 KB Output is correct
84 Correct 15 ms 12536 KB Output is correct
85 Correct 166 ms 26008 KB Output is correct
86 Correct 15 ms 12508 KB Output is correct
87 Correct 161 ms 25504 KB Output is correct
88 Correct 12 ms 12092 KB Output is correct
89 Correct 154 ms 22584 KB Output is correct
90 Correct 162 ms 25864 KB Output is correct
91 Correct 167 ms 26996 KB Output is correct
92 Correct 107 ms 26596 KB Output is correct
93 Correct 15 ms 12536 KB Output is correct
94 Correct 12 ms 12152 KB Output is correct
95 Correct 120 ms 27604 KB Output is correct
96 Correct 155 ms 27916 KB Output is correct
97 Correct 15 ms 12564 KB Output is correct
98 Correct 13 ms 12152 KB Output is correct
99 Correct 109 ms 27244 KB Output is correct
100 Correct 15 ms 12536 KB Output is correct
101 Correct 156 ms 23884 KB Output is correct
102 Correct 166 ms 28992 KB Output is correct
103 Correct 15 ms 12536 KB Output is correct
104 Correct 16 ms 12536 KB Output is correct
105 Correct 19 ms 12408 KB Output is correct
106 Correct 15 ms 12560 KB Output is correct
107 Correct 15 ms 12792 KB Output is correct
108 Correct 813 ms 80804 KB Output is correct
109 Correct 940 ms 76832 KB Output is correct
110 Correct 917 ms 90188 KB Output is correct
111 Correct 1008 ms 88528 KB Output is correct
112 Correct 1130 ms 89416 KB Output is correct
113 Correct 896 ms 82708 KB Output is correct
114 Correct 1006 ms 74116 KB Output is correct
115 Correct 1002 ms 74100 KB Output is correct
116 Correct 1047 ms 75584 KB Output is correct
117 Correct 1144 ms 80440 KB Output is correct
118 Correct 974 ms 73520 KB Output is correct
119 Correct 1098 ms 79824 KB Output is correct
120 Correct 1163 ms 91128 KB Output is correct
121 Correct 1126 ms 80020 KB Output is correct
122 Correct 1084 ms 77204 KB Output is correct
123 Correct 872 ms 83160 KB Output is correct
124 Correct 1043 ms 60172 KB Output is correct