답안 #128912

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
128912 2019-07-11T10:54:03 Z roseanne_pcy Mergers (JOI19_mergers) C++14
48 / 100
3000 ms 25948 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 = 1e5+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];
 
void solve(int u = 1, int p = 0)
{
	cnt[u] = 1;
	ii big = {0, -1};
	bst[u].push_back(col[u]);
	tar[u] = 0;
	par[u] = p;
	for(int v : adj[u])
	{
		if(v == p) continue;
		solve(v, u);
		chil[u]++;
		big = max(big, {bst[v].size(), v});
		cnt[u] += cnt[v];
	}
	if(big.Y != -1)
	{
		int best = big.Y;
		swap(bst[u], bst[best]);
		for(int v : adj[u])
		{
			if(v == p) continue;
			for(int x : bst[v]) bst[u].push_back(x);
		}
		sort(bst[u].begin(), bst[u].end());
		bst[u].resize(unique(bst[u].begin(), bst[u].end())-bst[u].begin());
	}
	for(int x : bst[u]) tar[u] += colcnt[x];
	// printf("%d tar %d cnt %d\n", u, tar[u], cnt[u]);
	bad[u] = (tar[u] == cnt[u]);
}
 
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]]++;
	}
	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);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:62: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:65: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:70: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 7 ms 5112 KB Output is correct
