답안 #102728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
102728 2019-03-27T09:29:27 Z psmao Mergers (JOI19_mergers) C++14
100 / 100
1187 ms 87288 KB
#include <bits/stdc++.h>
using namespace std;

#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;

void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}

const int maxn = 500050;

int n, K, fa[maxn], par[maxn], dep[maxn], deg[maxn];
VI adj[maxn], col[maxn];

int getfa(int x) {return fa[x] == x ? x : fa[x] = getfa(fa[x]);}
void merge(int u, int v) {u = getfa(u); v = getfa(v); fa[u] = v;}
void combine(int u, int v)
{
	while(u != v)
	{
		if(dep[u] > dep[v])
		{
			merge(u, par[u]);
			u = getfa(par[u]);
		}
		else
		{
			merge(v, par[v]);
			v = getfa(par[v]);
		}
	}
}
void dfs(int u, int f = 0)
{
	for(auto p : adj[u]) if(p != f)
	{
		par[p] = u;
		dep[p] = dep[u] + 1;
		dfs(p, u);
	}
}
int main()
{
	#ifdef MPS	
		fp("1.in","r",stdin);
		fp("1.out","w",stdout);
	#endif
	sf("%d%d",&n,&K);
	fo(i,2,n)
	{
		int u, v; sf("%d%d",&u,&v);
		adj[u].pb(v); adj[v].pb(u);
	}	
	fo(i,1,n) fa[i] = i;
	fo(i,1,n) {int x; sf("%d",&x); col[x].pb(i);}
	dfs(1);
	fo(i,1,K) fo(j,1,SZ(col[i])-1) combine(col[i][j], col[i][0]);
	fo(i,1,n) for(auto p : adj[i]) 
		if(getfa(i) != getfa(p) && dep[i] + 1 == dep[p]) 
		{
			++ deg[getfa(i)]; ++ deg[getfa(p)];
		}
	int leaves = 0;
	fo(i,1,n) if(getfa(i) == i && deg[i] == 1) ++ leaves;
	pf("%d\n",(int)ceil(leaves/2.0));
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:71:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d%d",&n,&K);
    ^
mergers.cpp:74:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; sf("%d%d",&u,&v);
               ^
mergers.cpp:78:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  fo(i,1,n) {int x; sf("%d",&x); col[x].pb(i);}
                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 23808 KB Output is correct
2 Correct 28 ms 23896 KB Output is correct
3 Correct 27 ms 23808 KB Output is correct
4 Correct 30 ms 23780 KB Output is correct
5 Correct 29 ms 23808 KB Output is correct
6 Correct 28 ms 23856 KB Output is correct
7 Correct 24 ms 23808 KB Output is correct
8 Correct 24 ms 23808 KB Output is correct
9 Correct 24 ms 23808 KB Output is correct
10 Correct 25 ms 23808 KB Output is correct
11 Correct 24 ms 23808 KB Output is correct
12 Correct 29 ms 23872 KB Output is correct
13 Correct 27 ms 23804 KB Output is correct
14 Correct 26 ms 23808 KB Output is correct
15 Correct 25 ms 23832 KB Output is correct
16 Correct 24 ms 23808 KB Output is correct
17 Correct 24 ms 23808 KB Output is correct
18 Correct 26 ms 23808 KB Output is correct
19 Correct 26 ms 23808 KB Output is correct
20 Correct 28 ms 23808 KB Output is correct
21 Correct 24 ms 23780 KB Output is correct
22 Correct 27 ms 23832 KB Output is correct
23 Correct 24 ms 23808 KB Output is correct
24 Correct 25 ms 23776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 23808 KB Output is correct
2 Correct 28 ms 23896 KB Output is correct
3 Correct 27 ms 23808 KB Output is correct
4 Correct 30 ms 23780 KB Output is correct
5 Correct 29 ms 23808 KB Output is correct
6 Correct 28 ms 23856 KB Output is correct
7 Correct 24 ms 23808 KB Output is correct
8 Correct 24 ms 23808 KB Output is correct
9 Correct 24 ms 23808 KB Output is correct
10 Correct 25 ms 23808 KB Output is correct
11 Correct 24 ms 23808 KB Output is correct
12 Correct 29 ms 23872 KB Output is correct
13 Correct 27 ms 23804 KB Output is correct
14 Correct 26 ms 23808 KB Output is correct
15 Correct 25 ms 23832 KB Output is correct
16 Correct 24 ms 23808 KB Output is correct
17 Correct 24 ms 23808 KB Output is correct
18 Correct 26 ms 23808 KB Output is correct
19 Correct 26 ms 23808 KB Output is correct
20 Correct 28 ms 23808 KB Output is correct
21 Correct 24 ms 23780 KB Output is correct
22 Correct 27 ms 23832 KB Output is correct
23 Correct 24 ms 23808 KB Output is correct
24 Correct 25 ms 23776 KB Output is correct
25 Correct 27 ms 24064 KB Output is correct
26 Correct 27 ms 24060 KB Output is correct
27 Correct 32 ms 24056 KB Output is correct
28 Correct 25 ms 24192 KB Output is correct
29 Correct 27 ms 24056 KB Output is correct
30 Correct 26 ms 24064 KB Output is correct
31 Correct 24 ms 23808 KB Output is correct
32 Correct 28 ms 24184 KB Output is correct
33 Correct 25 ms 23800 KB Output is correct
34 Correct 27 ms 24064 KB Output is correct
35 Correct 27 ms 24064 KB Output is correct
36 Correct 25 ms 24064 KB Output is correct
37 Correct 25 ms 24064 KB Output is correct
38 Correct 24 ms 23808 KB Output is correct
39 Correct 27 ms 24064 KB Output is correct
40 Correct 28 ms 24064 KB Output is correct
41 Correct 25 ms 24056 KB Output is correct
42 Correct 25 ms 24064 KB Output is correct
43 Correct 27 ms 24192 KB Output is correct
44 Correct 23 ms 23808 KB Output is correct
45 Correct 30 ms 24056 KB Output is correct
46 Correct 27 ms 24040 KB Output is correct
47 Correct 27 ms 23808 KB Output is correct
48 Correct 28 ms 24064 KB Output is correct
49 Correct 26 ms 24184 KB Output is correct
50 Correct 26 ms 24192 KB Output is correct
51 Correct 26 ms 24064 KB Output is correct
52 Correct 26 ms 24064 KB Output is correct
53 Correct 23 ms 24064 KB Output is correct
54 Correct 26 ms 24060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 23808 KB Output is correct
2 Correct 28 ms 23896 KB Output is correct
3 Correct 27 ms 23808 KB Output is correct
4 Correct 30 ms 23780 KB Output is correct
5 Correct 29 ms 23808 KB Output is correct
6 Correct 28 ms 23856 KB Output is correct
7 Correct 24 ms 23808 KB Output is correct
8 Correct 24 ms 23808 KB Output is correct
9 Correct 24 ms 23808 KB Output is correct
10 Correct 25 ms 23808 KB Output is correct
11 Correct 24 ms 23808 KB Output is correct
12 Correct 29 ms 23872 KB Output is correct
13 Correct 27 ms 23804 KB Output is correct
14 Correct 26 ms 23808 KB Output is correct
15 Correct 25 ms 23832 KB Output is correct
16 Correct 24 ms 23808 KB Output is correct
17 Correct 24 ms 23808 KB Output is correct
18 Correct 26 ms 23808 KB Output is correct
19 Correct 26 ms 23808 KB Output is correct
20 Correct 28 ms 23808 KB Output is correct
21 Correct 24 ms 23780 KB Output is correct
22 Correct 27 ms 23832 KB Output is correct
23 Correct 24 ms 23808 KB Output is correct
24 Correct 25 ms 23776 KB Output is correct
25 Correct 22 ms 23808 KB Output is correct
26 Correct 101 ms 30572 KB Output is correct
27 Correct 155 ms 30204 KB Output is correct
28 Correct 24 ms 24064 KB Output is correct
29 Correct 29 ms 23800 KB Output is correct
30 Correct 25 ms 23808 KB Output is correct
31 Correct 123 ms 30204 KB Output is correct
32 Correct 30 ms 24056 KB Output is correct
33 Correct 150 ms 34424 KB Output is correct
34 Correct 126 ms 30328 KB Output is correct
35 Correct 25 ms 24064 KB Output is correct
36 Correct 141 ms 30664 KB Output is correct
37 Correct 26 ms 24064 KB Output is correct
38 Correct 28 ms 24084 KB Output is correct
39 Correct 99 ms 30476 KB Output is correct
40 Correct 27 ms 24192 KB Output is correct
41 Correct 121 ms 30196 KB Output is correct
42 Correct 148 ms 31480 KB Output is correct
43 Correct 23 ms 23808 KB Output is correct
44 Correct 146 ms 34588 KB Output is correct
45 Correct 135 ms 32376 KB Output is correct
46 Correct 26 ms 24064 KB Output is correct
47 Correct 25 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 30528 KB Output is correct
2 Correct 120 ms 33888 KB Output is correct
3 Correct 28 ms 24184 KB Output is correct
4 Correct 26 ms 24056 KB Output is correct
5 Correct 24 ms 23808 KB Output is correct
6 Correct 24 ms 23808 KB Output is correct
7 Correct 28 ms 24036 KB Output is correct
8 Correct 143 ms 31864 KB Output is correct
9 Correct 27 ms 24056 KB Output is correct
10 Correct 140 ms 30840 KB Output is correct
11 Correct 27 ms 23808 KB Output is correct
12 Correct 176 ms 30688 KB Output is correct
13 Correct 207 ms 31840 KB Output is correct
14 Correct 155 ms 33692 KB Output is correct
15 Correct 103 ms 30448 KB Output is correct
16 Correct 33 ms 24064 KB Output is correct
17 Correct 26 ms 23928 KB Output is correct
18 Correct 120 ms 33520 KB Output is correct
19 Correct 159 ms 35932 KB Output is correct
20 Correct 25 ms 24064 KB Output is correct
21 Correct 25 ms 23808 KB Output is correct
22 Correct 108 ms 32160 KB Output is correct
23 Correct 24 ms 24064 KB Output is correct
24 Correct 161 ms 31240 KB Output is correct
25 Correct 159 ms 34960 KB Output is correct
26 Correct 30 ms 24172 KB Output is correct
27 Correct 26 ms 24184 KB Output is correct
28 Correct 29 ms 24060 KB Output is correct
29 Correct 26 ms 24056 KB Output is correct
30 Correct 26 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 23808 KB Output is correct
2 Correct 28 ms 23896 KB Output is correct
3 Correct 27 ms 23808 KB Output is correct
4 Correct 30 ms 23780 KB Output is correct
5 Correct 29 ms 23808 KB Output is correct
6 Correct 28 ms 23856 KB Output is correct
7 Correct 24 ms 23808 KB Output is correct
8 Correct 24 ms 23808 KB Output is correct
9 Correct 24 ms 23808 KB Output is correct
10 Correct 25 ms 23808 KB Output is correct
11 Correct 24 ms 23808 KB Output is correct
12 Correct 29 ms 23872 KB Output is correct
13 Correct 27 ms 23804 KB Output is correct
14 Correct 26 ms 23808 KB Output is correct
15 Correct 25 ms 23832 KB Output is correct
16 Correct 24 ms 23808 KB Output is correct
17 Correct 24 ms 23808 KB Output is correct
18 Correct 26 ms 23808 KB Output is correct
19 Correct 26 ms 23808 KB Output is correct
20 Correct 28 ms 23808 KB Output is correct
21 Correct 24 ms 23780 KB Output is correct
22 Correct 27 ms 23832 KB Output is correct
23 Correct 24 ms 23808 KB Output is correct
24 Correct 25 ms 23776 KB Output is correct
25 Correct 27 ms 24064 KB Output is correct
26 Correct 27 ms 24060 KB Output is correct
27 Correct 32 ms 24056 KB Output is correct
28 Correct 25 ms 24192 KB Output is correct
29 Correct 27 ms 24056 KB Output is correct
30 Correct 26 ms 24064 KB Output is correct
31 Correct 24 ms 23808 KB Output is correct
32 Correct 28 ms 24184 KB Output is correct
33 Correct 25 ms 23800 KB Output is correct
34 Correct 27 ms 24064 KB Output is correct
35 Correct 27 ms 24064 KB Output is correct
36 Correct 25 ms 24064 KB Output is correct
37 Correct 25 ms 24064 KB Output is correct
38 Correct 24 ms 23808 KB Output is correct
39 Correct 27 ms 24064 KB Output is correct
40 Correct 28 ms 24064 KB Output is correct
41 Correct 25 ms 24056 KB Output is correct
42 Correct 25 ms 24064 KB Output is correct
43 Correct 27 ms 24192 KB Output is correct
44 Correct 23 ms 23808 KB Output is correct
45 Correct 30 ms 24056 KB Output is correct
46 Correct 27 ms 24040 KB Output is correct
47 Correct 27 ms 23808 KB Output is correct
48 Correct 28 ms 24064 KB Output is correct
49 Correct 26 ms 24184 KB Output is correct
50 Correct 26 ms 24192 KB Output is correct
51 Correct 26 ms 24064 KB Output is correct
52 Correct 26 ms 24064 KB Output is correct
53 Correct 23 ms 24064 KB Output is correct
54 Correct 26 ms 24060 KB Output is correct
55 Correct 22 ms 23808 KB Output is correct
56 Correct 101 ms 30572 KB Output is correct
57 Correct 155 ms 30204 KB Output is correct
58 Correct 24 ms 24064 KB Output is correct
59 Correct 29 ms 23800 KB Output is correct
60 Correct 25 ms 23808 KB Output is correct
61 Correct 123 ms 30204 KB Output is correct
62 Correct 30 ms 24056 KB Output is correct
63 Correct 150 ms 34424 KB Output is correct
64 Correct 126 ms 30328 KB Output is correct
65 Correct 25 ms 24064 KB Output is correct
66 Correct 141 ms 30664 KB Output is correct
67 Correct 26 ms 24064 KB Output is correct
68 Correct 28 ms 24084 KB Output is correct
69 Correct 99 ms 30476 KB Output is correct
70 Correct 27 ms 24192 KB Output is correct
71 Correct 121 ms 30196 KB Output is correct
72 Correct 148 ms 31480 KB Output is correct
73 Correct 23 ms 23808 KB Output is correct
74 Correct 146 ms 34588 KB Output is correct
75 Correct 135 ms 32376 KB Output is correct
76 Correct 26 ms 24064 KB Output is correct
77 Correct 25 ms 24064 KB Output is correct
78 Correct 92 ms 30528 KB Output is correct
79 Correct 120 ms 33888 KB Output is correct
80 Correct 28 ms 24184 KB Output is correct
81 Correct 26 ms 24056 KB Output is correct
82 Correct 24 ms 23808 KB Output is correct
83 Correct 24 ms 23808 KB Output is correct
84 Correct 28 ms 24036 KB Output is correct
85 Correct 143 ms 31864 KB Output is correct
86 Correct 27 ms 24056 KB Output is correct
87 Correct 140 ms 30840 KB Output is correct
88 Correct 27 ms 23808 KB Output is correct
89 Correct 176 ms 30688 KB Output is correct
90 Correct 207 ms 31840 KB Output is correct
91 Correct 155 ms 33692 KB Output is correct
92 Correct 103 ms 30448 KB Output is correct
93 Correct 33 ms 24064 KB Output is correct
94 Correct 26 ms 23928 KB Output is correct
95 Correct 120 ms 33520 KB Output is correct
96 Correct 159 ms 35932 KB Output is correct
97 Correct 25 ms 24064 KB Output is correct
98 Correct 25 ms 23808 KB Output is correct
99 Correct 108 ms 32160 KB Output is correct
100 Correct 24 ms 24064 KB Output is correct
101 Correct 161 ms 31240 KB Output is correct
102 Correct 159 ms 34960 KB Output is correct
103 Correct 30 ms 24172 KB Output is correct
104 Correct 26 ms 24184 KB Output is correct
105 Correct 29 ms 24060 KB Output is correct
106 Correct 26 ms 24056 KB Output is correct
107 Correct 26 ms 24064 KB Output is correct
108 Correct 742 ms 65736 KB Output is correct
109 Correct 1000 ms 66356 KB Output is correct
110 Correct 958 ms 72932 KB Output is correct
111 Correct 1116 ms 87288 KB Output is correct
112 Correct 1074 ms 81272 KB Output is correct
113 Correct 708 ms 73436 KB Output is correct
114 Correct 772 ms 55780 KB Output is correct
115 Correct 837 ms 55800 KB Output is correct
116 Correct 1069 ms 62132 KB Output is correct
117 Correct 968 ms 73592 KB Output is correct
118 Correct 954 ms 58576 KB Output is correct
119 Correct 1010 ms 73692 KB Output is correct
120 Correct 1094 ms 81796 KB Output is correct
121 Correct 1187 ms 73848 KB Output is correct
122 Correct 1158 ms 67292 KB Output is correct
123 Correct 731 ms 74984 KB Output is correct
124 Correct 1103 ms 72264 KB Output is correct