답안 #128906

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
128906 2019-07-11T10:52:41 Z roseanne_pcy Mergers (JOI19_mergers) C++14
48 / 100
3000 ms 25760 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 merge(vector<int> &A, vector<int> &B)
{
	vector<int> res;
	int n = A.size(), m = B.size();
	int i = 0, j = 0;
	while(i< n && j< m)
	{
		if(A[i]< B[j])
		{
			if(res.empty() || res.back() != A[i]) res.pb(A[i]);
			i++;
		}
		else
		{
			if(res.empty() || res.back() != B[j]) res.pb(B[j]);
			j++;
		}
	}
	while(i< n)
	{
		if(res.empty() || res.back() != A[i]) res.pb(A[i]);
		i++;
	}
	while(j< m)
	{
		if(res.empty() || res.back() != B[j]) res.pb(B[j]);
		j++;
	}
	A = res;
}
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;
			merge(bst[u], bst[v]);
		}
	}
	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]);
		}
	}
	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:89: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:92: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:97: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 4984 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 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 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 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 5112 KB Output is correct
23 Correct 6 ms 5112 KB Output is correct
24 Correct 6 ms 5084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 4984 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 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 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 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 5112 KB Output is correct
23 Correct 6 ms 5112 KB Output is correct
24 Correct 6 ms 5084 KB Output is correct
25 Correct 6 ms 5116 KB Output is correct
26 Correct 10 ms 5368 KB Output is correct
27 Correct 8 ms 5368 KB Output is correct
28 Correct 29 ms 5624 KB Output is correct
29 Correct 10 ms 5496 KB Output is correct
30 Correct 10 ms 5368 KB Output is correct
31 Correct 6 ms 5112 KB Output is correct
32 Correct 24 ms 5628 KB Output is correct
33 Correct 6 ms 5112 KB Output is correct
34 Correct 9 ms 5368 KB Output is correct
35 Correct 11 ms 5496 KB Output is correct
36 Correct 10 ms 5368 KB Output is correct
37 Correct 10 ms 5368 KB Output is correct
38 Correct 6 ms 5112 KB Output is correct
39 Correct 10 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 21 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 17 ms 5468 KB Output is correct
46 Correct 11 ms 5368 KB Output is correct
47 Correct 6 ms 4984 KB Output is correct
48 Correct 10 ms 5372 KB Output is correct
49 Correct 28 ms 5496 KB Output is correct
50 Correct 22 ms 5496 KB Output is correct
51 Correct 9 ms 5368 KB Output is correct
52 Correct 10 ms 5368 KB Output is correct
53 Correct 21 ms 5368 KB Output is correct
54 Correct 10 ms 5392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 4984 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 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 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 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 5112 KB Output is correct
23 Correct 6 ms 5112 KB Output is correct
24 Correct 6 ms 5084 KB Output is correct
25 Correct 6 ms 5112 KB Output is correct
26 Correct 172 ms 15324 KB Output is correct
27 Correct 158 ms 15864 KB Output is correct
28 Correct 9 ms 5368 KB Output is correct
29 Correct 6 ms 5112 KB Output is correct
30 Correct 7 ms 4984 KB Output is correct
31 Correct 123 ms 15608 KB Output is correct
32 Correct 9 ms 5368 KB Output is correct
33 Correct 209 ms 25436 KB Output is correct
34 Correct 161 ms 15748 KB Output is correct
35 Correct 8 ms 5368 KB Output is correct
36 Correct 144 ms 16504 KB Output is correct
37 Correct 8 ms 5368 KB Output is correct
38 Correct 8 ms 5368 KB Output is correct
39 Correct 153 ms 15276 KB Output is correct
40 Correct 8 ms 5624 KB Output is correct
41 Correct 127 ms 15352 KB Output is correct
42 Correct 180 ms 18772 KB Output is correct
43 Correct 6 ms 5112 KB Output is correct
44 Correct 156 ms 25760 KB Output is correct
45 Correct 208 ms 20728 KB Output is correct
46 Correct 10 ms 5368 KB Output is correct
47 Correct 9 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 13804 KB Output is correct
2 Execution timed out 3037 ms 14856 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 4984 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 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 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 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 5112 KB Output is correct
23 Correct 6 ms 5112 KB Output is correct
24 Correct 6 ms 5084 KB Output is correct
25 Correct 6 ms 5116 KB Output is correct
26 Correct 10 ms 5368 KB Output is correct
27 Correct 8 ms 5368 KB Output is correct
28 Correct 29 ms 5624 KB Output is correct
29 Correct 10 ms 5496 KB Output is correct
30 Correct 10 ms 5368 KB Output is correct
31 Correct 6 ms 5112 KB Output is correct
32 Correct 24 ms 5628 KB Output is correct
33 Correct 6 ms 5112 KB Output is correct
34 Correct 9 ms 5368 KB Output is correct
35 Correct 11 ms 5496 KB Output is correct
36 Correct 10 ms 5368 KB Output is correct
37 Correct 10 ms 5368 KB Output is correct
38 Correct 6 ms 5112 KB Output is correct
39 Correct 10 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 21 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 17 ms 5468 KB Output is correct
46 Correct 11 ms 5368 KB Output is correct
47 Correct 6 ms 4984 KB Output is correct
48 Correct 10 ms 5372 KB Output is correct
49 Correct 28 ms 5496 KB Output is correct
50 Correct 22 ms 5496 KB Output is correct
51 Correct 9 ms 5368 KB Output is correct
52 Correct 10 ms 5368 KB Output is correct
53 Correct 21 ms 5368 KB Output is correct
54 Correct 10 ms 5392 KB Output is correct
55 Correct 6 ms 5112 KB Output is correct
56 Correct 172 ms 15324 KB Output is correct
57 Correct 158 ms 15864 KB Output is correct
58 Correct 9 ms 5368 KB Output is correct
59 Correct 6 ms 5112 KB Output is correct
60 Correct 7 ms 4984 KB Output is correct
61 Correct 123 ms 15608 KB Output is correct
62 Correct 9 ms 5368 KB Output is correct
63 Correct 209 ms 25436 KB Output is correct
64 Correct 161 ms 15748 KB Output is correct
65 Correct 8 ms 5368 KB Output is correct
66 Correct 144 ms 16504 KB Output is correct
67 Correct 8 ms 5368 KB Output is correct
68 Correct 8 ms 5368 KB Output is correct
69 Correct 153 ms 15276 KB Output is correct
70 Correct 8 ms 5624 KB Output is correct
71 Correct 127 ms 15352 KB Output is correct
72 Correct 180 ms 18772 KB Output is correct
73 Correct 6 ms 5112 KB Output is correct
74 Correct 156 ms 25760 KB Output is correct
75 Correct 208 ms 20728 KB Output is correct
76 Correct 10 ms 5368 KB Output is correct
77 Correct 9 ms 5368 KB Output is correct
78 Correct 186 ms 13804 KB Output is correct
79 Execution timed out 3037 ms 14856 KB Time limit exceeded
80 Halted 0 ms 0 KB -