답안 #978011

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
978011 2024-05-08T16:10:15 Z happy_node Mergers (JOI19_mergers) C++17
100 / 100
1272 ms 170744 KB
#include <bits/stdc++.h>
using namespace std;

const int MX=5e5+5;
int N,K;
int S[MX];
vector<int> adj[MX];

int up[MX][20];
int timer=0,tin[MX],tout[MX];

void dfs(int v, int p) {
	up[v][0]=p;
	tin[v]=++timer;
	for(int k=1;k<20;k++) {
		up[v][k]=up[up[v][k-1]][k-1];
	}
	for(auto u:adj[v]) {
		if(u==p) continue;
		dfs(u,v);
	}
	tout[v]=timer;
}

bool isAnc(int anc, int nd) {
	return tin[anc]<=tin[nd] && tout[nd]<=tout[anc];
}

int LCA(int u, int v) {
	if(isAnc(u,v)) return u;
	if(isAnc(v,u)) return v;
	for(int k=19;k>=0;k--) {
		if(up[u][k]!=0 && !isAnc(up[u][k],v)) {
			u=up[u][k];
		}
	}
	return up[u][0];
}

struct fenwick {
	int t[MX];

	void upd(int pos, int val) {
		for(int i=pos;i<=N;i+=i&-i) t[i]+=val;
	}

	int que(int pos) {
		int res=0;
		for(int i=pos;i>0;i-=i&-i) res+=t[i];
		return res;
	}

	int que(int l, int r) {
		return que(r)-que(l-1);
	}
} ft;

vector<int> pos[MX];

struct dsu {
	int par[MX];

	int find(int v) {
		return par[v]==v?v:par[v]=find(par[v]);
	}

	bool merge(int u, int v) {
		u=find(u),v=find(v);
		if(u==v) return false;
		par[u]=v;
		return true;
	}

	void prep() {
		for(int i=1;i<=N;i++) par[i]=i;
	}
} ds;

int deg[MX];

void dfs2(int v, int p) {
	for(auto u:adj[v]) {
		if(u==p) continue;
		dfs2(u,v);
	}
	int k=ft.que(tin[v],tout[v]);
	if(k>0) {
		if(p!=0) ds.merge(v,p);
	}
}

