답안 #1017545

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1017545 2024-07-09T08:46:25 Z ByeWorld Mergers (JOI19_mergers) C++14
56 / 100
3000 ms 83024 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 = 13;
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];
void dfs(int nw, int par){
	anc[nw][0] = par; dep[nw] = dep[par]+1;
	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];
}
void upd(int x, int y){
	while(x != y){
		DSU.mer(x, anc[x][0]); x = anc[x][0];
	}
}
vector <int> edge[MAXN];

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++){
		int las = -1;
		for(auto in : vec[i]){
			if(las==-1) las = in;
			else {
				int lca = LCA(las, in); // update las->lca, in->lca
				upd(las, lca); upd(in, lca);
				las = lca;
			}
		}
	}

	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';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 51288 KB Output is correct
2 Correct 7 ms 51292 KB Output is correct
3 Correct 8 ms 51404 KB Output is correct
4 Correct 9 ms 51544 KB Output is correct
5 Correct 7 ms 51336 KB Output is correct
6 Correct 7 ms 49244 KB Output is correct
7 Correct 7 ms 47196 KB Output is correct
8 Correct 8 ms 51288 KB Output is correct
9 Correct 8 ms 49240 KB Output is correct
10 Correct 7 ms 51292 KB Output is correct
11 Correct 7 ms 51412 KB Output is correct
12 Correct 8 ms 51292 KB Output is correct
13 Correct 8 ms 51292 KB Output is correct
14 Correct 7 ms 51416 KB Output is correct
15 Correct 8 ms 49400 KB Output is correct
16 Correct 7 ms 49244 KB Output is correct
17 Correct 8 ms 51288 KB Output is correct
18 Correct 8 ms 51292 KB Output is correct
19 Correct 10 ms 51292 KB Output is correct
20 Correct 7 ms 49380 KB Output is correct
21 Correct 8 ms 49240 KB Output is correct
22 Correct 8 ms 51292 KB Output is correct
23 Correct 7 ms 49364 KB Output is correct
24 Correct 7 ms 51264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 51288 KB Output is correct
2 Correct 7 ms 51292 KB Output is correct
3 Correct 8 ms 51404 KB Output is correct
4 Correct 9 ms 51544 KB Output is correct
5 Correct 7 ms 51336 KB Output is correct
6 Correct 7 ms 49244 KB Output is correct
7 Correct 7 ms 47196 KB Output is correct
8 Correct 8 ms 51288 KB Output is correct
9 Correct 8 ms 49240 KB Output is correct
10 Correct 7 ms 51292 KB Output is correct
11 Correct 7 ms 51412 KB Output is correct
12 Correct 8 ms 51292 KB Output is correct
13 Correct 8 ms 51292 KB Output is correct
14 Correct 7 ms 51416 KB Output is correct
15 Correct 8 ms 49400 KB Output is correct
16 Correct 7 ms 49244 KB Output is correct
17 Correct 8 ms 51288 KB Output is correct
18 Correct 8 ms 51292 KB Output is correct
19 Correct 10 ms 51292 KB Output is correct
20 Correct 7 ms 49380 KB Output is correct
21 Correct 8 ms 49240 KB Output is correct
22 Correct 8 ms 51292 KB Output is correct
23 Correct 7 ms 49364 KB Output is correct
24 Correct 7 ms 51264 KB Output is correct
25 Correct 8 ms 51292 KB Output is correct
26 Correct 10 ms 51576 KB Output is correct
27 Correct 9 ms 51548 KB Output is correct
28 Correct 10 ms 50012 KB Output is correct
29 Correct 11 ms 51548 KB Output is correct
30 Correct 10 ms 49880 KB Output is correct
31 Correct 7 ms 49244 KB Output is correct
32 Correct 9 ms 51688 KB Output is correct
33 Correct 7 ms 49244 KB Output is correct
34 Correct 10 ms 51632 KB Output is correct
35 Correct 10 ms 50012 KB Output is correct
36 Correct 9 ms 51380 KB Output is correct
37 Correct 12 ms 47960 KB Output is correct
38 Correct 7 ms 47196 KB Output is correct
39 Correct 10 ms 47836 KB Output is correct
40 Correct 9 ms 45916 KB Output is correct
41 Correct 9 ms 47964 KB Output is correct
42 Correct 9 ms 47960 KB Output is correct
43 Correct 37 ms 47964 KB Output is correct
44 Correct 7 ms 49244 KB Output is correct
45 Correct 9 ms 45916 KB Output is correct
46 Correct 9 ms 50012 KB Output is correct
47 Correct 7 ms 47196 KB Output is correct
48 Correct 10 ms 47964 KB Output is correct
49 Correct 10 ms 48120 KB Output is correct
50 Correct 10 ms 48220 KB Output is correct
51 Correct 10 ms 50012 KB Output is correct
52 Correct 9 ms 47964 KB Output is correct
53 Correct 9 ms 45912 KB Output is correct
54 Correct 9 ms 47960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 51288 KB Output is correct
2 Correct 7 ms 51292 KB Output is correct
3 Correct 8 ms 51404 KB Output is correct
4 Correct 9 ms 51544 KB Output is correct
5 Correct 7 ms 51336 KB Output is correct
6 Correct 7 ms 49244 KB Output is correct
7 Correct 7 ms 47196 KB Output is correct
8 Correct 8 ms 51288 KB Output is correct
9 Correct 8 ms 49240 KB Output is correct
10 Correct 7 ms 51292 KB Output is correct
11 Correct 7 ms 51412 KB Output is correct
12 Correct 8 ms 51292 KB Output is correct
13 Correct 8 ms 51292 KB Output is correct
14 Correct 7 ms 51416 KB Output is correct
15 Correct 8 ms 49400 KB Output is correct
16 Correct 7 ms 49244 KB Output is correct
17 Correct 8 ms 51288 KB Output is correct
18 Correct 8 ms 51292 KB Output is correct
19 Correct 10 ms 51292 KB Output is correct
20 Correct 7 ms 49380 KB Output is correct
21 Correct 8 ms 49240 KB Output is correct
22 Correct 8 ms 51292 KB Output is correct
23 Correct 7 ms 49364 KB Output is correct
24 Correct 7 ms 51264 KB Output is correct
25 Correct 7 ms 47356 KB Output is correct
26 Correct 75 ms 68032 KB Output is correct
27 Correct 98 ms 69272 KB Output is correct
28 Correct 10 ms 47708 KB Output is correct
29 Correct 7 ms 47196 KB Output is correct
30 Correct 7 ms 45148 KB Output is correct
31 Correct 87 ms 68948 KB Output is correct
32 Correct 9 ms 50012 KB Output is correct
33 Execution timed out 3068 ms 71508 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 69316 KB Output is correct
2 Correct 94 ms 76484 KB Output is correct
3 Correct 10 ms 45912 KB Output is correct
4 Correct 10 ms 49996 KB Output is correct
5 Correct 7 ms 45148 KB Output is correct
6 Correct 7 ms 47196 KB Output is correct
7 Correct 9 ms 49904 KB Output is correct
8 Correct 106 ms 70948 KB Output is correct
9 Correct 13 ms 47704 KB Output is correct
10 Correct 87 ms 70688 KB Output is correct
11 Correct 7 ms 49240 KB Output is correct
12 Correct 103 ms 67676 KB Output is correct
13 Correct 108 ms 71252 KB Output is correct
14 Correct 102 ms 76112 KB Output is correct
15 Correct 77 ms 70700 KB Output is correct
16 Correct 9 ms 50008 KB Output is correct
17 Correct 7 ms 47312 KB Output is correct
18 Correct 93 ms 74052 KB Output is correct
19 Correct 108 ms 77396 KB Output is correct
20 Correct 10 ms 50008 KB Output is correct
21 Correct 7 ms 51380 KB Output is correct
22 Correct 90 ms 72776 KB Output is correct
23 Correct 10 ms 51536 KB Output is correct
24 Correct 95 ms 71216 KB Output is correct
25 Correct 117 ms 83024 KB Output is correct
26 Correct 10 ms 51820 KB Output is correct
27 Correct 10 ms 51724 KB Output is correct
28 Correct 10 ms 51392 KB Output is correct
29 Correct 9 ms 51548 KB Output is correct
30 Correct 9 ms 51548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 51288 KB Output is correct
2 Correct 7 ms 51292 KB Output is correct
3 Correct 8 ms 51404 KB Output is correct
4 Correct 9 ms 51544 KB Output is correct
5 Correct 7 ms 51336 KB Output is correct
6 Correct 7 ms 49244 KB Output is correct
7 Correct 7 ms 47196 KB Output is correct
8 Correct 8 ms 51288 KB Output is correct
9 Correct 8 ms 49240 KB Output is correct
10 Correct 7 ms 51292 KB Output is correct
11 Correct 7 ms 51412 KB Output is correct
12 Correct 8 ms 51292 KB Output is correct
13 Correct 8 ms 51292 KB Output is correct
14 Correct 7 ms 51416 KB Output is correct
15 Correct 8 ms 49400 KB Output is correct
16 Correct 7 ms 49244 KB Output is correct
17 Correct 8 ms 51288 KB Output is correct
18 Correct 8 ms 51292 KB Output is correct
19 Correct 10 ms 51292 KB Output is correct
20 Correct 7 ms 49380 KB Output is correct
21 Correct 8 ms 49240 KB Output is correct
22 Correct 8 ms 51292 KB Output is correct
23 Correct 7 ms 49364 KB Output is correct
24 Correct 7 ms 51264 KB Output is correct
25 Correct 8 ms 51292 KB Output is correct
26 Correct 10 ms 51576 KB Output is correct
27 Correct 9 ms 51548 KB Output is correct
28 Correct 10 ms 50012 KB Output is correct
29 Correct 11 ms 51548 KB Output is correct
30 Correct 10 ms 49880 KB Output is correct
31 Correct 7 ms 49244 KB Output is correct
32 Correct 9 ms 51688 KB Output is correct
33 Correct 7 ms 49244 KB Output is correct
34 Correct 10 ms 51632 KB Output is correct
35 Correct 10 ms 50012 KB Output is correct
36 Correct 9 ms 51380 KB Output is correct
37 Correct 12 ms 47960 KB Output is correct
38 Correct 7 ms 47196 KB Output is correct
39 Correct 10 ms 47836 KB Output is correct
40 Correct 9 ms 45916 KB Output is correct
41 Correct 9 ms 47964 KB Output is correct
42 Correct 9 ms 47960 KB Output is correct
43 Correct 37 ms 47964 KB Output is correct
44 Correct 7 ms 49244 KB Output is correct
45 Correct 9 ms 45916 KB Output is correct
46 Correct 9 ms 50012 KB Output is correct
47 Correct 7 ms 47196 KB Output is correct
48 Correct 10 ms 47964 KB Output is correct
49 Correct 10 ms 48120 KB Output is correct
50 Correct 10 ms 48220 KB Output is correct
51 Correct 10 ms 50012 KB Output is correct
52 Correct 9 ms 47964 KB Output is correct
53 Correct 9 ms 45912 KB Output is correct
54 Correct 9 ms 47960 KB Output is correct
55 Correct 7 ms 47356 KB Output is correct
56 Correct 75 ms 68032 KB Output is correct
57 Correct 98 ms 69272 KB Output is correct
58 Correct 10 ms 47708 KB Output is correct
59 Correct 7 ms 47196 KB Output is correct
60 Correct 7 ms 45148 KB Output is correct
61 Correct 87 ms 68948 KB Output is correct
62 Correct 9 ms 50012 KB Output is correct
63 Execution timed out 3068 ms 71508 KB Time limit exceeded
64 Halted 0 ms 0 KB -