답안 #128921

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
128921 2019-07-11T10:57:19 Z roseanne_pcy Mergers (JOI19_mergers) C++14
48 / 100
3000 ms 25788 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];
	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);
			return 0;
		}
	}
	printf("%d\n", (res+superbad+1)/2);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:61: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:64: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:69: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 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 4988 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 6 ms 4984 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 6 ms 4984 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 7 ms 5112 KB Output is correct
16 Correct 7 ms 4984 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 6 ms 5112 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 6 ms 5112 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 4984 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 4988 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 6 ms 4984 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 6 ms 4984 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 7 ms 5112 KB Output is correct
16 Correct 7 ms 4984 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 6 ms 5112 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 6 ms 5112 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 4984 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 4984 KB Output is correct
25 Correct 6 ms 4984 KB Output is correct
26 Correct 10 ms 5368 KB Output is correct
27 Correct 8 ms 5368 KB Output is correct
28 Correct 99 ms 5552 KB Output is correct
29 Correct 10 ms 5372 KB Output is correct
30 Correct 9 ms 5368 KB Output is correct
31 Correct 6 ms 4984 KB Output is correct
32 Correct 121 ms 5752 KB Output is correct
33 Correct 7 ms 5112 KB Output is correct
34 Correct 8 ms 5368 KB Output is correct
35 Correct 10 ms 5368 KB Output is correct
36 Correct 9 ms 5368 KB Output is correct
37 Correct 11 ms 5332 KB Output is correct
38 Correct 6 ms 4984 KB Output is correct
39 Correct 9 ms 5368 KB Output is correct
40 Correct 11 ms 5240 KB Output is correct
41 Correct 10 ms 5368 KB Output is correct
42 Correct 8 ms 5368 KB Output is correct
43 Correct 9 ms 5752 KB Output is correct
44 Correct 6 ms 5112 KB Output is correct
45 Correct 50 ms 5496 KB Output is correct
46 Correct 11 ms 5368 KB Output is correct
47 Correct 6 ms 4984 KB Output is correct
48 Correct 9 ms 5368 KB Output is correct
49 Correct 8 ms 5368 KB Output is correct
50 Correct 57 ms 5452 KB Output is correct
51 Correct 9 ms 5372 KB Output is correct
52 Correct 8 ms 5368 KB Output is correct
53 Correct 8 ms 5368 KB Output is correct
54 Correct 8 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 4988 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 6 ms 4984 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 6 ms 4984 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 7 ms 5112 KB Output is correct
16 Correct 7 ms 4984 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 6 ms 5112 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 6 ms 5112 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 4984 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 4984 KB Output is correct
25 Correct 6 ms 4984 KB Output is correct
26 Correct 96 ms 14220 KB Output is correct
27 Correct 137 ms 14808 KB Output is correct
28 Correct 8 ms 5368 KB Output is correct
29 Correct 6 ms 5112 KB Output is correct
30 Correct 6 ms 4984 KB Output is correct
31 Correct 123 ms 14328 KB Output is correct
32 Correct 9 ms 5368 KB Output is correct
33 Correct 210 ms 25336 KB Output is correct
34 Correct 138 ms 14768 KB Output is correct
35 Correct 8 ms 5368 KB Output is correct
36 Correct 180 ms 15388 KB Output is correct
37 Correct 8 ms 5368 KB Output is correct
38 Correct 8 ms 5368 KB Output is correct
39 Correct 93 ms 14056 KB Output is correct
40 Correct 8 ms 5752 KB Output is correct
41 Correct 119 ms 14200 KB Output is correct
42 Correct 158 ms 17760 KB Output is correct
43 Correct 6 ms 5112 KB Output is correct
44 Correct 143 ms 25788 KB Output is correct
45 Correct 168 ms 20084 KB Output is correct
46 Correct 8 ms 5344 KB Output is correct
47 Correct 8 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 14192 KB Output is correct
2 Correct 112 ms 14612 KB Output is correct
3 Correct 10 ms 5368 KB Output is correct
4 Correct 8 ms 5368 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 4984 KB Output is correct
7 Correct 8 ms 5368 KB Output is correct
8 Correct 384 ms 15576 KB Output is correct
9 Correct 8 ms 5368 KB Output is correct
10 Correct 127 ms 14328 KB Output is correct
11 Correct 6 ms 4984 KB Output is correct
12 Correct 140 ms 14044 KB Output is correct
13 Correct 250 ms 15452 KB Output is correct
14 Correct 235 ms 16976 KB Output is correct
15 Correct 137 ms 13932 KB Output is correct
16 Correct 10 ms 5368 KB Output is correct
17 Correct 3 ms 4984 KB Output is correct
18 Correct 125 ms 14448 KB Output is correct
19 Execution timed out 3054 ms 18424 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 4988 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 6 ms 4984 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 6 ms 4984 KB Output is correct
14 Correct 6 ms 4984 KB Output is correct
15 Correct 7 ms 5112 KB Output is correct
16 Correct 7 ms 4984 KB Output is correct
17 Correct 6 ms 4984 KB Output is correct
18 Correct 6 ms 5112 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 6 ms 5112 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 4984 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 4984 KB Output is correct
25 Correct 6 ms 4984 KB Output is correct
26 Correct 10 ms 5368 KB Output is correct
27 Correct 8 ms 5368 KB Output is correct
28 Correct 99 ms 5552 KB Output is correct
29 Correct 10 ms 5372 KB Output is correct
30 Correct 9 ms 5368 KB Output is correct
31 Correct 6 ms 4984 KB Output is correct
32 Correct 121 ms 5752 KB Output is correct
33 Correct 7 ms 5112 KB Output is correct
34 Correct 8 ms 5368 KB Output is correct
35 Correct 10 ms 5368 KB Output is correct
36 Correct 9 ms 5368 KB Output is correct
37 Correct 11 ms 5332 KB Output is correct
38 Correct 6 ms 4984 KB Output is correct
39 Correct 9 ms 5368 KB Output is correct
40 Correct 11 ms 5240 KB Output is correct
41 Correct 10 ms 5368 KB Output is correct
42 Correct 8 ms 5368 KB Output is correct
43 Correct 9 ms 5752 KB Output is correct
44 Correct 6 ms 5112 KB Output is correct
45 Correct 50 ms 5496 KB Output is correct
46 Correct 11 ms 5368 KB Output is correct
47 Correct 6 ms 4984 KB Output is correct
48 Correct 9 ms 5368 KB Output is correct
49 Correct 8 ms 5368 KB Output is correct
50 Correct 57 ms 5452 KB Output is correct
51 Correct 9 ms 5372 KB Output is correct
52 Correct 8 ms 5368 KB Output is correct
53 Correct 8 ms 5368 KB Output is correct
54 Correct 8 ms 5368 KB Output is correct
55 Correct 6 ms 4984 KB Output is correct
56 Correct 96 ms 14220 KB Output is correct
57 Correct 137 ms 14808 KB Output is correct
58 Correct 8 ms 5368 KB Output is correct
59 Correct 6 ms 5112 KB Output is correct
60 Correct 6 ms 4984 KB Output is correct
61 Correct 123 ms 14328 KB Output is correct
62 Correct 9 ms 5368 KB Output is correct
63 Correct 210 ms 25336 KB Output is correct
64 Correct 138 ms 14768 KB Output is correct
65 Correct 8 ms 5368 KB Output is correct
66 Correct 180 ms 15388 KB Output is correct
67 Correct 8 ms 5368 KB Output is correct
68 Correct 8 ms 5368 KB Output is correct
69 Correct 93 ms 14056 KB Output is correct
70 Correct 8 ms 5752 KB Output is correct
71 Correct 119 ms 14200 KB Output is correct
72 Correct 158 ms 17760 KB Output is correct
73 Correct 6 ms 5112 KB Output is correct
74 Correct 143 ms 25788 KB Output is correct
75 Correct 168 ms 20084 KB Output is correct
76 Correct 8 ms 5344 KB Output is correct
77 Correct 8 ms 5368 KB Output is correct
78 Correct 100 ms 14192 KB Output is correct
79 Correct 112 ms 14612 KB Output is correct
80 Correct 10 ms 5368 KB Output is correct
81 Correct 8 ms 5368 KB Output is correct
82 Correct 6 ms 5112 KB Output is correct
83 Correct 6 ms 4984 KB Output is correct
84 Correct 8 ms 5368 KB Output is correct
85 Correct 384 ms 15576 KB Output is correct
86 Correct 8 ms 5368 KB Output is correct
87 Correct 127 ms 14328 KB Output is correct
88 Correct 6 ms 4984 KB Output is correct
89 Correct 140 ms 14044 KB Output is correct
90 Correct 250 ms 15452 KB Output is correct
91 Correct 235 ms 16976 KB Output is correct
92 Correct 137 ms 13932 KB Output is correct
93 Correct 10 ms 5368 KB Output is correct
94 Correct 3 ms 4984 KB Output is correct
95 Correct 125 ms 14448 KB Output is correct
96 Execution timed out 3054 ms 18424 KB Time limit exceeded
97 Halted 0 ms 0 KB -