int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);

	cin>>N>>K;

	for(int i=0;i<N-1;i++) {
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	for(int i=1;i<=N;i++) {
		cin>>S[i];
		pos[S[i]].push_back(i);
	}

	dfs(1,0);

	for(int c=1;c<=K;c++) {
		if(pos[c].empty()) continue;
		int cur=pos[c][0];
		for(auto p:pos[c]) {
			cur=LCA(cur,p);
		}
		for(auto p:pos[c]) {
			ft.upd(tin[p],1);
			ft.upd(tin[cur],-1);
		}
	}	

	ds.prep();
	dfs2(1,0);

	set<pair<int,int>> e;
	for(int i=1;i<=N;i++) {
		for(auto j:adj[i]) {
			if(ds.find(i)!=ds.find(j)) {
				int u=ds.find(i);
				int v=ds.find(j);
				if(u>v) swap(u,v);
				e.insert({u,v});
			}
		}
	}

	for(auto [x,y]:e) {
		deg[x]+=1;
		deg[y]+=1;
	}

	int leafs=0;
	for(int i=1;i<=N;i++) {
		if(deg[i]==1) {
			leafs+=1;
		}
	}

	cout<<(leafs+1)/2<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 35164 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 7 ms 35164 KB Output is correct
4 Correct 7 ms 35164 KB Output is correct
5 Correct 7 ms 35160 KB Output is correct
6 Correct 7 ms 35160 KB Output is correct
7 Correct 7 ms 35160 KB Output is correct
8 Correct 7 ms 35160 KB Output is correct
9 Correct 6 ms 35164 KB Output is correct
10 Correct 7 ms 35160 KB Output is correct
11 Correct 7 ms 35300 KB Output is correct
12 Correct 7 ms 35292 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35160 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 7 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 7 ms 35292 KB Output is correct
19 Correct 7 ms 35164 KB Output is correct
20 Correct 7 ms 35164 KB Output is correct
21 Correct 7 ms 35296 KB Output is correct
22 Correct 7 ms 35160 KB Output is correct
23 Correct 8 ms 35164 KB Output is correct
24 Correct 7 ms 35416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 35164 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 7 ms 35164 KB Output is correct
4 Correct 7 ms 35164 KB Output is correct
5 Correct 7 ms 35160 KB Output is correct
6 Correct 7 ms 35160 KB Output is correct
7 Correct 7 ms 35160 KB Output is correct
8 Correct 7 ms 35160 KB Output is correct
9 Correct 6 ms 35164 KB Output is correct
10 Correct 7 ms 35160 KB Output is correct
11 Correct 7 ms 35300 KB Output is correct
12 Correct 7 ms 35292 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35160 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 7 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 7 ms 35292 KB Output is correct
19 Correct 7 ms 35164 KB Output is correct
20 Correct 7 ms 35164 KB Output is correct
21 Correct 7 ms 35296 KB Output is correct
22 Correct 7 ms 35160 KB Output is correct
23 Correct 8 ms 35164 KB Output is correct
24 Correct 7 ms 35416 KB Output is correct
25 Correct 7 ms 35164 KB Output is correct
26 Correct 9 ms 35676 KB Output is correct
27 Correct 10 ms 35604 KB Output is correct
28 Correct 12 ms 35860 KB Output is correct
29 Correct 18 ms 35928 KB Output is correct
30 Correct 9 ms 35672 KB Output is correct
31 Correct 7 ms 35164 KB Output is correct
32 Correct 10 ms 35928 KB Output is correct
33 Correct 7 ms 35160 KB Output is correct
34 Correct 10 ms 35676 KB Output is correct
35 Correct 11 ms 35864 KB Output is correct
36 Correct 9 ms 35676 KB Output is correct
37 Correct 9 ms 35584 KB Output is correct
38 Correct 7 ms 35164 KB Output is correct
39 Correct 9 ms 35684 KB Output is correct
40 Correct 9 ms 35516 KB Output is correct
41 Correct 8 ms 35672 KB Output is correct
42 Correct 9 ms 35676 KB Output is correct
43 Correct 8 ms 35932 KB Output is correct
44 Correct 7 ms 35256 KB Output is correct
45 Correct 10 ms 35676 KB Output is correct
46 Correct 9 ms 35712 KB Output is correct
47 Correct 7 ms 35164 KB Output is correct
48 Correct 9 ms 35676 KB Output is correct
49 Correct 10 ms 35992 KB Output is correct
50 Correct 9 ms 36028 KB Output is correct
51 Correct 11 ms 35676 KB Output is correct
52 Correct 8 ms 35608 KB Output is correct
53 Correct 10 ms 35824 KB Output is correct
54 Correct 10 ms 35676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 35164 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 7 ms 35164 KB Output is correct
4 Correct 7 ms 35164 KB Output is correct
5 Correct 7 ms 35160 KB Output is correct
6 Correct 7 ms 35160 KB Output is correct
7 Correct 7 ms 35160 KB Output is correct
8 Correct 7 ms 35160 KB Output is correct
9 Correct 6 ms 35164 KB Output is correct
10 Correct 7 ms 35160 KB Output is correct
11 Correct 7 ms 35300 KB Output is correct
12 Correct 7 ms 35292 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35160 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 7 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 7 ms 35292 KB Output is correct
19 Correct 7 ms 35164 KB Output is correct
20 Correct 7 ms 35164 KB Output is correct
21 Correct 7 ms 35296 KB Output is correct
22 Correct 7 ms 35160 KB Output is correct
23 Correct 8 ms 35164 KB Output is correct
24 Correct 7 ms 35416 KB Output is correct
25 Correct 7 ms 35164 KB Output is correct
26 Correct 59 ms 50320 KB Output is correct
27 Correct 99 ms 49964 KB Output is correct
28 Correct 10 ms 35704 KB Output is correct
29 Correct 8 ms 35164 KB Output is correct
30 Correct 7 ms 35356 KB Output is correct
31 Correct 86 ms 49912 KB Output is correct
32 Correct 9 ms 35760 KB Output is correct
33 Correct 117 ms 60040 KB Output is correct
34 Correct 77 ms 49976 KB Output is correct
35 Correct 8 ms 35608 KB Output is correct
36 Correct 87 ms 50888 KB Output is correct
37 Correct 9 ms 35756 KB Output is correct
38 Correct 10 ms 35676 KB Output is correct
39 Correct 68 ms 49984 KB Output is correct
40 Correct 10 ms 35944 KB Output is correct
41 Correct 84 ms 49672 KB Output is correct
42 Correct 120 ms 53272 KB Output is correct
43 Correct 8 ms 35164 KB Output is correct
44 Correct 106 ms 60280 KB Output is correct
45 Correct 95 ms 55004 KB Output is correct
46 Correct 9 ms 35772 KB Output is correct
47 Correct 10 ms 35748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 48844 KB Output is correct
2 Correct 137 ms 56076 KB Output is correct
3 Correct 10 ms 35676 KB Output is correct
4 Correct 9 ms 35676 KB Output is correct
5 Correct 7 ms 35284 KB Output is correct
6 Correct 7 ms 35164 KB Output is correct
7 Correct 8 ms 35548 KB Output is correct
8 Correct 97 ms 51952 KB Output is correct
9 Correct 9 ms 35676 KB Output is correct
10 Correct 89 ms 50292 KB Output is correct
11 Correct 8 ms 35164 KB Output is correct
12 Correct 73 ms 50032 KB Output is correct
13 Correct 104 ms 52240 KB Output is correct
14 Correct 125 ms 57872 KB Output is correct
15 Correct 67 ms 49996 KB Output is correct
16 Correct 8 ms 35672 KB Output is correct
17 Correct 8 ms 35164 KB Output is correct
18 Correct 106 ms 56800 KB Output is correct
19 Correct 138 ms 63056 KB Output is correct
20 Correct 10 ms 35672 KB Output is correct
21 Correct 7 ms 35164 KB Output is correct
22 Correct 93 ms 52324 KB Output is correct
23 Correct 8 ms 35676 KB Output is correct
24 Correct 92 ms 50960 KB Output is correct
25 Correct 128 ms 61148 KB Output is correct
26 Correct 9 ms 36184 KB Output is correct
27 Correct 10 ms 35928 KB Output is correct
28 Correct 9 ms 35672 KB Output is correct
29 Correct 8 ms 35540 KB Output is correct
30 Correct 8 ms 35672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 35164 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 7 ms 35164 KB Output is correct
4 Correct 7 ms 35164 KB Output is correct
5 Correct 7 ms 35160 KB Output is correct
6 Correct 7 ms 35160 KB Output is correct
7 Correct 7 ms 35160 KB Output is correct
8 Correct 7 ms 35160 KB Output is correct
9 Correct 6 ms 35164 KB Output is correct
10 Correct 7 ms 35160 KB Output is correct
11 Correct 7 ms 35300 KB Output is correct
12 Correct 7 ms 35292 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35160 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 7 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 7 ms 35292 KB Output is correct
19 Correct 7 ms 35164 KB Output is correct
20 Correct 7 ms 35164 KB Output is correct
21 Correct 7 ms 35296 KB Output is correct
22 Correct 7 ms 35160 KB Output is correct
23 Correct 8 ms 35164 KB Output is correct
24 Correct 7 ms 35416 KB Output is correct
25 Correct 7 ms 35164 KB Output is correct
26 Correct 9 ms 35676 KB Output is correct
27 Correct 10 ms 35604 KB Output is correct
28 Correct 12 ms 35860 KB Output is correct
29 Correct 18 ms 35928 KB Output is correct
30 Correct 9 ms 35672 KB Output is correct
31 Correct 7 ms 35164 KB Output is correct
32 Correct 10 ms 35928 KB Output is correct
33 Correct 7 ms 35160 KB Output is correct
34 Correct 10 ms 35676 KB Output is correct
35 Correct 11 ms 35864 KB Output is correct
36 Correct 9 ms 35676 KB Output is correct
37 Correct 9 ms 35584 KB Output is correct
38 Correct 7 ms 35164 KB Output is correct
39 Correct 9 ms 35684 KB Output is correct
40 Correct 9 ms 35516 KB Output is correct
41 Correct 8 ms 35672 KB Output is correct
42 Correct 9 ms 35676 KB Output is correct
43 Correct 8 ms 35932 KB Output is correct
44 Correct 7 ms 35256 KB Output is correct
45 Correct 10 ms 35676 KB Output is correct
46 Correct 9 ms 35712 KB Output is correct
47 Correct 7 ms 35164 KB Output is correct
48 Correct 9 ms 35676 KB Output is correct
49 Correct 10 ms 35992 KB Output is correct
50 Correct 9 ms 36028 KB Output is correct
51 Correct 11 ms 35676 KB Output is correct
52 Correct 8 ms 35608 KB Output is correct
53 Correct 10 ms 35824 KB Output is correct
54 Correct 10 ms 35676 KB Output is correct
55 Correct 7 ms 35164 KB Output is correct
56 Correct 59 ms 50320 KB Output is correct
57 Correct 99 ms 49964 KB Output is correct
58 Correct 10 ms 35704 KB Output is correct
59 Correct 8 ms 35164 KB Output is correct
60 Correct 7 ms 35356 KB Output is correct
61 Correct 86 ms 49912 KB Output is correct
62 Correct 9 ms 35760 KB Output is correct
63 Correct 117 ms 60040 KB Output is correct
64 Correct 77 ms 49976 KB Output is correct
65 Correct 8 ms 35608 KB Output is correct
66 Correct 87 ms 50888 KB Output is correct
67 Correct 9 ms 35756 KB Output is correct
68 Correct 10 ms 35676 KB Output is correct
69 Correct 68 ms 49984 KB Output is correct
70 Correct 10 ms 35944 KB Output is correct
71 Correct 84 ms 49672 KB Output is correct
72 Correct 120 ms 53272 KB Output is correct
73 Correct 8 ms 35164 KB Output is correct
74 Correct 106 ms 60280 KB Output is correct
75 Correct 95 ms 55004 KB Output is correct
76 Correct 9 ms 35772 KB Output is correct
77 Correct 10 ms 35748 KB Output is correct
78 Correct 59 ms 48844 KB Output is correct
79 Correct 137 ms 56076 KB Output is correct
80 Correct 10 ms 35676 KB Output is correct
81 Correct 9 ms 35676 KB Output is correct
82 Correct 7 ms 35284 KB Output is correct
83 Correct 7 ms 35164 KB Output is correct
84 Correct 8 ms 35548 KB Output is correct
85 Correct 97 ms 51952 KB Output is correct
86 Correct 9 ms 35676 KB Output is correct
87 Correct 89 ms 50292 KB Output is correct
88 Correct 8 ms 35164 KB Output is correct
89 Correct 73 ms 50032 KB Output is correct
90 Correct 104 ms 52240 KB Output is correct
91 Correct 125 ms 57872 KB Output is correct
92 Correct 67 ms 49996 KB Output is correct
93 Correct 8 ms 35672 KB Output is correct
94 Correct 8 ms 35164 KB Output is correct
95 Correct 106 ms 56800 KB Output is correct
96 Correct 138 ms 63056 KB Output is correct
97 Correct 10 ms 35672 KB Output is correct
98 Correct 7 ms 35164 KB Output is correct
99 Correct 93 ms 52324 KB Output is correct
100 Correct 8 ms 35676 KB Output is correct
101 Correct 92 ms 50960 KB Output is correct
102 Correct 128 ms 61148 KB Output is correct
103 Correct 9 ms 36184 KB Output is correct
104 Correct 10 ms 35928 KB Output is correct
105 Correct 9 ms 35672 KB Output is correct
106 Correct 8 ms 35540 KB Output is correct
107 Correct 8 ms 35672 KB Output is correct
108 Correct 509 ms 110884 KB Output is correct
109 Correct 763 ms 125912 KB Output is correct
110 Correct 873 ms 141764 KB Output is correct
111 Correct 1201 ms 170744 KB Output is correct
112 Correct 1084 ms 147796 KB Output is correct
113 Correct 824 ms 135476 KB Output is correct
114 Correct 631 ms 100508 KB Output is correct
115 Correct 659 ms 100556 KB Output is correct
116 Correct 729 ms 105812 KB Output is correct
117 Correct 1150 ms 140096 KB Output is correct
118 Correct 576 ms 101956 KB Output is correct
119 Correct 1085 ms 140116 KB Output is correct
120 Correct 1272 ms 159672 KB Output is correct
121 Correct 1154 ms 140040 KB Output is correct
122 Correct 837 ms 114224 KB Output is correct
123 Correct 919 ms 141628 KB Output is correct
124 Correct 1090 ms 130740 KB Output is correct