답안 #156260

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
156260 2019-10-04T16:37:39 Z popovicirobert Mergers (JOI19_mergers) C++14
100 / 100
1400 ms 128212 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lsb(x) (x & (-x)) 
    
using namespace std;

const int MAXN = (int) 5e5;
    
struct DSU {
	vector <int> par;
	int n;

	inline void init(int _n) {
		n = _n;
		par.resize(n + 1);
	}

	int root(int x) {
		if(par[x] == 0) return x;
		return par[x] = root(par[x]);
	}

	inline void join(int x, int y) {
		x = root(x), y = root(y);
		if(x != y) {
			par[x] = y;
		}
	}
};

vector <int> g[MAXN + 1];
int idl[MAXN + 1], idr[MAXN + 1], sz;
int anc[MAXN + 1][20], lvl[MAXN + 1];

void dfs(int nod) {
	idl[nod] = ++sz;
	for(auto it : g[nod]) {
		if(lvl[it] == 0) {
			anc[it][0] = nod;
			lvl[it] = lvl[nod] + 1;
			dfs(it);
		}
	}
	idr[nod] = sz;
}
   
int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n, k;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
		
	cin >> n >> k;

	for(i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	vector < vector <int> > col(k + 1);
	for(i = 1; i <= n; i++) {
		int c;
		cin >> c;
		col[c].push_back(i);
	}

	lvl[1] = 1;
	dfs(1);

	for(int bit = 1; bit < 20; bit++) {
		for(i = 1; i <= n; i++) {
			anc[i][bit] = anc[anc[i][bit - 1]][bit - 1];
		}
	}

	auto get_lca = [&](int x, int y) {
		if(lvl[x] < lvl[y]) {
			swap(x, y);
		}
		int dst = lvl[x] - lvl[y];
		for(int bit = 19; bit >= 0; bit--) {
			if(dst & (1 << bit)) {
				x = anc[x][bit];
			}
		}
		if(x == y) return x;
		for(int bit = 19; bit >= 0; bit--) {
			if(anc[x][bit] != anc[y][bit]) {
				x = anc[x][bit], y = anc[y][bit];
			}
		}
		return anc[x][0];
	};

	auto in = [&](int x, int y) -> bool {
		return idl[x] <= idl[y] && idr[y] <= idr[x];
	};

	DSU dsu; dsu.init(n);

	for(int c = 1; c <= k; c++) {
		sort(col[c].begin(), col[c].end(), [&](const int &x, const int &y) {
				return idl[x] < idl[y];
			});

		vector <int> nodes = col[c];
		for(i = 0; i + 1 < col[c].size(); i++) {
			nodes.push_back(get_lca(nodes[i], nodes[i + 1]));
		}
		
		sort(nodes.begin(), nodes.end(), [&](const int &x, const int &y) {
				return idl[x] < idl[y];
			});

		/*for(auto it : nodes) {
			cerr << it << " ";
		}
		cerr << "\n";*/

		stack <int> stk;	
		for(auto nod : nodes) {
			while(stk.size() && in(stk.top(), nod) == 0) {
				stk.pop();
			}
			if(stk.size()) {
				int cur = nod;
				while(lvl[cur] > lvl[stk.top()]) {
					dsu.join(cur, anc[cur][0]);
					//cerr << cur << " " << anc[cur][0] << "\n";
					cur = dsu.root(cur);
				}
			}
			stk.push(nod);
		}
	}

	vector <int> deg(n + 1);

	for(i = 1; i <= n; i++) {
		int x = dsu.root(i);
		//cerr << x << " ";
		for(auto it : g[i]) {
			int y = dsu.root(it);
			if(x != y) {
				deg[x]++;
			}
		}
	}

	int ans = 0;
	for(i = 1; i <= n; i++) {
		if(dsu.root(i) == i && deg[i] == 1) {
			ans++;
		}
	}

	cout << (ans + 1) / 2;
            
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:111:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i = 0; i + 1 < col[c].size(); i++) {
              ~~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12152 KB Output is correct
4 Correct 13 ms 12156 KB Output is correct
5 Correct 13 ms 12128 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12028 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 13 ms 12152 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 13 ms 12024 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 13 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 13 ms 12028 KB Output is correct
20 Correct 19 ms 12156 KB Output is correct
21 Correct 15 ms 12124 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12152 KB Output is correct
4 Correct 13 ms 12156 KB Output is correct
5 Correct 13 ms 12128 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12028 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 13 ms 12152 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 13 ms 12024 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 13 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 13 ms 12028 KB Output is correct
20 Correct 19 ms 12156 KB Output is correct
21 Correct 15 ms 12124 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 13 ms 12152 KB Output is correct
26 Correct 16 ms 12636 KB Output is correct
27 Correct 15 ms 12536 KB Output is correct
28 Correct 16 ms 12792 KB Output is correct
29 Correct 16 ms 12664 KB Output is correct
30 Correct 16 ms 12536 KB Output is correct
31 Correct 13 ms 12152 KB Output is correct
32 Correct 16 ms 12792 KB Output is correct
33 Correct 13 ms 12124 KB Output is correct
34 Correct 15 ms 12536 KB Output is correct
35 Correct 16 ms 12664 KB Output is correct
36 Correct 16 ms 12536 KB Output is correct
37 Correct 16 ms 12536 KB Output is correct
38 Correct 13 ms 12152 KB Output is correct
39 Correct 16 ms 12536 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 16 ms 12536 KB Output is correct
42 Correct 15 ms 12668 KB Output is correct
43 Correct 16 ms 12664 KB Output is correct
44 Correct 15 ms 12152 KB Output is correct
45 Correct 18 ms 12792 KB Output is correct
46 Correct 16 ms 12664 KB Output is correct
47 Correct 13 ms 12152 KB Output is correct
48 Correct 16 ms 12536 KB Output is correct
49 Correct 15 ms 12664 KB Output is correct
50 Correct 15 ms 12664 KB Output is correct
51 Correct 15 ms 12536 KB Output is correct
52 Correct 15 ms 12536 KB Output is correct
53 Correct 15 ms 12664 KB Output is correct
54 Correct 16 ms 12536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12152 KB Output is correct
4 Correct 13 ms 12156 KB Output is correct
5 Correct 13 ms 12128 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12028 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 13 ms 12152 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 13 ms 12024 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 13 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 13 ms 12028 KB Output is correct
20 Correct 19 ms 12156 KB Output is correct
21 Correct 15 ms 12124 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 12 ms 12152 KB Output is correct
26 Correct 115 ms 27504 KB Output is correct
27 Correct 165 ms 27256 KB Output is correct
28 Correct 15 ms 12536 KB Output is correct
29 Correct 12 ms 12152 KB Output is correct
30 Correct 12 ms 12152 KB Output is correct
31 Correct 146 ms 27104 KB Output is correct
32 Correct 15 ms 12536 KB Output is correct
33 Correct 162 ms 31352 KB Output is correct
34 Correct 166 ms 27228 KB Output is correct
35 Correct 23 ms 12536 KB Output is correct
36 Correct 163 ms 27572 KB Output is correct
37 Correct 19 ms 12536 KB Output is correct
38 Correct 19 ms 12536 KB Output is correct
39 Correct 161 ms 27388 KB Output is correct
40 Correct 15 ms 12664 KB Output is correct
41 Correct 198 ms 27428 KB Output is correct
42 Correct 198 ms 28408 KB Output is correct
43 Correct 12 ms 12152 KB Output is correct
44 Correct 187 ms 32472 KB Output is correct
45 Correct 162 ms 29432 KB Output is correct
46 Correct 15 ms 12540 KB Output is correct
47 Correct 16 ms 12536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 26080 KB Output is correct
2 Correct 130 ms 30980 KB Output is correct
3 Correct 18 ms 12536 KB Output is correct
4 Correct 16 ms 12536 KB Output is correct
5 Correct 36 ms 12152 KB Output is correct
6 Correct 15 ms 12212 KB Output is correct
7 Correct 18 ms 12540 KB Output is correct
8 Correct 169 ms 29688 KB Output is correct
9 Correct 16 ms 12536 KB Output is correct
10 Correct 154 ms 27400 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 148 ms 27256 KB Output is correct
13 Correct 176 ms 29580 KB Output is correct
14 Correct 153 ms 32376 KB Output is correct
15 Correct 134 ms 27284 KB Output is correct
16 Correct 15 ms 12664 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 140 ms 32048 KB Output is correct
19 Correct 156 ms 34680 KB Output is correct
20 Correct 16 ms 12540 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 141 ms 29888 KB Output is correct
23 Correct 16 ms 12536 KB Output is correct
24 Correct 153 ms 27780 KB Output is correct
25 Correct 162 ms 33840 KB Output is correct
26 Correct 15 ms 12664 KB Output is correct
27 Correct 16 ms 12920 KB Output is correct
28 Correct 16 ms 12536 KB Output is correct
29 Correct 15 ms 12540 KB Output is correct
30 Correct 15 ms 12668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12152 KB Output is correct
4 Correct 13 ms 12156 KB Output is correct
5 Correct 13 ms 12128 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12028 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 13 ms 12152 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 13 ms 12024 KB Output is correct
14 Correct 13 ms 12152 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 13 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 13 ms 12028 KB Output is correct
20 Correct 19 ms 12156 KB Output is correct
21 Correct 15 ms 12124 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 13 ms 12152 KB Output is correct
26 Correct 16 ms 12636 KB Output is correct
27 Correct 15 ms 12536 KB Output is correct
28 Correct 16 ms 12792 KB Output is correct
29 Correct 16 ms 12664 KB Output is correct
30 Correct 16 ms 12536 KB Output is correct
31 Correct 13 ms 12152 KB Output is correct
32 Correct 16 ms 12792 KB Output is correct
33 Correct 13 ms 12124 KB Output is correct
34 Correct 15 ms 12536 KB Output is correct
35 Correct 16 ms 12664 KB Output is correct
36 Correct 16 ms 12536 KB Output is correct
37 Correct 16 ms 12536 KB Output is correct
38 Correct 13 ms 12152 KB Output is correct
39 Correct 16 ms 12536 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 16 ms 12536 KB Output is correct
42 Correct 15 ms 12668 KB Output is correct
43 Correct 16 ms 12664 KB Output is correct
44 Correct 15 ms 12152 KB Output is correct
45 Correct 18 ms 12792 KB Output is correct
46 Correct 16 ms 12664 KB Output is correct
47 Correct 13 ms 12152 KB Output is correct
48 Correct 16 ms 12536 KB Output is correct
49 Correct 15 ms 12664 KB Output is correct
50 Correct 15 ms 12664 KB Output is correct
51 Correct 15 ms 12536 KB Output is correct
52 Correct 15 ms 12536 KB Output is correct
53 Correct 15 ms 12664 KB Output is correct
54 Correct 16 ms 12536 KB Output is correct
55 Correct 12 ms 12152 KB Output is correct
56 Correct 115 ms 27504 KB Output is correct
57 Correct 165 ms 27256 KB Output is correct
58 Correct 15 ms 12536 KB Output is correct
59 Correct 12 ms 12152 KB Output is correct
60 Correct 12 ms 12152 KB Output is correct
61 Correct 146 ms 27104 KB Output is correct
62 Correct 15 ms 12536 KB Output is correct
63 Correct 162 ms 31352 KB Output is correct
64 Correct 166 ms 27228 KB Output is correct
65 Correct 23 ms 12536 KB Output is correct
66 Correct 163 ms 27572 KB Output is correct
67 Correct 19 ms 12536 KB Output is correct
68 Correct 19 ms 12536 KB Output is correct
69 Correct 161 ms 27388 KB Output is correct
70 Correct 15 ms 12664 KB Output is correct
71 Correct 198 ms 27428 KB Output is correct
72 Correct 198 ms 28408 KB Output is correct
73 Correct 12 ms 12152 KB Output is correct
74 Correct 187 ms 32472 KB Output is correct
75 Correct 162 ms 29432 KB Output is correct
76 Correct 15 ms 12540 KB Output is correct
77 Correct 16 ms 12536 KB Output is correct
78 Correct 120 ms 26080 KB Output is correct
79 Correct 130 ms 30980 KB Output is correct
80 Correct 18 ms 12536 KB Output is correct
81 Correct 16 ms 12536 KB Output is correct
82 Correct 36 ms 12152 KB Output is correct
83 Correct 15 ms 12212 KB Output is correct
84 Correct 18 ms 12540 KB Output is correct
85 Correct 169 ms 29688 KB Output is correct
86 Correct 16 ms 12536 KB Output is correct
87 Correct 154 ms 27400 KB Output is correct
88 Correct 13 ms 12152 KB Output is correct
89 Correct 148 ms 27256 KB Output is correct
90 Correct 176 ms 29580 KB Output is correct
91 Correct 153 ms 32376 KB Output is correct
92 Correct 134 ms 27284 KB Output is correct
93 Correct 15 ms 12664 KB Output is correct
94 Correct 12 ms 12152 KB Output is correct
95 Correct 140 ms 32048 KB Output is correct
96 Correct 156 ms 34680 KB Output is correct
97 Correct 16 ms 12540 KB Output is correct
98 Correct 13 ms 12152 KB Output is correct
99 Correct 141 ms 29888 KB Output is correct
100 Correct 16 ms 12536 KB Output is correct
101 Correct 153 ms 27780 KB Output is correct
102 Correct 162 ms 33840 KB Output is correct
103 Correct 15 ms 12664 KB Output is correct
104 Correct 16 ms 12920 KB Output is correct
105 Correct 16 ms 12536 KB Output is correct
106 Correct 15 ms 12540 KB Output is correct
107 Correct 15 ms 12668 KB Output is correct
108 Correct 920 ms 99808 KB Output is correct
109 Correct 1340 ms 99548 KB Output is correct
110 Correct 1400 ms 109220 KB Output is correct
111 Correct 1255 ms 128212 KB Output is correct
112 Correct 1221 ms 122396 KB Output is correct
113 Correct 908 ms 113312 KB Output is correct
114 Correct 1222 ms 87816 KB Output is correct
115 Correct 1252 ms 89012 KB Output is correct
116 Correct 1249 ms 94060 KB Output is correct
117 Correct 1048 ms 114600 KB Output is correct
118 Correct 970 ms 87400 KB Output is correct
119 Correct 1008 ms 114552 KB Output is correct
120 Correct 1094 ms 122816 KB Output is correct
121 Correct 1114 ms 114552 KB Output is correct
122 Correct 1195 ms 103640 KB Output is correct
123 Correct 821 ms 116068 KB Output is correct
124 Correct 1192 ms 112268 KB Output is correct