답안 #563757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563757 2022-05-18T05:24:27 Z errorgorn Mergers (JOI19_mergers) C++17
100 / 100
957 ms 107320 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'

#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound

#define rep(x,s,e) for (int x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct UFDS{
	int p[500005];
	
	UFDS(){
		rep(x,0,500005) p[x]=x;
	}
	
	int par(int i){
		if (p[i]==i) return p[i];
		else return p[i]=par(p[i]);
	}
	
	void unions(int i,int j){
		i=par(i),j=par(j);
		p[j]=i;
	}
} ufds;

int n,k;
vector<int> al[500005];
vector<ii> edges;
vector<int> pos[500005];

int d[500005];
int pp[500005];

void dfs(int i,int p){
	for (auto it:al[i]){
		if (it==p) continue;
		
		d[it]=d[i]+1;
		pp[it]=i;
		
		dfs(it,i);
	}
}

int deg[500005];

signed main(){
	cin>>n>>k;
	
	int a,b;
	rep(x,1,n){
		cin>>a>>b;
		al[a].pub(b);
		al[b].pub(a);
		edges.pub({a,b});
	}
	
	rep(x,1,n+1){
		cin>>a;
		pos[a].pub(x);
	}
	
	dfs(1,-1);
	
	rep(x,1,k+1) rep(y,1,sz(pos[x])){
		int a=pos[x][0],b=pos[x][y];
		//cout<<a<<" "<<b<<endl;
		
		while (a!=b){
			if (d[a]<d[b]) swap(a,b);
			ufds.unions(pp[a],a);
			a=ufds.par(a);
		}
	}
	
	for (auto [a,b]:edges){
		a=ufds.par(a),b=ufds.par(b);
		if (a!=b) deg[a]++,deg[b]++;
	}
	
	int ans=0;
	rep(x,1,n+1) ans+=deg[x]==1;
	
	cout<<(ans+1)/2<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 27604 KB Output is correct
2 Correct 15 ms 27700 KB Output is correct
3 Correct 15 ms 27696 KB Output is correct
4 Correct 14 ms 27640 KB Output is correct
5 Correct 14 ms 27604 KB Output is correct
6 Correct 15 ms 27708 KB Output is correct
7 Correct 15 ms 27604 KB Output is correct
8 Correct 14 ms 27632 KB Output is correct
9 Correct 14 ms 27724 KB Output is correct
10 Correct 15 ms 27604 KB Output is correct
11 Correct 18 ms 27616 KB Output is correct
12 Correct 18 ms 27720 KB Output is correct
13 Correct 15 ms 27692 KB Output is correct
14 Correct 15 ms 27732 KB Output is correct
15 Correct 15 ms 27708 KB Output is correct
16 Correct 14 ms 27732 KB Output is correct
17 Correct 14 ms 27604 KB Output is correct
18 Correct 15 ms 27732 KB Output is correct
19 Correct 14 ms 27732 KB Output is correct
20 Correct 15 ms 27816 KB Output is correct
21 Correct 15 ms 27668 KB Output is correct
22 Correct 14 ms 27712 KB Output is correct
23 Correct 15 ms 27604 KB Output is correct
24 Correct 13 ms 27604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 27604 KB Output is correct
2 Correct 15 ms 27700 KB Output is correct
3 Correct 15 ms 27696 KB Output is correct
4 Correct 14 ms 27640 KB Output is correct
5 Correct 14 ms 27604 KB Output is correct
6 Correct 15 ms 27708 KB Output is correct
7 Correct 15 ms 27604 KB Output is correct
8 Correct 14 ms 27632 KB Output is correct
9 Correct 14 ms 27724 KB Output is correct
10 Correct 15 ms 27604 KB Output is correct
11 Correct 18 ms 27616 KB Output is correct
12 Correct 18 ms 27720 KB Output is correct
13 Correct 15 ms 27692 KB Output is correct
14 Correct 15 ms 27732 KB Output is correct
15 Correct 15 ms 27708 KB Output is correct
16 Correct 14 ms 27732 KB Output is correct
17 Correct 14 ms 27604 KB Output is correct
18 Correct 15 ms 27732 KB Output is correct
19 Correct 14 ms 27732 KB Output is correct
20 Correct 15 ms 27816 KB Output is correct
21 Correct 15 ms 27668 KB Output is correct
22 Correct 14 ms 27712 KB Output is correct
23 Correct 15 ms 27604 KB Output is correct
24 Correct 13 ms 27604 KB Output is correct
25 Correct 15 ms 27636 KB Output is correct
26 Correct 17 ms 27968 KB Output is correct
27 Correct 19 ms 28024 KB Output is correct
28 Correct 19 ms 28064 KB Output is correct
29 Correct 18 ms 28100 KB Output is correct
30 Correct 18 ms 27976 KB Output is correct
31 Correct 14 ms 27708 KB Output is correct
32 Correct 17 ms 28108 KB Output is correct
33 Correct 14 ms 27668 KB Output is correct
34 Correct 16 ms 27988 KB Output is correct
35 Correct 18 ms 27988 KB Output is correct
36 Correct 17 ms 28004 KB Output is correct
37 Correct 18 ms 27988 KB Output is correct
38 Correct 14 ms 27732 KB Output is correct
39 Correct 18 ms 27988 KB Output is correct
40 Correct 16 ms 27988 KB Output is correct
41 Correct 18 ms 27980 KB Output is correct
42 Correct 19 ms 28060 KB Output is correct
43 Correct 18 ms 28116 KB Output is correct
44 Correct 15 ms 27640 KB Output is correct
45 Correct 17 ms 28204 KB Output is correct
46 Correct 17 ms 27988 KB Output is correct
47 Correct 15 ms 27736 KB Output is correct
48 Correct 17 ms 27972 KB Output is correct
49 Correct 17 ms 28048 KB Output is correct
50 Correct 17 ms 28104 KB Output is correct
51 Correct 18 ms 27976 KB Output is correct
52 Correct 17 ms 28040 KB Output is correct
53 Correct 18 ms 28088 KB Output is correct
54 Correct 17 ms 27936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 27604 KB Output is correct
2 Correct 15 ms 27700 KB Output is correct
3 Correct 15 ms 27696 KB Output is correct
4 Correct 14 ms 27640 KB Output is correct
5 Correct 14 ms 27604 KB Output is correct
6 Correct 15 ms 27708 KB Output is correct
7 Correct 15 ms 27604 KB Output is correct
8 Correct 14 ms 27632 KB Output is correct
9 Correct 14 ms 27724 KB Output is correct
10 Correct 15 ms 27604 KB Output is correct
11 Correct 18 ms 27616 KB Output is correct
12 Correct 18 ms 27720 KB Output is correct
13 Correct 15 ms 27692 KB Output is correct
14 Correct 15 ms 27732 KB Output is correct
15 Correct 15 ms 27708 KB Output is correct
16 Correct 14 ms 27732 KB Output is correct
17 Correct 14 ms 27604 KB Output is correct
18 Correct 15 ms 27732 KB Output is correct
19 Correct 14 ms 27732 KB Output is correct
20 Correct 15 ms 27816 KB Output is correct
21 Correct 15 ms 27668 KB Output is correct
22 Correct 14 ms 27712 KB Output is correct
23 Correct 15 ms 27604 KB Output is correct
24 Correct 13 ms 27604 KB Output is correct
25 Correct 15 ms 27732 KB Output is correct
26 Correct 100 ms 37276 KB Output is correct
27 Correct 125 ms 37176 KB Output is correct
28 Correct 17 ms 27988 KB Output is correct
29 Correct 15 ms 27732 KB Output is correct
30 Correct 15 ms 27732 KB Output is correct
31 Correct 116 ms 37140 KB Output is correct
32 Correct 18 ms 27884 KB Output is correct
33 Correct 126 ms 42168 KB Output is correct
34 Correct 118 ms 37020 KB Output is correct
35 Correct 17 ms 27920 KB Output is correct
36 Correct 121 ms 37352 KB Output is correct
37 Correct 17 ms 27992 KB Output is correct
38 Correct 18 ms 28008 KB Output is correct
39 Correct 104 ms 37224 KB Output is correct
40 Correct 17 ms 28116 KB Output is correct
41 Correct 112 ms 37728 KB Output is correct
42 Correct 131 ms 38404 KB Output is correct
43 Correct 15 ms 27732 KB Output is correct
44 Correct 124 ms 43168 KB Output is correct
45 Correct 120 ms 40628 KB Output is correct
46 Correct 17 ms 27988 KB Output is correct
47 Correct 18 ms 27988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 37248 KB Output is correct
2 Correct 122 ms 40496 KB Output is correct
3 Correct 18 ms 27972 KB Output is correct
4 Correct 17 ms 27964 KB Output is correct
5 Correct 14 ms 27732 KB Output is correct
6 Correct 16 ms 27636 KB Output is correct
7 Correct 19 ms 27988 KB Output is correct
8 Correct 133 ms 38828 KB Output is correct
9 Correct 17 ms 28012 KB Output is correct
10 Correct 142 ms 38060 KB Output is correct
11 Correct 15 ms 27732 KB Output is correct
12 Correct 122 ms 37436 KB Output is correct
13 Correct 140 ms 38724 KB Output is correct
14 Correct 131 ms 40248 KB Output is correct
15 Correct 120 ms 37192 KB Output is correct
16 Correct 17 ms 27972 KB Output is correct
17 Correct 15 ms 27732 KB Output is correct
18 Correct 121 ms 40112 KB Output is correct
19 Correct 138 ms 42792 KB Output is correct
20 Correct 17 ms 27936 KB Output is correct
21 Correct 14 ms 27700 KB Output is correct
22 Correct 116 ms 38996 KB Output is correct
23 Correct 17 ms 27968 KB Output is correct
24 Correct 130 ms 38176 KB Output is correct
25 Correct 140 ms 42472 KB Output is correct
26 Correct 17 ms 28084 KB Output is correct
27 Correct 17 ms 28124 KB Output is correct
28 Correct 17 ms 27980 KB Output is correct
29 Correct 16 ms 27988 KB Output is correct
30 Correct 17 ms 27988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 27604 KB Output is correct
2 Correct 15 ms 27700 KB Output is correct
3 Correct 15 ms 27696 KB Output is correct
4 Correct 14 ms 27640 KB Output is correct
5 Correct 14 ms 27604 KB Output is correct
6 Correct 15 ms 27708 KB Output is correct
7 Correct 15 ms 27604 KB Output is correct
8 Correct 14 ms 27632 KB Output is correct
9 Correct 14 ms 27724 KB Output is correct
10 Correct 15 ms 27604 KB Output is correct
11 Correct 18 ms 27616 KB Output is correct
12 Correct 18 ms 27720 KB Output is correct
13 Correct 15 ms 27692 KB Output is correct
14 Correct 15 ms 27732 KB Output is correct
15 Correct 15 ms 27708 KB Output is correct
16 Correct 14 ms 27732 KB Output is correct
17 Correct 14 ms 27604 KB Output is correct
18 Correct 15 ms 27732 KB Output is correct
19 Correct 14 ms 27732 KB Output is correct
20 Correct 15 ms 27816 KB Output is correct
21 Correct 15 ms 27668 KB Output is correct
22 Correct 14 ms 27712 KB Output is correct
23 Correct 15 ms 27604 KB Output is correct
24 Correct 13 ms 27604 KB Output is correct
25 Correct 15 ms 27636 KB Output is correct
26 Correct 17 ms 27968 KB Output is correct
27 Correct 19 ms 28024 KB Output is correct
28 Correct 19 ms 28064 KB Output is correct
29 Correct 18 ms 28100 KB Output is correct
30 Correct 18 ms 27976 KB Output is correct
31 Correct 14 ms 27708 KB Output is correct
32 Correct 17 ms 28108 KB Output is correct
33 Correct 14 ms 27668 KB Output is correct
34 Correct 16 ms 27988 KB Output is correct
35 Correct 18 ms 27988 KB Output is correct
36 Correct 17 ms 28004 KB Output is correct
37 Correct 18 ms 27988 KB Output is correct
38 Correct 14 ms 27732 KB Output is correct
39 Correct 18 ms 27988 KB Output is correct
40 Correct 16 ms 27988 KB Output is correct
41 Correct 18 ms 27980 KB Output is correct
42 Correct 19 ms 28060 KB Output is correct
43 Correct 18 ms 28116 KB Output is correct
44 Correct 15 ms 27640 KB Output is correct
45 Correct 17 ms 28204 KB Output is correct
46 Correct 17 ms 27988 KB Output is correct
47 Correct 15 ms 27736 KB Output is correct
48 Correct 17 ms 27972 KB Output is correct
49 Correct 17 ms 28048 KB Output is correct
50 Correct 17 ms 28104 KB Output is correct
51 Correct 18 ms 27976 KB Output is correct
52 Correct 17 ms 28040 KB Output is correct
53 Correct 18 ms 28088 KB Output is correct
54 Correct 17 ms 27936 KB Output is correct
55 Correct 15 ms 27732 KB Output is correct
56 Correct 100 ms 37276 KB Output is correct
57 Correct 125 ms 37176 KB Output is correct
58 Correct 17 ms 27988 KB Output is correct
59 Correct 15 ms 27732 KB Output is correct
60 Correct 15 ms 27732 KB Output is correct
61 Correct 116 ms 37140 KB Output is correct
62 Correct 18 ms 27884 KB Output is correct
63 Correct 126 ms 42168 KB Output is correct
64 Correct 118 ms 37020 KB Output is correct
65 Correct 17 ms 27920 KB Output is correct
66 Correct 121 ms 37352 KB Output is correct
67 Correct 17 ms 27992 KB Output is correct
68 Correct 18 ms 28008 KB Output is correct
69 Correct 104 ms 37224 KB Output is correct
70 Correct 17 ms 28116 KB Output is correct
71 Correct 112 ms 37728 KB Output is correct
72 Correct 131 ms 38404 KB Output is correct
73 Correct 15 ms 27732 KB Output is correct
74 Correct 124 ms 43168 KB Output is correct
75 Correct 120 ms 40628 KB Output is correct
76 Correct 17 ms 27988 KB Output is correct
77 Correct 18 ms 27988 KB Output is correct
78 Correct 103 ms 37248 KB Output is correct
79 Correct 122 ms 40496 KB Output is correct
80 Correct 18 ms 27972 KB Output is correct
81 Correct 17 ms 27964 KB Output is correct
82 Correct 14 ms 27732 KB Output is correct
83 Correct 16 ms 27636 KB Output is correct
84 Correct 19 ms 27988 KB Output is correct
85 Correct 133 ms 38828 KB Output is correct
86 Correct 17 ms 28012 KB Output is correct
87 Correct 142 ms 38060 KB Output is correct
88 Correct 15 ms 27732 KB Output is correct
89 Correct 122 ms 37436 KB Output is correct
90 Correct 140 ms 38724 KB Output is correct
91 Correct 131 ms 40248 KB Output is correct
92 Correct 120 ms 37192 KB Output is correct
93 Correct 17 ms 27972 KB Output is correct
94 Correct 15 ms 27732 KB Output is correct
95 Correct 121 ms 40112 KB Output is correct
96 Correct 138 ms 42792 KB Output is correct
97 Correct 17 ms 27936 KB Output is correct
98 Correct 14 ms 27700 KB Output is correct
99 Correct 116 ms 38996 KB Output is correct
100 Correct 17 ms 27968 KB Output is correct
101 Correct 130 ms 38176 KB Output is correct
102 Correct 140 ms 42472 KB Output is correct
103 Correct 17 ms 28084 KB Output is correct
104 Correct 17 ms 28124 KB Output is correct
105 Correct 17 ms 27980 KB Output is correct
106 Correct 16 ms 27988 KB Output is correct
107 Correct 17 ms 27988 KB Output is correct
108 Correct 715 ms 85364 KB Output is correct
109 Correct 799 ms 88880 KB Output is correct
110 Correct 812 ms 98420 KB Output is correct
111 Correct 937 ms 107320 KB Output is correct
112 Correct 934 ms 103736 KB Output is correct
113 Correct 790 ms 91000 KB Output is correct
114 Correct 698 ms 75120 KB Output is correct
115 Correct 692 ms 77644 KB Output is correct
116 Correct 892 ms 81772 KB Output is correct
117 Correct 859 ms 91556 KB Output is correct
118 Correct 740 ms 77972 KB Output is correct
119 Correct 865 ms 91568 KB Output is correct
120 Correct 957 ms 104488 KB Output is correct
121 Correct 859 ms 91596 KB Output is correct
122 Correct 903 ms 86028 KB Output is correct
123 Correct 710 ms 92516 KB Output is correct
124 Correct 903 ms 87668 KB Output is correct