답안 #486617

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486617 2021-11-12T06:44:13 Z cheissmart Mergers (JOI19_mergers) C++14
100 / 100
509 ms 52976 KB
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
	cerr << to_string(h);
	if(sizeof ...(t)) cerr << ", ";
	DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif

const int INF = 1e9 + 7, N = 5e5 + 7;

vi G[N];

int p[N], lst[N], d[N], parent[N], deg[N];

void dfs(int u, int pa = 0) {
	d[u] = d[pa] + 1;
	parent[u] = pa;
	for(int v:G[u]) if(v != pa)
		dfs(v, u);
}

signed main()
{
	IO_OP;
	
	int n, k;
	cin >> n >> k;
	for(int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		G[a].PB(b);
		G[b].PB(a);
	}
	
	dfs(1);
	
	iota(p + 1, p + n + 1, 1);

	function<int(int)> find = [&] (int u) {
		return p[u] == u ? u : p[u] = find(p[u]); 
	};

	auto cover = [&] (int u, int v) {
		while(find(u) != find(v)){
			if(d[p[u]] > d[p[v]]) swap(u, v);
			p[p[v]] = parent[p[v]];
		}
	};
	
	for(int i = 1; i <= n; i++) {
		int s;
		cin >> s;
		if(lst[s])
			cover(lst[s], i);
		lst[s] = i;
	}
	
	for(int i = 1; i <= n; i++)
		for(int j:G[i]) if(find(i) != find(j))
			deg[p[i]]++;
	
			
	int cnt = 0;
	for(int i = 1; i <= n; i++)
		if(deg[i] == 1)	
			cnt++;
	cout << (cnt + 1) / 2 << '\n';
}


# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12088 KB Output is correct
2 Correct 6 ms 12072 KB Output is correct
3 Correct 6 ms 12012 KB Output is correct
4 Correct 5 ms 12048 KB Output is correct
5 Correct 5 ms 11980 KB Output is correct
6 Correct 5 ms 11980 KB Output is correct
7 Correct 6 ms 11980 KB Output is correct
8 Correct 5 ms 12108 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 5 ms 12064 KB Output is correct
11 Correct 6 ms 12108 KB Output is correct
12 Correct 6 ms 11980 KB Output is correct
13 Correct 5 ms 12060 KB Output is correct
14 Correct 6 ms 11980 KB Output is correct
15 Correct 5 ms 11980 KB Output is correct
16 Correct 6 ms 12032 KB Output is correct
17 Correct 6 ms 12008 KB Output is correct
18 Correct 5 ms 12108 KB Output is correct
19 Correct 6 ms 11980 KB Output is correct
20 Correct 5 ms 11980 KB Output is correct
21 Correct 6 ms 11980 KB Output is correct
22 Correct 6 ms 11980 KB Output is correct
23 Correct 5 ms 11980 KB Output is correct
24 Correct 6 ms 11980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12088 KB Output is correct
2 Correct 6 ms 12072 KB Output is correct
3 Correct 6 ms 12012 KB Output is correct
4 Correct 5 ms 12048 KB Output is correct
5 Correct 5 ms 11980 KB Output is correct
6 Correct 5 ms 11980 KB Output is correct
7 Correct 6 ms 11980 KB Output is correct
8 Correct 5 ms 12108 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 5 ms 12064 KB Output is correct
11 Correct 6 ms 12108 KB Output is correct
12 Correct 6 ms 11980 KB Output is correct
13 Correct 5 ms 12060 KB Output is correct
14 Correct 6 ms 11980 KB Output is correct
15 Correct 5 ms 11980 KB Output is correct
16 Correct 6 ms 12032 KB Output is correct
17 Correct 6 ms 12008 KB Output is correct
18 Correct 5 ms 12108 KB Output is correct
19 Correct 6 ms 11980 KB Output is correct
20 Correct 5 ms 11980 KB Output is correct
21 Correct 6 ms 11980 KB Output is correct
22 Correct 6 ms 11980 KB Output is correct
23 Correct 5 ms 11980 KB Output is correct
24 Correct 6 ms 11980 KB Output is correct
25 Correct 5 ms 12064 KB Output is correct
26 Correct 7 ms 12252 KB Output is correct
27 Correct 7 ms 12236 KB Output is correct
28 Correct 7 ms 12336 KB Output is correct
29 Correct 7 ms 12196 KB Output is correct
30 Correct 7 ms 12212 KB Output is correct
31 Correct 6 ms 11980 KB Output is correct
32 Correct 7 ms 12328 KB Output is correct
33 Correct 7 ms 11980 KB Output is correct
34 Correct 7 ms 12192 KB Output is correct
35 Correct 7 ms 12236 KB Output is correct
36 Correct 7 ms 12236 KB Output is correct
37 Correct 7 ms 12204 KB Output is correct
38 Correct 6 ms 12108 KB Output is correct
39 Correct 7 ms 12176 KB Output is correct
40 Correct 7 ms 12236 KB Output is correct
41 Correct 7 ms 12236 KB Output is correct
42 Correct 7 ms 12236 KB Output is correct
43 Correct 7 ms 12332 KB Output is correct
44 Correct 6 ms 12056 KB Output is correct
45 Correct 7 ms 12236 KB Output is correct
46 Correct 7 ms 12188 KB Output is correct
47 Correct 6 ms 12108 KB Output is correct
48 Correct 7 ms 12236 KB Output is correct
49 Correct 6 ms 12236 KB Output is correct
50 Correct 7 ms 12236 KB Output is correct
51 Correct 7 ms 12236 KB Output is correct
52 Correct 7 ms 12236 KB Output is correct
53 Correct 8 ms 12236 KB Output is correct
54 Correct 9 ms 12236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12088 KB Output is correct
2 Correct 6 ms 12072 KB Output is correct
3 Correct 6 ms 12012 KB Output is correct
4 Correct 5 ms 12048 KB Output is correct
5 Correct 5 ms 11980 KB Output is correct
6 Correct 5 ms 11980 KB Output is correct
7 Correct 6 ms 11980 KB Output is correct
8 Correct 5 ms 12108 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 5 ms 12064 KB Output is correct
11 Correct 6 ms 12108 KB Output is correct
12 Correct 6 ms 11980 KB Output is correct
13 Correct 5 ms 12060 KB Output is correct
14 Correct 6 ms 11980 KB Output is correct
15 Correct 5 ms 11980 KB Output is correct
16 Correct 6 ms 12032 KB Output is correct
17 Correct 6 ms 12008 KB Output is correct
18 Correct 5 ms 12108 KB Output is correct
19 Correct 6 ms 11980 KB Output is correct
20 Correct 5 ms 11980 KB Output is correct
21 Correct 6 ms 11980 KB Output is correct
22 Correct 6 ms 11980 KB Output is correct
23 Correct 5 ms 11980 KB Output is correct
24 Correct 6 ms 11980 KB Output is correct
25 Correct 6 ms 11980 KB Output is correct
26 Correct 43 ms 18128 KB Output is correct
27 Correct 55 ms 17856 KB Output is correct
28 Correct 7 ms 12236 KB Output is correct
29 Correct 6 ms 11980 KB Output is correct
30 Correct 6 ms 11988 KB Output is correct
31 Correct 56 ms 18000 KB Output is correct
32 Correct 6 ms 12236 KB Output is correct
33 Correct 61 ms 22176 KB Output is correct
34 Correct 58 ms 17860 KB Output is correct
35 Correct 7 ms 12236 KB Output is correct
36 Correct 61 ms 18444 KB Output is correct
37 Correct 7 ms 12236 KB Output is correct
38 Correct 7 ms 12236 KB Output is correct
39 Correct 43 ms 18072 KB Output is correct
40 Correct 7 ms 12364 KB Output is correct
41 Correct 58 ms 17840 KB Output is correct
42 Correct 60 ms 19268 KB Output is correct
43 Correct 6 ms 12108 KB Output is correct
44 Correct 73 ms 22176 KB Output is correct
45 Correct 69 ms 20032 KB Output is correct
46 Correct 7 ms 12236 KB Output is correct
47 Correct 7 ms 12236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 18172 KB Output is correct
2 Correct 42 ms 19096 KB Output is correct
3 Correct 9 ms 12260 KB Output is correct
4 Correct 8 ms 12208 KB Output is correct
5 Correct 8 ms 12064 KB Output is correct
6 Correct 6 ms 11980 KB Output is correct
7 Correct 9 ms 12180 KB Output is correct
8 Correct 72 ms 18608 KB Output is correct
9 Correct 9 ms 12236 KB Output is correct
10 Correct 68 ms 18396 KB Output is correct
11 Correct 6 ms 12108 KB Output is correct
12 Correct 62 ms 18336 KB Output is correct
13 Correct 60 ms 18632 KB Output is correct
14 Correct 56 ms 18864 KB Output is correct
15 Correct 47 ms 18096 KB Output is correct
16 Correct 7 ms 12236 KB Output is correct
17 Correct 6 ms 12108 KB Output is correct
18 Correct 47 ms 19196 KB Output is correct
19 Correct 53 ms 21120 KB Output is correct
20 Correct 7 ms 12236 KB Output is correct
21 Correct 6 ms 12108 KB Output is correct
22 Correct 47 ms 18996 KB Output is correct
23 Correct 7 ms 12236 KB Output is correct
24 Correct 68 ms 18428 KB Output is correct
25 Correct 51 ms 20388 KB Output is correct
26 Correct 7 ms 12264 KB Output is correct
27 Correct 7 ms 12224 KB Output is correct
28 Correct 7 ms 12236 KB Output is correct
29 Correct 7 ms 12264 KB Output is correct
30 Correct 7 ms 12204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12088 KB Output is correct
2 Correct 6 ms 12072 KB Output is correct
3 Correct 6 ms 12012 KB Output is correct
4 Correct 5 ms 12048 KB Output is correct
5 Correct 5 ms 11980 KB Output is correct
6 Correct 5 ms 11980 KB Output is correct
7 Correct 6 ms 11980 KB Output is correct
8 Correct 5 ms 12108 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 5 ms 12064 KB Output is correct
11 Correct 6 ms 12108 KB Output is correct
12 Correct 6 ms 11980 KB Output is correct
13 Correct 5 ms 12060 KB Output is correct
14 Correct 6 ms 11980 KB Output is correct
15 Correct 5 ms 11980 KB Output is correct
16 Correct 6 ms 12032 KB Output is correct
17 Correct 6 ms 12008 KB Output is correct
18 Correct 5 ms 12108 KB Output is correct
19 Correct 6 ms 11980 KB Output is correct
20 Correct 5 ms 11980 KB Output is correct
21 Correct 6 ms 11980 KB Output is correct
22 Correct 6 ms 11980 KB Output is correct
23 Correct 5 ms 11980 KB Output is correct
24 Correct 6 ms 11980 KB Output is correct
25 Correct 5 ms 12064 KB Output is correct
26 Correct 7 ms 12252 KB Output is correct
27 Correct 7 ms 12236 KB Output is correct
28 Correct 7 ms 12336 KB Output is correct
29 Correct 7 ms 12196 KB Output is correct
30 Correct 7 ms 12212 KB Output is correct
31 Correct 6 ms 11980 KB Output is correct
32 Correct 7 ms 12328 KB Output is correct
33 Correct 7 ms 11980 KB Output is correct
34 Correct 7 ms 12192 KB Output is correct
35 Correct 7 ms 12236 KB Output is correct
36 Correct 7 ms 12236 KB Output is correct
37 Correct 7 ms 12204 KB Output is correct
38 Correct 6 ms 12108 KB Output is correct
39 Correct 7 ms 12176 KB Output is correct
40 Correct 7 ms 12236 KB Output is correct
41 Correct 7 ms 12236 KB Output is correct
42 Correct 7 ms 12236 KB Output is correct
43 Correct 7 ms 12332 KB Output is correct
44 Correct 6 ms 12056 KB Output is correct
45 Correct 7 ms 12236 KB Output is correct
46 Correct 7 ms 12188 KB Output is correct
47 Correct 6 ms 12108 KB Output is correct
48 Correct 7 ms 12236 KB Output is correct
49 Correct 6 ms 12236 KB Output is correct
50 Correct 7 ms 12236 KB Output is correct
51 Correct 7 ms 12236 KB Output is correct
52 Correct 7 ms 12236 KB Output is correct
53 Correct 8 ms 12236 KB Output is correct
54 Correct 9 ms 12236 KB Output is correct
55 Correct 6 ms 11980 KB Output is correct
56 Correct 43 ms 18128 KB Output is correct
57 Correct 55 ms 17856 KB Output is correct
58 Correct 7 ms 12236 KB Output is correct
59 Correct 6 ms 11980 KB Output is correct
60 Correct 6 ms 11988 KB Output is correct
61 Correct 56 ms 18000 KB Output is correct
62 Correct 6 ms 12236 KB Output is correct
63 Correct 61 ms 22176 KB Output is correct
64 Correct 58 ms 17860 KB Output is correct
65 Correct 7 ms 12236 KB Output is correct
66 Correct 61 ms 18444 KB Output is correct
67 Correct 7 ms 12236 KB Output is correct
68 Correct 7 ms 12236 KB Output is correct
69 Correct 43 ms 18072 KB Output is correct
70 Correct 7 ms 12364 KB Output is correct
71 Correct 58 ms 17840 KB Output is correct
72 Correct 60 ms 19268 KB Output is correct
73 Correct 6 ms 12108 KB Output is correct
74 Correct 73 ms 22176 KB Output is correct
75 Correct 69 ms 20032 KB Output is correct
76 Correct 7 ms 12236 KB Output is correct
77 Correct 7 ms 12236 KB Output is correct
78 Correct 47 ms 18172 KB Output is correct
79 Correct 42 ms 19096 KB Output is correct
80 Correct 9 ms 12260 KB Output is correct
81 Correct 8 ms 12208 KB Output is correct
82 Correct 8 ms 12064 KB Output is correct
83 Correct 6 ms 11980 KB Output is correct
84 Correct 9 ms 12180 KB Output is correct
85 Correct 72 ms 18608 KB Output is correct
86 Correct 9 ms 12236 KB Output is correct
87 Correct 68 ms 18396 KB Output is correct
88 Correct 6 ms 12108 KB Output is correct
89 Correct 62 ms 18336 KB Output is correct
90 Correct 60 ms 18632 KB Output is correct
91 Correct 56 ms 18864 KB Output is correct
92 Correct 47 ms 18096 KB Output is correct
93 Correct 7 ms 12236 KB Output is correct
94 Correct 6 ms 12108 KB Output is correct
95 Correct 47 ms 19196 KB Output is correct
96 Correct 53 ms 21120 KB Output is correct
97 Correct 7 ms 12236 KB Output is correct
98 Correct 6 ms 12108 KB Output is correct
99 Correct 47 ms 18996 KB Output is correct
100 Correct 7 ms 12236 KB Output is correct
101 Correct 68 ms 18428 KB Output is correct
102 Correct 51 ms 20388 KB Output is correct
103 Correct 7 ms 12264 KB Output is correct
104 Correct 7 ms 12224 KB Output is correct
105 Correct 7 ms 12236 KB Output is correct
106 Correct 7 ms 12264 KB Output is correct
107 Correct 7 ms 12204 KB Output is correct
108 Correct 303 ms 39688 KB Output is correct
109 Correct 507 ms 46276 KB Output is correct
110 Correct 509 ms 52756 KB Output is correct
111 Correct 475 ms 52976 KB Output is correct
112 Correct 483 ms 46976 KB Output is correct
113 Correct 309 ms 40456 KB Output is correct
114 Correct 429 ms 35432 KB Output is correct
115 Correct 442 ms 35396 KB Output is correct
116 Correct 459 ms 37444 KB Output is correct
117 Correct 400 ms 39192 KB Output is correct
118 Correct 439 ms 36548 KB Output is correct
119 Correct 406 ms 39208 KB Output is correct
120 Correct 427 ms 47188 KB Output is correct
121 Correct 393 ms 39048 KB Output is correct
122 Correct 466 ms 38272 KB Output is correct
123 Correct 249 ms 40240 KB Output is correct
124 Correct 456 ms 38100 KB Output is correct