2 Correct 6 ms 5096 KB Output is correct
3 Correct 6 ms 5116 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 7 ms 5084 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 8 ms 5112 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 4984 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 5112 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5096 KB Output is correct
3 Correct 6 ms 5116 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 7 ms 5084 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 8 ms 5112 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 4984 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 5112 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 5244 KB Output is correct
25 Correct 6 ms 5112 KB Output is correct
26 Correct 10 ms 5368 KB Output is correct
27 Correct 8 ms 5368 KB Output is correct
28 Correct 96 ms 5496 KB Output is correct
29 Correct 10 ms 5368 KB Output is correct
30 Correct 9 ms 5368 KB Output is correct
31 Correct 6 ms 5112 KB Output is correct
32 Correct 120 ms 5864 KB Output is correct
33 Correct 6 ms 5112 KB Output is correct
34 Correct 8 ms 5368 KB Output is correct
35 Correct 10 ms 5496 KB Output is correct
36 Correct 8 ms 5368 KB Output is correct
37 Correct 11 ms 5368 KB Output is correct
38 Correct 6 ms 5112 KB Output is correct
39 Correct 9 ms 5368 KB Output is correct
40 Correct 8 ms 5368 KB Output is correct
41 Correct 8 ms 5368 KB Output is correct
42 Correct 9 ms 5368 KB Output is correct
43 Correct 8 ms 5752 KB Output is correct
44 Correct 6 ms 5084 KB Output is correct
45 Correct 49 ms 5496 KB Output is correct
46 Correct 11 ms 5368 KB Output is correct
47 Correct 6 ms 5112 KB Output is correct
48 Correct 9 ms 5368 KB Output is correct
49 Correct 9 ms 5368 KB Output is correct
50 Correct 55 ms 5496 KB Output is correct
51 Correct 9 ms 5368 KB Output is correct
52 Correct 8 ms 5368 KB Output is correct
53 Correct 8 ms 5416 KB Output is correct
54 Correct 9 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5096 KB Output is correct
3 Correct 6 ms 5116 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 7 ms 5084 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 8 ms 5112 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 4984 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 5112 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 5244 KB Output is correct
25 Correct 6 ms 5112 KB Output is correct
26 Correct 95 ms 14320 KB Output is correct
27 Correct 137 ms 14928 KB Output is correct
28 Correct 8 ms 5368 KB Output is correct
29 Correct 6 ms 4984 KB Output is correct
30 Correct 6 ms 5112 KB Output is correct
31 Correct 131 ms 14504 KB Output is correct
32 Correct 8 ms 5368 KB Output is correct
33 Correct 202 ms 25464 KB Output is correct
34 Correct 162 ms 14860 KB Output is correct
35 Correct 9 ms 5340 KB Output is correct
36 Correct 125 ms 15452 KB Output is correct
37 Correct 8 ms 5368 KB Output is correct
38 Correct 8 ms 5368 KB Output is correct
39 Correct 105 ms 14060 KB Output is correct
40 Correct 8 ms 5752 KB Output is correct
41 Correct 123 ms 14328 KB Output is correct
42 Correct 167 ms 18040 KB Output is correct
43 Correct 6 ms 5112 KB Output is correct
44 Correct 142 ms 25948 KB Output is correct
45 Correct 170 ms 20216 KB Output is correct
46 Correct 8 ms 5368 KB Output is correct
47 Correct 9 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 14180 KB Output is correct
2 Correct 110 ms 14588 KB Output is correct
3 Correct 10 ms 5368 KB Output is correct
4 Correct 9 ms 5436 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 8 ms 5368 KB Output is correct
8 Correct 378 ms 17396 KB Output is correct
9 Correct 11 ms 5368 KB Output is correct
10 Correct 130 ms 15864 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 134 ms 15608 KB Output is correct
13 Correct 241 ms 17292 KB Output is correct
14 Correct 224 ms 18692 KB Output is correct
15 Correct 98 ms 15340 KB Output is correct
16 Correct 8 ms 5368 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 120 ms 16208 KB Output is correct
19 Execution timed out 3032 ms 20068 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5096 KB Output is correct
3 Correct 6 ms 5116 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 7 ms 5084 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 8 ms 5112 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 4984 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 5112 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 5244 KB Output is correct
25 Correct 6 ms 5112 KB Output is correct
26 Correct 10 ms 5368 KB Output is correct
27 Correct 8 ms 5368 KB Output is correct
28 Correct 96 ms 5496 KB Output is correct
29 Correct 10 ms 5368 KB Output is correct
30 Correct 9 ms 5368 KB Output is correct
31 Correct 6 ms 5112 KB Output is correct
32 Correct 120 ms 5864 KB Output is correct
33 Correct 6 ms 5112 KB Output is correct
34 Correct 8 ms 5368 KB Output is correct
35 Correct 10 ms 5496 KB Output is correct
36 Correct 8 ms 5368 KB Output is correct
37 Correct 11 ms 5368 KB Output is correct
38 Correct 6 ms 5112 KB Output is correct
39 Correct 9 ms 5368 KB Output is correct
40 Correct 8 ms 5368 KB Output is correct
41 Correct 8 ms 5368 KB Output is correct
42 Correct 9 ms 5368 KB Output is correct
43 Correct 8 ms 5752 KB Output is correct
44 Correct 6 ms 5084 KB Output is correct
45 Correct 49 ms 5496 KB Output is correct
46 Correct 11 ms 5368 KB Output is correct
47 Correct 6 ms 5112 KB Output is correct
48 Correct 9 ms 5368 KB Output is correct
49 Correct 9 ms 5368 KB Output is correct
50 Correct 55 ms 5496 KB Output is correct
51 Correct 9 ms 5368 KB Output is correct
52 Correct 8 ms 5368 KB Output is correct
53 Correct 8 ms 5416 KB Output is correct
54 Correct 9 ms 5368 KB Output is correct
55 Correct 6 ms 5112 KB Output is correct
56 Correct 95 ms 14320 KB Output is correct
57 Correct 137 ms 14928 KB Output is correct
58 Correct 8 ms 5368 KB Output is correct
59 Correct 6 ms 4984 KB Output is correct
60 Correct 6 ms 5112 KB Output is correct
61 Correct 131 ms 14504 KB Output is correct
62 Correct 8 ms 5368 KB Output is correct
63 Correct 202 ms 25464 KB Output is correct
64 Correct 162 ms 14860 KB Output is correct
65 Correct 9 ms 5340 KB Output is correct
66 Correct 125 ms 15452 KB Output is correct
67 Correct 8 ms 5368 KB Output is correct
68 Correct 8 ms 5368 KB Output is correct
69 Correct 105 ms 14060 KB Output is correct
70 Correct 8 ms 5752 KB Output is correct
71 Correct 123 ms 14328 KB Output is correct
72 Correct 167 ms 18040 KB Output is correct
73 Correct 6 ms 5112 KB Output is correct
74 Correct 142 ms 25948 KB Output is correct
75 Correct 170 ms 20216 KB Output is correct
76 Correct 8 ms 5368 KB Output is correct
77 Correct 9 ms 5368 KB Output is correct
78 Correct 99 ms 14180 KB Output is correct
79 Correct 110 ms 14588 KB Output is correct
80 Correct 10 ms 5368 KB Output is correct
81 Correct 9 ms 5436 KB Output is correct
82 Correct 6 ms 4984 KB Output is correct
83 Correct 6 ms 5112 KB Output is correct
84 Correct 8 ms 5368 KB Output is correct
85 Correct 378 ms 17396 KB Output is correct
86 Correct 11 ms 5368 KB Output is correct
87 Correct 130 ms 15864 KB Output is correct
88 Correct 6 ms 5112 KB Output is correct
89 Correct 134 ms 15608 KB Output is correct
90 Correct 241 ms 17292 KB Output is correct
91 Correct 224 ms 18692 KB Output is correct
92 Correct 98 ms 15340 KB Output is correct
93 Correct 8 ms 5368 KB Output is correct
94 Correct 6 ms 4984 KB Output is correct
95 Correct 120 ms 16208 KB Output is correct
96 Execution timed out 3032 ms 20068 KB Time limit exceeded
97 Halted 0 ms 0 KB -