답안 #1017701

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1017701 2024-07-09T09:32:29 Z ByeWorld Mergers (JOI19_mergers) C++14
100 / 100
937 ms 239424 KB
#include <bits/stdc++.h>
// #pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 5e5+10;
const int MAXA = 9e3+20;
const ll INF = 1e9+10;
const int LOG = 20;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
vector <int> key = {29, 31};
vector <int> mod = {998244353, 1000000007};
void chmx(int &a, int b){ a = max(a, b); }
 
int n, k;
vector <int> adj[MAXN], vec[MAXN];
int a[MAXN], tot[MAXN], u[MAXN], v[MAXN];
 
struct dsu {
	int par[MAXN], siz[MAXN];
	void bd(){
		for(int i=1; i<=n; i++){
			par[i] = i; siz[i] = 1;
		}
	}
	int f(int x){
		if(par[x]==x) return x;
		return par[x] = f(par[x]);
	}
	bool con(int x, int y){ return f(x) == f(y); }
	void mer(int x, int y){
		x = f(x); y = f(y);
		if(x==y) return;
		if(siz[x] > siz[y]) swap(x, y);
		par[x] = y; siz[y] += siz[x];
	}
} DSU;
 
int anc[MAXN][LOG+5], dep[MAXN], in[MAXN], tim;
void dfs(int nw, int par){
	anc[nw][0] = par; dep[nw] = dep[par]+1; in[nw] = ++tim;
	for(auto nx : adj[nw]){
		if(nx==par) continue;
		dfs(nx, nw);
	}
}
int LCA(int x, int y){
	if(dep[x] > dep[y]) swap(x, y); 
	for(int i=LOG-1; i>=0; i--){
		if(dep[anc[y][i]] >= dep[x])
			y = anc[y][i];
	}
	if(x==y) return x;
	for(int i=LOG-1; i>=0; i--){
		if(anc[y][i] != anc[x][i]){
			y = anc[y][i]; x = anc[x][i];
		}
	}
	return anc[x][0];
}
int cnt[MAXN];
vector <int> edge[MAXN];
bool cmp(int a, int b){
	return in[a] < in[b];
}
int ANS(int nw, int par){
	for(auto nx : adj[nw]){
		if(nx==par) continue;
		cnt[nw] += ANS(nx, nw);
	}
	return cnt[nw];
}
signed main(){
	// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> k; DSU.bd();
	for(int i=1; i<=n-1; i++){
		int x, y; cin >> x >> y; u[i] = x; v[i] = y;
		adj[x].pb(y); adj[y].pb(x);
	}
	for(int i=1; i<=n; i++){
		cin >> a[i]; tot[a[i]]++;
		vec[a[i]].pb(i);
	}
	dfs(1, 0);
	for(int j=1; j<LOG; j++)
		for(int i=1; i<=n; i++) anc[i][j] = anc[anc[i][j-1]][j-1];
 
	for(int i=1; i<=k; i++){
		if(vec[i].size() <= 1) continue;
		sort(vec[i].begin(), vec[i].end(), cmp);
		for(int j=1; j<vec[i].size(); j++){
			cnt[LCA(vec[i][j-1], vec[i][j])] += 2;
			cnt[vec[i][j-1]]--; cnt[vec[i][j]]--;
		}
			cnt[LCA(vec[i][0], vec[i].back())] += 2;
			cnt[vec[i][0]]--; cnt[vec[i].back()]--;
	}
 	
 	ANS(1, 0);
 	for(int i=1; i<=n; i++) 
 		if(cnt[i]) DSU.mer(i, anc[i][0]);

	for(int i=1; i<=n-1; i++){
		int x = DSU.f(u[i]), y = DSU.f(v[i]);
		if(x == y) continue;
		edge[x].pb(y); edge[y].pb(x);
	}
	int tot = 0;
	for(int i=1; i<=n; i++){
		if(edge[i].size() == 1) tot++;
	}
	cout << (tot+1)/2 << '\n';
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:104:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |   for(int j=1; j<vec[i].size(); j++){
      |                ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41564 KB Output is correct
2 Correct 7 ms 43612 KB Output is correct
3 Correct 7 ms 43604 KB Output is correct
4 Correct 7 ms 43608 KB Output is correct
5 Correct 11 ms 41580 KB Output is correct
6 Correct 10 ms 41564 KB Output is correct
7 Correct 7 ms 43356 KB Output is correct
8 Correct 7 ms 43452 KB Output is correct
9 Correct 7 ms 41564 KB Output is correct
10 Correct 7 ms 43612 KB Output is correct
11 Correct 7 ms 41564 KB Output is correct
12 Correct 7 ms 43612 KB Output is correct
13 Correct 7 ms 41564 KB Output is correct
14 Correct 10 ms 43460 KB Output is correct
15 Correct 11 ms 41564 KB Output is correct
16 Correct 7 ms 41564 KB Output is correct
17 Correct 7 ms 41572 KB Output is correct
18 Correct 7 ms 43640 KB Output is correct
19 Correct 7 ms 41564 KB Output is correct
20 Correct 10 ms 43592 KB Output is correct
21 Correct 8 ms 43612 KB Output is correct
22 Correct 7 ms 41564 KB Output is correct
23 Correct 7 ms 43624 KB Output is correct
24 Correct 7 ms 43588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41564 KB Output is correct
2 Correct 7 ms 43612 KB Output is correct
3 Correct 7 ms 43604 KB Output is correct
4 Correct 7 ms 43608 KB Output is correct
5 Correct 11 ms 41580 KB Output is correct
6 Correct 10 ms 41564 KB Output is correct
7 Correct 7 ms 43356 KB Output is correct
8 Correct 7 ms 43452 KB Output is correct
9 Correct 7 ms 41564 KB Output is correct
10 Correct 7 ms 43612 KB Output is correct
11 Correct 7 ms 41564 KB Output is correct
12 Correct 7 ms 43612 KB Output is correct
13 Correct 7 ms 41564 KB Output is correct
14 Correct 10 ms 43460 KB Output is correct
15 Correct 11 ms 41564 KB Output is correct
16 Correct 7 ms 41564 KB Output is correct
17 Correct 7 ms 41572 KB Output is correct
18 Correct 7 ms 43640 KB Output is correct
19 Correct 7 ms 41564 KB Output is correct
20 Correct 10 ms 43592 KB Output is correct
21 Correct 8 ms 43612 KB Output is correct
22 Correct 7 ms 41564 KB Output is correct
23 Correct 7 ms 43624 KB Output is correct
24 Correct 7 ms 43588 KB Output is correct
25 Correct 8 ms 51548 KB Output is correct
26 Correct 11 ms 51996 KB Output is correct
27 Correct 14 ms 51804 KB Output is correct
28 Correct 10 ms 52044 KB Output is correct
29 Correct 12 ms 52092 KB Output is correct
30 Correct 12 ms 51804 KB Output is correct
31 Correct 8 ms 51548 KB Output is correct
32 Correct 11 ms 52108 KB Output is correct
33 Correct 9 ms 51548 KB Output is correct
34 Correct 11 ms 52016 KB Output is correct
35 Correct 12 ms 52040 KB Output is correct
36 Correct 12 ms 51980 KB Output is correct
37 Correct 13 ms 51932 KB Output is correct
38 Correct 9 ms 51792 KB Output is correct
39 Correct 11 ms 51804 KB Output is correct
40 Correct 11 ms 51804 KB Output is correct
41 Correct 10 ms 51860 KB Output is correct
42 Correct 11 ms 51984 KB Output is correct
43 Correct 10 ms 52060 KB Output is correct
44 Correct 9 ms 51804 KB Output is correct
45 Correct 12 ms 52060 KB Output is correct
46 Correct 11 ms 51804 KB Output is correct
47 Correct 8 ms 51548 KB Output is correct
48 Correct 11 ms 51796 KB Output is correct
49 Correct 13 ms 52060 KB Output is correct
50 Correct 11 ms 51976 KB Output is correct
51 Correct 12 ms 51800 KB Output is correct
52 Correct 10 ms 51800 KB Output is correct
53 Correct 11 ms 51800 KB Output is correct
54 Correct 10 ms 51800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41564 KB Output is correct
2 Correct 7 ms 43612 KB Output is correct
3 Correct 7 ms 43604 KB Output is correct
4 Correct 7 ms 43608 KB Output is correct
5 Correct 11 ms 41580 KB Output is correct
6 Correct 10 ms 41564 KB Output is correct
7 Correct 7 ms 43356 KB Output is correct
8 Correct 7 ms 43452 KB Output is correct
9 Correct 7 ms 41564 KB Output is correct
10 Correct 7 ms 43612 KB Output is correct
11 Correct 7 ms 41564 KB Output is correct
12 Correct 7 ms 43612 KB Output is correct
13 Correct 7 ms 41564 KB Output is correct
14 Correct 10 ms 43460 KB Output is correct
15 Correct 11 ms 41564 KB Output is correct
16 Correct 7 ms 41564 KB Output is correct
17 Correct 7 ms 41572 KB Output is correct
18 Correct 7 ms 43640 KB Output is correct
19 Correct 7 ms 41564 KB Output is correct
20 Correct 10 ms 43592 KB Output is correct
21 Correct 8 ms 43612 KB Output is correct
22 Correct 7 ms 41564 KB Output is correct
23 Correct 7 ms 43624 KB Output is correct
24 Correct 7 ms 43588 KB Output is correct
25 Correct 9 ms 51656 KB Output is correct
26 Correct 112 ms 82684 KB Output is correct
27 Correct 156 ms 82516 KB Output is correct
28 Correct 12 ms 51804 KB Output is correct
29 Correct 8 ms 51548 KB Output is correct
30 Correct 8 ms 51548 KB Output is correct
31 Correct 123 ms 78324 KB Output is correct
32 Correct 14 ms 51800 KB Output is correct
33 Correct 123 ms 85332 KB Output is correct
34 Correct 123 ms 81744 KB Output is correct
35 Correct 12 ms 51784 KB Output is correct
36 Correct 131 ms 82480 KB Output is correct
37 Correct 11 ms 51804 KB Output is correct
38 Correct 14 ms 51804 KB Output is correct
39 Correct 107 ms 73648 KB Output is correct
40 Correct 10 ms 52060 KB Output is correct
41 Correct 127 ms 82288 KB Output is correct
42 Correct 122 ms 83284 KB Output is correct
43 Correct 9 ms 51800 KB Output is correct
44 Correct 130 ms 87600 KB Output is correct
45 Correct 164 ms 86460 KB Output is correct
46 Correct 11 ms 50524 KB Output is correct
47 Correct 12 ms 51804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 82888 KB Output is correct
2 Correct 109 ms 90656 KB Output is correct
3 Correct 10 ms 52072 KB Output is correct
4 Correct 13 ms 51904 KB Output is correct
5 Correct 8 ms 51548 KB Output is correct
6 Correct 9 ms 51548 KB Output is correct
7 Correct 10 ms 46416 KB Output is correct
8 Correct 126 ms 76636 KB Output is correct
9 Correct 12 ms 51800 KB Output is correct
10 Correct 110 ms 82700 KB Output is correct
11 Correct 8 ms 51800 KB Output is correct
12 Correct 124 ms 82352 KB Output is correct
13 Correct 133 ms 83976 KB Output is correct
14 Correct 148 ms 90448 KB Output is correct
15 Correct 102 ms 82952 KB Output is correct
16 Correct 10 ms 52056 KB Output is correct
17 Correct 8 ms 51792 KB Output is correct
18 Correct 105 ms 89668 KB Output is correct
19 Correct 114 ms 91732 KB Output is correct
20 Correct 10 ms 51804 KB Output is correct
21 Correct 10 ms 51804 KB Output is correct
22 Correct 109 ms 84124 KB Output is correct
23 Correct 10 ms 51804 KB Output is correct
24 Correct 123 ms 83060 KB Output is correct
25 Correct 125 ms 92752 KB Output is correct
26 Correct 10 ms 52044 KB Output is correct
27 Correct 10 ms 52044 KB Output is correct
28 Correct 10 ms 51804 KB Output is correct
29 Correct 10 ms 51948 KB Output is correct
30 Correct 10 ms 51804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41564 KB Output is correct
2 Correct 7 ms 43612 KB Output is correct
3 Correct 7 ms 43604 KB Output is correct
4 Correct 7 ms 43608 KB Output is correct
5 Correct 11 ms 41580 KB Output is correct
6 Correct 10 ms 41564 KB Output is correct
7 Correct 7 ms 43356 KB Output is correct
8 Correct 7 ms 43452 KB Output is correct
9 Correct 7 ms 41564 KB Output is correct
10 Correct 7 ms 43612 KB Output is correct
11 Correct 7 ms 41564 KB Output is correct
12 Correct 7 ms 43612 KB Output is correct
13 Correct 7 ms 41564 KB Output is correct
14 Correct 10 ms 43460 KB Output is correct
15 Correct 11 ms 41564 KB Output is correct
16 Correct 7 ms 41564 KB Output is correct
17 Correct 7 ms 41572 KB Output is correct
18 Correct 7 ms 43640 KB Output is correct
19 Correct 7 ms 41564 KB Output is correct
20 Correct 10 ms 43592 KB Output is correct
21 Correct 8 ms 43612 KB Output is correct
22 Correct 7 ms 41564 KB Output is correct
23 Correct 7 ms 43624 KB Output is correct
24 Correct 7 ms 43588 KB Output is correct
25 Correct 8 ms 51548 KB Output is correct
26 Correct 11 ms 51996 KB Output is correct
27 Correct 14 ms 51804 KB Output is correct
28 Correct 10 ms 52044 KB Output is correct
29 Correct 12 ms 52092 KB Output is correct
30 Correct 12 ms 51804 KB Output is correct
31 Correct 8 ms 51548 KB Output is correct
32 Correct 11 ms 52108 KB Output is correct
33 Correct 9 ms 51548 KB Output is correct
34 Correct 11 ms 52016 KB Output is correct
35 Correct 12 ms 52040 KB Output is correct
36 Correct 12 ms 51980 KB Output is correct
37 Correct 13 ms 51932 KB Output is correct
38 Correct 9 ms 51792 KB Output is correct
39 Correct 11 ms 51804 KB Output is correct
40 Correct 11 ms 51804 KB Output is correct
41 Correct 10 ms 51860 KB Output is correct
42 Correct 11 ms 51984 KB Output is correct
43 Correct 10 ms 52060 KB Output is correct
44 Correct 9 ms 51804 KB Output is correct
45 Correct 12 ms 52060 KB Output is correct
46 Correct 11 ms 51804 KB Output is correct
47 Correct 8 ms 51548 KB Output is correct
48 Correct 11 ms 51796 KB Output is correct
49 Correct 13 ms 52060 KB Output is correct
50 Correct 11 ms 51976 KB Output is correct
51 Correct 12 ms 51800 KB Output is correct
52 Correct 10 ms 51800 KB Output is correct
53 Correct 11 ms 51800 KB Output is correct
54 Correct 10 ms 51800 KB Output is correct
55 Correct 9 ms 51656 KB Output is correct
56 Correct 112 ms 82684 KB Output is correct
57 Correct 156 ms 82516 KB Output is correct
58 Correct 12 ms 51804 KB Output is correct
59 Correct 8 ms 51548 KB Output is correct
60 Correct 8 ms 51548 KB Output is correct
61 Correct 123 ms 78324 KB Output is correct
62 Correct 14 ms 51800 KB Output is correct
63 Correct 123 ms 85332 KB Output is correct
64 Correct 123 ms 81744 KB Output is correct
65 Correct 12 ms 51784 KB Output is correct
66 Correct 131 ms 82480 KB Output is correct
67 Correct 11 ms 51804 KB Output is correct
68 Correct 14 ms 51804 KB Output is correct
69 Correct 107 ms 73648 KB Output is correct
70 Correct 10 ms 52060 KB Output is correct
71 Correct 127 ms 82288 KB Output is correct
72 Correct 122 ms 83284 KB Output is correct
73 Correct 9 ms 51800 KB Output is correct
74 Correct 130 ms 87600 KB Output is correct
75 Correct 164 ms 86460 KB Output is correct
76 Correct 11 ms 50524 KB Output is correct
77 Correct 12 ms 51804 KB Output is correct
78 Correct 102 ms 82888 KB Output is correct
79 Correct 109 ms 90656 KB Output is correct
80 Correct 10 ms 52072 KB Output is correct
81 Correct 13 ms 51904 KB Output is correct
82 Correct 8 ms 51548 KB Output is correct
83 Correct 9 ms 51548 KB Output is correct
84 Correct 10 ms 46416 KB Output is correct
85 Correct 126 ms 76636 KB Output is correct
86 Correct 12 ms 51800 KB Output is correct
87 Correct 110 ms 82700 KB Output is correct
88 Correct 8 ms 51800 KB Output is correct
89 Correct 124 ms 82352 KB Output is correct
90 Correct 133 ms 83976 KB Output is correct
91 Correct 148 ms 90448 KB Output is correct
92 Correct 102 ms 82952 KB Output is correct
93 Correct 10 ms 52056 KB Output is correct
94 Correct 8 ms 51792 KB Output is correct
95 Correct 105 ms 89668 KB Output is correct
96 Correct 114 ms 91732 KB Output is correct
97 Correct 10 ms 51804 KB Output is correct
98 Correct 10 ms 51804 KB Output is correct
99 Correct 109 ms 84124 KB Output is correct
100 Correct 10 ms 51804 KB Output is correct
101 Correct 123 ms 83060 KB Output is correct
102 Correct 125 ms 92752 KB Output is correct
103 Correct 10 ms 52044 KB Output is correct
104 Correct 10 ms 52044 KB Output is correct
105 Correct 10 ms 51804 KB Output is correct
106 Correct 10 ms 51948 KB Output is correct
107 Correct 10 ms 51804 KB Output is correct
108 Correct 646 ms 203284 KB Output is correct
109 Correct 868 ms 201604 KB Output is correct
110 Correct 909 ms 208312 KB Output is correct
111 Correct 786 ms 233492 KB Output is correct
112 Correct 746 ms 227912 KB Output is correct
113 Correct 621 ms 223528 KB Output is correct
114 Correct 687 ms 193432 KB Output is correct
115 Correct 721 ms 193584 KB Output is correct
116 Correct 937 ms 199972 KB Output is correct
117 Correct 725 ms 226896 KB Output is correct
118 Correct 670 ms 193604 KB Output is correct
119 Correct 686 ms 226848 KB Output is correct
120 Correct 783 ms 239424 KB Output is correct
121 Correct 702 ms 226720 KB Output is correct
122 Correct 824 ms 206032 KB Output is correct
123 Correct 597 ms 225680 KB Output is correct
124 Correct 760 ms 212632 KB Output is correct