답안 #1048932

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1048932 2024-08-08T11:21:15 Z 1L1YA Mergers (JOI19_mergers) C++17
100 / 100
436 ms 122548 KB
//1L1YA

#include<bits/stdc++.h>
using namespace std;

#pragma GCC optimize ("O3,unrool-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

typedef long long       ll;
typedef pair<ll,ll>     pll;
typedef pair<int,int>   pii;

#define Pb              push_back
#define F               first
#define S               second
#define endl            '\n'
#define sep             ' '
#define all(x)          x.begin(),x.end()
#define al(x,n)         x+1,x+n+1
#define SZ(x)           ((int)x.size())
#define lc              (id<<1)
#define rc              (lc|1)
#define mid             (l+r>>1)
#define dokme(x)        cout << x << endl, exit(0)
#define sik(x)          cout << x << endl;continue;
#define FastIO          ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define FileIO          freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

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

const ll oo=1e18;
const int mod=1e9+7;
const int inf=1e9+111;
const int N=5e5+11;
const int lg=20;

int n,k,ans,a[N],h[N],pr[N],cnt[N],par[lg][N];
vector<int> vc[N],adj[N];
vector<pii> E;
bool mark[N];

void dfs(int v,int p=0){
	for(int u: adj[v])
		if(u!=p){
			h[u]=h[v]+1;
			par[0][u]=v;
			dfs(u,v);
		}
}

int jump(int v,int k){
	for(int i=0;i<lg;i++)
		if(k>>i & 1)
			v=par[i][v];
	return v;
}

int lca(int u,int v){
	if(h[u]>h[v])	swap(u,v);
	v=jump(v,h[v]-h[u]);
	if(u==v)	return v;
	for(int i=lg-1;~i;i--)
		if(par[i][u]!=par[i][v])
			u=par[i][u],v=par[i][v];
	return par[0][v];
}

void DFS(int v,int p=0){
	for(int u: adj[v])
		if(u!=p){
			DFS(u,v);
			cnt[v]+=cnt[u];
		}
	if(!cnt[v])	mark[v]=1;
}

void dFs(int v,int p=0){
	for(int u: adj[v])
		if(u!=p){
			if(!mark[u])	pr[u]=pr[v];
			dFs(u,v);
		}
}

int main(){
	FastIO

	cin >> n >> k;
	for(int i=1;i<n;i++){
		int u,v;
		cin >> u >> v;
		adj[u].Pb(v);
		adj[v].Pb(u);
		E.Pb({u,v});
	}
	for(int i=1;i<=n;i++)	cin >> a[i],vc[a[i]].Pb(i);
	dfs(1);
	for(int i=1;i<lg;i++)
		for(int v=1;v<=n;v++)
			par[i][v]=par[i-1][par[i-1][v]];
	for(int i=1;i<=k;i++){
		int v=vc[i][0];
		for(int u: vc[i])	v=lca(u,v),cnt[u]++;
		cnt[v]-=SZ(vc[i]);
	}
	DFS(1);
	for(int i=1;i<=n;i++)	pr[i]=i;
	dFs(1);
	fill(cnt,cnt+n+1,0);
	for(auto [u,v]: E)
		if(pr[u]!=pr[v]){
			cnt[pr[u]]++;
			cnt[pr[v]]++;
			if(cnt[pr[u]]==1)	ans++;
			else if(cnt[pr[u]]==2)	ans--;
			if(cnt[pr[v]]==1)	ans++;
			else if(cnt[pr[v]]==2)	ans--;	
		}
	cout << (ans+1)/2 << endl;

	return 0;
}

Compilation message

mergers.cpp:6:40: warning: bad option '-funrool-loops' to pragma 'optimize' [-Wpragmas]
    6 | #pragma GCC optimize ("O3,unrool-loops")
      |                                        ^
mergers.cpp:42:23: warning: bad option '-funrool-loops' to attribute 'optimize' [-Wattributes]
   42 | void dfs(int v,int p=0){
      |                       ^
mergers.cpp:51:21: warning: bad option '-funrool-loops' to attribute 'optimize' [-Wattributes]
   51 | int jump(int v,int k){
      |                     ^
mergers.cpp:58:20: warning: bad option '-funrool-loops' to attribute 'optimize' [-Wattributes]
   58 | int lca(int u,int v){
      |                    ^
mergers.cpp:68:23: warning: bad option '-funrool-loops' to attribute 'optimize' [-Wattributes]
   68 | void DFS(int v,int p=0){
      |                       ^
mergers.cpp:77:23: warning: bad option '-funrool-loops' to attribute 'optimize' [-Wattributes]
   77 | void dFs(int v,int p=0){
      |                       ^
mergers.cpp:85:10: warning: bad option '-funrool-loops' to attribute 'optimize' [-Wattributes]
   85 | int main(){
      |          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 68188 KB Output is correct
2 Correct 7 ms 68136 KB Output is correct
3 Correct 6 ms 68188 KB Output is correct
4 Correct 8 ms 68188 KB Output is correct
5 Correct 6 ms 68120 KB Output is correct
6 Correct 7 ms 68048 KB Output is correct
7 Correct 6 ms 68188 KB Output is correct
8 Correct 6 ms 68188 KB Output is correct
9 Correct 7 ms 68188 KB Output is correct
10 Correct 8 ms 68188 KB Output is correct
11 Correct 7 ms 68136 KB Output is correct
12 Correct 7 ms 68184 KB Output is correct
13 Correct 7 ms 68188 KB Output is correct
14 Correct 6 ms 68188 KB Output is correct
15 Correct 6 ms 68188 KB Output is correct
16 Correct 7 ms 68188 KB Output is correct
17 Correct 6 ms 68188 KB Output is correct
18 Correct 6 ms 68200 KB Output is correct
19 Correct 7 ms 68184 KB Output is correct
20 Correct 6 ms 68132 KB Output is correct
21 Correct 6 ms 68196 KB Output is correct
22 Correct 7 ms 68188 KB Output is correct
23 Correct 7 ms 68188 KB Output is correct
24 Correct 7 ms 68188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 68188 KB Output is correct
2 Correct 7 ms 68136 KB Output is correct
3 Correct 6 ms 68188 KB Output is correct
4 Correct 8 ms 68188 KB Output is correct
5 Correct 6 ms 68120 KB Output is correct
6 Correct 7 ms 68048 KB Output is correct
7 Correct 6 ms 68188 KB Output is correct
8 Correct 6 ms 68188 KB Output is correct
9 Correct 7 ms 68188 KB Output is correct
10 Correct 8 ms 68188 KB Output is correct
11 Correct 7 ms 68136 KB Output is correct
12 Correct 7 ms 68184 KB Output is correct
13 Correct 7 ms 68188 KB Output is correct
14 Correct 6 ms 68188 KB Output is correct
15 Correct 6 ms 68188 KB Output is correct
16 Correct 7 ms 68188 KB Output is correct
17 Correct 6 ms 68188 KB Output is correct
18 Correct 6 ms 68200 KB Output is correct
19 Correct 7 ms 68184 KB Output is correct
20 Correct 6 ms 68132 KB Output is correct
21 Correct 6 ms 68196 KB Output is correct
22 Correct 7 ms 68188 KB Output is correct
23 Correct 7 ms 68188 KB Output is correct
24 Correct 7 ms 68188 KB Output is correct
25 Correct 6 ms 68184 KB Output is correct
26 Correct 7 ms 68400 KB Output is correct
27 Correct 8 ms 68188 KB Output is correct
28 Correct 8 ms 68312 KB Output is correct
29 Correct 8 ms 68188 KB Output is correct
30 Correct 8 ms 68188 KB Output is correct
31 Correct 7 ms 68140 KB Output is correct
32 Correct 7 ms 68444 KB Output is correct
33 Correct 7 ms 68188 KB Output is correct
34 Correct 7 ms 68352 KB Output is correct
35 Correct 8 ms 68188 KB Output is correct
36 Correct 7 ms 68328 KB Output is correct
37 Correct 7 ms 68188 KB Output is correct
38 Correct 6 ms 68188 KB Output is correct
39 Correct 8 ms 68188 KB Output is correct
40 Correct 7 ms 68308 KB Output is correct
41 Correct 7 ms 68364 KB Output is correct
42 Correct 8 ms 68188 KB Output is correct
43 Correct 8 ms 68444 KB Output is correct
44 Correct 7 ms 68184 KB Output is correct
45 Correct 9 ms 68440 KB Output is correct
46 Correct 7 ms 68384 KB Output is correct
47 Correct 6 ms 68160 KB Output is correct
48 Correct 7 ms 68356 KB Output is correct
49 Correct 8 ms 68444 KB Output is correct
50 Correct 9 ms 68444 KB Output is correct
51 Correct 9 ms 68188 KB Output is correct
52 Correct 7 ms 68188 KB Output is correct
53 Correct 7 ms 68188 KB Output is correct
54 Correct 7 ms 68340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 68188 KB Output is correct
2 Correct 7 ms 68136 KB Output is correct
3 Correct 6 ms 68188 KB Output is correct
4 Correct 8 ms 68188 KB Output is correct
5 Correct 6 ms 68120 KB Output is correct
6 Correct 7 ms 68048 KB Output is correct
7 Correct 6 ms 68188 KB Output is correct
8 Correct 6 ms 68188 KB Output is correct
9 Correct 7 ms 68188 KB Output is correct
10 Correct 8 ms 68188 KB Output is correct
11 Correct 7 ms 68136 KB Output is correct
12 Correct 7 ms 68184 KB Output is correct
13 Correct 7 ms 68188 KB Output is correct
14 Correct 6 ms 68188 KB Output is correct
15 Correct 6 ms 68188 KB Output is correct
16 Correct 7 ms 68188 KB Output is correct
17 Correct 6 ms 68188 KB Output is correct
18 Correct 6 ms 68200 KB Output is correct
19 Correct 7 ms 68184 KB Output is correct
20 Correct 6 ms 68132 KB Output is correct
21 Correct 6 ms 68196 KB Output is correct
22 Correct 7 ms 68188 KB Output is correct
23 Correct 7 ms 68188 KB Output is correct
24 Correct 7 ms 68188 KB Output is correct
25 Correct 7 ms 68072 KB Output is correct
26 Correct 32 ms 73828 KB Output is correct
27 Correct 46 ms 73516 KB Output is correct
28 Correct 8 ms 68188 KB Output is correct
29 Correct 6 ms 68188 KB Output is correct
30 Correct 7 ms 68188 KB Output is correct
31 Correct 43 ms 73432 KB Output is correct
32 Correct 8 ms 68188 KB Output is correct
33 Correct 56 ms 76504 KB Output is correct
34 Correct 42 ms 73592 KB Output is correct
35 Correct 7 ms 68188 KB Output is correct
36 Correct 53 ms 73684 KB Output is correct
37 Correct 8 ms 68188 KB Output is correct
38 Correct 7 ms 68188 KB Output is correct
39 Correct 32 ms 73676 KB Output is correct
40 Correct 8 ms 68444 KB Output is correct
41 Correct 42 ms 73660 KB Output is correct
42 Correct 51 ms 74692 KB Output is correct
43 Correct 7 ms 68184 KB Output is correct
44 Correct 51 ms 76808 KB Output is correct
45 Correct 49 ms 74840 KB Output is correct
46 Correct 7 ms 68188 KB Output is correct
47 Correct 7 ms 68188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 73736 KB Output is correct
2 Correct 43 ms 76732 KB Output is correct
3 Correct 8 ms 68228 KB Output is correct
4 Correct 8 ms 68188 KB Output is correct
5 Correct 7 ms 68188 KB Output is correct
6 Correct 6 ms 68188 KB Output is correct
7 Correct 7 ms 68188 KB Output is correct
8 Correct 53 ms 74732 KB Output is correct
9 Correct 8 ms 68188 KB Output is correct
10 Correct 43 ms 73576 KB Output is correct
11 Correct 7 ms 68188 KB Output is correct
12 Correct 53 ms 73556 KB Output is correct
13 Correct 53 ms 74692 KB Output is correct
14 Correct 49 ms 76400 KB Output is correct
15 Correct 33 ms 73676 KB Output is correct
16 Correct 8 ms 68188 KB Output is correct
17 Correct 7 ms 68188 KB Output is correct
18 Correct 48 ms 76488 KB Output is correct
19 Correct 56 ms 77948 KB Output is correct
20 Correct 8 ms 68184 KB Output is correct
21 Correct 7 ms 68188 KB Output is correct
22 Correct 39 ms 75072 KB Output is correct
23 Correct 8 ms 68188 KB Output is correct
24 Correct 53 ms 73928 KB Output is correct
25 Correct 54 ms 77548 KB Output is correct
26 Correct 8 ms 68440 KB Output is correct
27 Correct 7 ms 68444 KB Output is correct
28 Correct 7 ms 68188 KB Output is correct
29 Correct 7 ms 68188 KB Output is correct
30 Correct 7 ms 68188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 68188 KB Output is correct
2 Correct 7 ms 68136 KB Output is correct
3 Correct 6 ms 68188 KB Output is correct
4 Correct 8 ms 68188 KB Output is correct
5 Correct 6 ms 68120 KB Output is correct
6 Correct 7 ms 68048 KB Output is correct
7 Correct 6 ms 68188 KB Output is correct
8 Correct 6 ms 68188 KB Output is correct
9 Correct 7 ms 68188 KB Output is correct
10 Correct 8 ms 68188 KB Output is correct
11 Correct 7 ms 68136 KB Output is correct
12 Correct 7 ms 68184 KB Output is correct
13 Correct 7 ms 68188 KB Output is correct
14 Correct 6 ms 68188 KB Output is correct
15 Correct 6 ms 68188 KB Output is correct
16 Correct 7 ms 68188 KB Output is correct
17 Correct 6 ms 68188 KB Output is correct
18 Correct 6 ms 68200 KB Output is correct
19 Correct 7 ms 68184 KB Output is correct
20 Correct 6 ms 68132 KB Output is correct
21 Correct 6 ms 68196 KB Output is correct
22 Correct 7 ms 68188 KB Output is correct
23 Correct 7 ms 68188 KB Output is correct
24 Correct 7 ms 68188 KB Output is correct
25 Correct 6 ms 68184 KB Output is correct
26 Correct 7 ms 68400 KB Output is correct
27 Correct 8 ms 68188 KB Output is correct
28 Correct 8 ms 68312 KB Output is correct
29 Correct 8 ms 68188 KB Output is correct
30 Correct 8 ms 68188 KB Output is correct
31 Correct 7 ms 68140 KB Output is correct
32 Correct 7 ms 68444 KB Output is correct
33 Correct 7 ms 68188 KB Output is correct
34 Correct 7 ms 68352 KB Output is correct
35 Correct 8 ms 68188 KB Output is correct
36 Correct 7 ms 68328 KB Output is correct
37 Correct 7 ms 68188 KB Output is correct
38 Correct 6 ms 68188 KB Output is correct
39 Correct 8 ms 68188 KB Output is correct
40 Correct 7 ms 68308 KB Output is correct
41 Correct 7 ms 68364 KB Output is correct
42 Correct 8 ms 68188 KB Output is correct
43 Correct 8 ms 68444 KB Output is correct
44 Correct 7 ms 68184 KB Output is correct
45 Correct 9 ms 68440 KB Output is correct
46 Correct 7 ms 68384 KB Output is correct
47 Correct 6 ms 68160 KB Output is correct
48 Correct 7 ms 68356 KB Output is correct
49 Correct 8 ms 68444 KB Output is correct
50 Correct 9 ms 68444 KB Output is correct
51 Correct 9 ms 68188 KB Output is correct
52 Correct 7 ms 68188 KB Output is correct
53 Correct 7 ms 68188 KB Output is correct
54 Correct 7 ms 68340 KB Output is correct
55 Correct 7 ms 68072 KB Output is correct
56 Correct 32 ms 73828 KB Output is correct
57 Correct 46 ms 73516 KB Output is correct
58 Correct 8 ms 68188 KB Output is correct
59 Correct 6 ms 68188 KB Output is correct
60 Correct 7 ms 68188 KB Output is correct
61 Correct 43 ms 73432 KB Output is correct
62 Correct 8 ms 68188 KB Output is correct
63 Correct 56 ms 76504 KB Output is correct
64 Correct 42 ms 73592 KB Output is correct
65 Correct 7 ms 68188 KB Output is correct
66 Correct 53 ms 73684 KB Output is correct
67 Correct 8 ms 68188 KB Output is correct
68 Correct 7 ms 68188 KB Output is correct
69 Correct 32 ms 73676 KB Output is correct
70 Correct 8 ms 68444 KB Output is correct
71 Correct 42 ms 73660 KB Output is correct
72 Correct 51 ms 74692 KB Output is correct
73 Correct 7 ms 68184 KB Output is correct
74 Correct 51 ms 76808 KB Output is correct
75 Correct 49 ms 74840 KB Output is correct
76 Correct 7 ms 68188 KB Output is correct
77 Correct 7 ms 68188 KB Output is correct
78 Correct 35 ms 73736 KB Output is correct
79 Correct 43 ms 76732 KB Output is correct
80 Correct 8 ms 68228 KB Output is correct
81 Correct 8 ms 68188 KB Output is correct
82 Correct 7 ms 68188 KB Output is correct
83 Correct 6 ms 68188 KB Output is correct
84 Correct 7 ms 68188 KB Output is correct
85 Correct 53 ms 74732 KB Output is correct
86 Correct 8 ms 68188 KB Output is correct
87 Correct 43 ms 73576 KB Output is correct
88 Correct 7 ms 68188 KB Output is correct
89 Correct 53 ms 73556 KB Output is correct
90 Correct 53 ms 74692 KB Output is correct
91 Correct 49 ms 76400 KB Output is correct
92 Correct 33 ms 73676 KB Output is correct
93 Correct 8 ms 68188 KB Output is correct
94 Correct 7 ms 68188 KB Output is correct
95 Correct 48 ms 76488 KB Output is correct
96 Correct 56 ms 77948 KB Output is correct
97 Correct 8 ms 68184 KB Output is correct
98 Correct 7 ms 68188 KB Output is correct
99 Correct 39 ms 75072 KB Output is correct
100 Correct 8 ms 68188 KB Output is correct
101 Correct 53 ms 73928 KB Output is correct
102 Correct 54 ms 77548 KB Output is correct
103 Correct 8 ms 68440 KB Output is correct
104 Correct 7 ms 68444 KB Output is correct
105 Correct 7 ms 68188 KB Output is correct
106 Correct 7 ms 68188 KB Output is correct
107 Correct 7 ms 68188 KB Output is correct
108 Correct 226 ms 102172 KB Output is correct
109 Correct 387 ms 104500 KB Output is correct
110 Correct 436 ms 109496 KB Output is correct
111 Correct 428 ms 119412 KB Output is correct
112 Correct 386 ms 115264 KB Output is correct
113 Correct 271 ms 110008 KB Output is correct
114 Correct 277 ms 96900 KB Output is correct
115 Correct 280 ms 97556 KB Output is correct
116 Correct 318 ms 99128 KB Output is correct
117 Correct 310 ms 117248 KB Output is correct
118 Correct 280 ms 102328 KB Output is correct
119 Correct 314 ms 116916 KB Output is correct
120 Correct 349 ms 122548 KB Output is correct
121 Correct 335 ms 116920 KB Output is correct
122 Correct 367 ms 110532 KB Output is correct
123 Correct 221 ms 118504 KB Output is correct
124 Correct 431 ms 115676 KB Output is correct