답안 #203565

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203565 2020-02-21T10:33:19 Z kig9981 Unique Cities (JOI19_ho_t5) C++17
100 / 100
1861 ms 110348 KB
#include <bits/stdc++.h>
 
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
 
using namespace std;

vector<int> adj[200000], Q[400000], tree[400000];
int res, N, hld[200000], W[200000], depth[200000], parent[200000], P[400000][19], dist[400000], sdist[400000], ans[200000], C[200000], CC[400000], cnt[200001];

void dfs(int c)
{
	W[c]=1;
	for(auto n: adj[c]) if(W[n]==0) {
		parent[n]=c;
		depth[n]=depth[c]+1;
		dfs(n);
		W[c]+=W[n];
	}
}

void dfs2(int c)
{
	for(auto n: adj[c]) if(W[n]<W[c]) {
		hld[n]=2*W[n]>=W[c] ? hld[c]:n;
		dfs2(n);
	}
}

int LCA(int a, int b)
{
	while(hld[a]!=hld[b]) {
		if(depth[hld[a]]<depth[hld[b]]) swap(a,b);
		a=parent[hld[a]];
	}
	return depth[a]<depth[b] ? a:b;
}

int get_dist(int a, int b)
{
	b=b<N ? b:parent[b-N];
	return depth[a]+depth[b]-2*depth[LCA(a,b)];
}

void solve(int c)
{
	for(auto n: adj[c]) if(W[n]<W[c]) {
		solve(n);
		if(dist[c]<dist[n]+1) {
			sdist[c]=dist[c];
			dist[c]=dist[n]+1;
		}
		else if(sdist[c]<dist[n]+1) sdist[c]=dist[n]+1;
	}
	P[c][0]=c;
	for(auto n: adj[c]) if(W[n]<W[c] && sdist[c]<dist[n]+1)  {
		int np=n;
		for(int i=18;i>=0;i--) if(get_dist(c,P[np][i])<=sdist[c]) np=P[np][i];
		if(get_dist(c,np)>sdist[c]) P[c][0]=np;
		else if(get_dist(c,P[np][0])>sdist[c]) P[c][0]=P[np][0];
	}
	for(int j=1;j<19;j++) P[c][j]=P[P[c][j-1]][j-1];
}

void solve2(int c)
{
	int m=N+c, sm=N, tm=N;
	for(int j=1;j<19;j++) P[N+c][j]=P[P[N+c][j-1]][j-1];
	for(auto n: adj[c]) if(W[n]<W[c]) {
		if(dist[m]<dist[n]) {
			tm=sm;
			sm=m;
			m=n;
		}
		else if(dist[sm]<dist[n]) {
			tm=sm;
			sm=n;
		}
		else if(dist[tm]<dist[n]) tm=n;
	}
	for(auto n: adj[c]) if(W[n]<W[c]) {
		int np;
		dist[N+n]=dist[m]==dist[n] ? dist[sm]+1:dist[m]+1;
		if(m==n) {
			np=sm;
			for(int i=18;i>=0;i--) if(get_dist(c,P[np][i])<=dist[tm]+1) np=P[np][i];
			if(get_dist(c,np)>dist[tm]+1) P[N+n][0]=np;
			else if(get_dist(c,P[np][0])>dist[tm]+1) P[N+n][0]=P[np][0];
		}
		else if(sm==n) {
			np=m;
			for(int i=18;i>=0;i--) if(get_dist(c,P[np][i])<=dist[tm]+1) np=P[np][i];
			if(get_dist(c,np)>dist[tm]+1) P[N+n][0]=np;
			else if(get_dist(c,P[np][0])>dist[tm]+1) P[N+n][0]=P[np][0];
		}
		else {
			np=m;
			for(int i=18;i>=0;i--) if(get_dist(c,P[np][i])<=dist[sm]+1) np=P[np][i];
			if(get_dist(c,np)>dist[sm]+1) P[N+n][0]=np;
			else if(get_dist(c,P[np][0])>dist[sm]+1) P[N+n][0]=P[np][0];
		}
		solve2(n);
	}
}

void dfs3(int c)
{
	CC[c]=2;
	if(++cnt[C[c<N ? c:parent[c-N]]]==1) res++;
	for(auto i: Q[c]) ans[i]=res;
	for(auto n: tree[c]) if(CC[n]==0) dfs3(n);
	if(--cnt[C[c<N ? c:parent[c-N]]]==0) res--;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int M;
	cin>>N>>M;
	memset(P,-1,sizeof(P));
	dist[P[N][0]=N]=-1;
	for(int i=1;i<N;i++) {
		int a, b;
		cin>>a>>b;
		adj[--a].push_back(--b);
		adj[b].push_back(a);
		P[N+i][0]=N+i;
	}
	dfs(0); dfs2(0);
	solve(0); solve2(0);
	for(int i=0;i<N;i++) {
		cin>>C[i];
		if(dist[i]>dist[N+i]+1) {
			int c=i;
			for(int j=18;j>=0;j--) if(get_dist(i,P[c][j])<=dist[N+i]+1) c=P[c][j];
			if(get_dist(i,c)>dist[N+i]+1) Q[c].push_back(i);
			else if(get_dist(i,P[c][0])>dist[N+i]+1) Q[P[c][0]].push_back(i);
		}
		else if(dist[i]<dist[N+i]+1) {
			int c=N+i;
			for(int j=18;j>=0;j--) if(get_dist(i,P[c][j])<=dist[i]) c=P[c][j];
			if(get_dist(i,c)>dist[i]) Q[c].push_back(i);
			else if(get_dist(i,P[c][0])>dist[i]) Q[P[c][0]].push_back(i);
		}
	}
	for(int i=0;i<2*N;i++) tree[P[i][0]].push_back(i);
	CC[N]=2;
	for(int i=0;i<2*N;i++) if(CC[i]==0) {
		int r=P[i][18];
		for(;CC[r]==0;r=P[r][0]) {
			CC[r]=1;
			if(++cnt[C[r<N ? r:parent[r-N]]]==1) res++;
		}
		for(;CC[r]==1;r=P[r][0]) dfs3(r);
		for(;CC[r]==2;r=P[r][0]) {
			CC[r]=3;
			if(--cnt[C[r<N ? r:parent[r-N]]]==0) res--;
		}
	}
	for(int i=0;i<N;i++) cout<<ans[i]<<'\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 53624 KB Output is correct
2 Correct 35 ms 53880 KB Output is correct
3 Correct 45 ms 53880 KB Output is correct
4 Correct 35 ms 54008 KB Output is correct
5 Correct 35 ms 53880 KB Output is correct
6 Correct 36 ms 54136 KB Output is correct
7 Correct 36 ms 54008 KB Output is correct
8 Correct 35 ms 53880 KB Output is correct
9 Correct 36 ms 54008 KB Output is correct
10 Correct 37 ms 53880 KB Output is correct
11 Correct 36 ms 53880 KB Output is correct
12 Correct 34 ms 54008 KB Output is correct
13 Correct 36 ms 54008 KB Output is correct
14 Correct 36 ms 54008 KB Output is correct
15 Correct 36 ms 54008 KB Output is correct
16 Correct 35 ms 54008 KB Output is correct
17 Correct 36 ms 54136 KB Output is correct
18 Correct 34 ms 53880 KB Output is correct
19 Correct 35 ms 53880 KB Output is correct
20 Correct 37 ms 54140 KB Output is correct
21 Correct 36 ms 54008 KB Output is correct
22 Correct 35 ms 53880 KB Output is correct
23 Correct 37 ms 53880 KB Output is correct
24 Correct 36 ms 53880 KB Output is correct
25 Correct 36 ms 53880 KB Output is correct
26 Correct 35 ms 54008 KB Output is correct
27 Correct 37 ms 54136 KB Output is correct
28 Correct 35 ms 54008 KB Output is correct
29 Correct 36 ms 54008 KB Output is correct
30 Correct 33 ms 54008 KB Output is correct
31 Correct 36 ms 54136 KB Output is correct
32 Correct 35 ms 54008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 417 ms 70388 KB Output is correct
2 Correct 760 ms 84728 KB Output is correct
3 Correct 112 ms 59636 KB Output is correct
4 Correct 744 ms 82160 KB Output is correct
5 Correct 1562 ms 102136 KB Output is correct
6 Correct 1114 ms 96248 KB Output is correct
7 Correct 696 ms 82800 KB Output is correct
8 Correct 747 ms 84088 KB Output is correct
9 Correct 731 ms 84472 KB Output is correct
10 Correct 758 ms 83828 KB Output is correct
11 Correct 429 ms 90344 KB Output is correct
12 Correct 1237 ms 98132 KB Output is correct
13 Correct 1065 ms 91252 KB Output is correct
14 Correct 1053 ms 93900 KB Output is correct
15 Correct 380 ms 89708 KB Output is correct
16 Correct 1172 ms 98288 KB Output is correct
17 Correct 1030 ms 95624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 578 ms 77504 KB Output is correct
2 Correct 1514 ms 103868 KB Output is correct
3 Correct 118 ms 60664 KB Output is correct
4 Correct 776 ms 84280 KB Output is correct
5 Correct 1610 ms 105588 KB Output is correct
6 Correct 1116 ms 94072 KB Output is correct
7 Correct 657 ms 86008 KB Output is correct
8 Correct 825 ms 87032 KB Output is correct
9 Correct 785 ms 86388 KB Output is correct
10 Correct 779 ms 85492 KB Output is correct
11 Correct 602 ms 87796 KB Output is correct
12 Correct 1425 ms 98916 KB Output is correct
13 Correct 1037 ms 92008 KB Output is correct
14 Correct 1030 ms 95056 KB Output is correct
15 Correct 398 ms 91600 KB Output is correct
16 Correct 1169 ms 100460 KB Output is correct
17 Correct 1026 ms 96160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 53624 KB Output is correct
2 Correct 35 ms 53880 KB Output is correct
3 Correct 45 ms 53880 KB Output is correct
4 Correct 35 ms 54008 KB Output is correct
5 Correct 35 ms 53880 KB Output is correct
6 Correct 36 ms 54136 KB Output is correct
7 Correct 36 ms 54008 KB Output is correct
8 Correct 35 ms 53880 KB Output is correct
9 Correct 36 ms 54008 KB Output is correct
10 Correct 37 ms 53880 KB Output is correct
11 Correct 36 ms 53880 KB Output is correct
12 Correct 34 ms 54008 KB Output is correct
13 Correct 36 ms 54008 KB Output is correct
14 Correct 36 ms 54008 KB Output is correct
15 Correct 36 ms 54008 KB Output is correct
16 Correct 35 ms 54008 KB Output is correct
17 Correct 36 ms 54136 KB Output is correct
18 Correct 34 ms 53880 KB Output is correct
19 Correct 35 ms 53880 KB Output is correct
20 Correct 37 ms 54140 KB Output is correct
21 Correct 36 ms 54008 KB Output is correct
22 Correct 35 ms 53880 KB Output is correct
23 Correct 37 ms 53880 KB Output is correct
24 Correct 36 ms 53880 KB Output is correct
25 Correct 36 ms 53880 KB Output is correct
26 Correct 35 ms 54008 KB Output is correct
27 Correct 37 ms 54136 KB Output is correct
28 Correct 35 ms 54008 KB Output is correct
29 Correct 36 ms 54008 KB Output is correct
30 Correct 33 ms 54008 KB Output is correct
31 Correct 36 ms 54136 KB Output is correct
32 Correct 35 ms 54008 KB Output is correct
33 Correct 417 ms 70388 KB Output is correct
34 Correct 760 ms 84728 KB Output is correct
35 Correct 112 ms 59636 KB Output is correct
36 Correct 744 ms 82160 KB Output is correct
37 Correct 1562 ms 102136 KB Output is correct
38 Correct 1114 ms 96248 KB Output is correct
39 Correct 696 ms 82800 KB Output is correct
40 Correct 747 ms 84088 KB Output is correct
41 Correct 731 ms 84472 KB Output is correct
42 Correct 758 ms 83828 KB Output is correct
43 Correct 429 ms 90344 KB Output is correct
44 Correct 1237 ms 98132 KB Output is correct
45 Correct 1065 ms 91252 KB Output is correct
46 Correct 1053 ms 93900 KB Output is correct
47 Correct 380 ms 89708 KB Output is correct
48 Correct 1172 ms 98288 KB Output is correct
49 Correct 1030 ms 95624 KB Output is correct
50 Correct 578 ms 77504 KB Output is correct
51 Correct 1514 ms 103868 KB Output is correct
52 Correct 118 ms 60664 KB Output is correct
53 Correct 776 ms 84280 KB Output is correct
54 Correct 1610 ms 105588 KB Output is correct
55 Correct 1116 ms 94072 KB Output is correct
56 Correct 657 ms 86008 KB Output is correct
57 Correct 825 ms 87032 KB Output is correct
58 Correct 785 ms 86388 KB Output is correct
59 Correct 779 ms 85492 KB Output is correct
60 Correct 602 ms 87796 KB Output is correct
61 Correct 1425 ms 98916 KB Output is correct
62 Correct 1037 ms 92008 KB Output is correct
63 Correct 1030 ms 95056 KB Output is correct
64 Correct 398 ms 91600 KB Output is correct
65 Correct 1169 ms 100460 KB Output is correct
66 Correct 1026 ms 96160 KB Output is correct
67 Correct 91 ms 57720 KB Output is correct
68 Correct 514 ms 79608 KB Output is correct
69 Correct 568 ms 77944 KB Output is correct
70 Correct 736 ms 82856 KB Output is correct
71 Correct 1576 ms 104824 KB Output is correct
72 Correct 1089 ms 95352 KB Output is correct
73 Correct 639 ms 84216 KB Output is correct
74 Correct 784 ms 86636 KB Output is correct
75 Correct 739 ms 83920 KB Output is correct
76 Correct 736 ms 83516 KB Output is correct
77 Correct 576 ms 86380 KB Output is correct
78 Correct 1337 ms 95224 KB Output is correct
79 Correct 1142 ms 99572 KB Output is correct
80 Correct 954 ms 94324 KB Output is correct
81 Correct 375 ms 89576 KB Output is correct
82 Correct 1155 ms 105328 KB Output is correct
83 Correct 1031 ms 95392 KB Output is correct
84 Correct 766 ms 82412 KB Output is correct
85 Correct 1572 ms 108536 KB Output is correct
86 Correct 1123 ms 94420 KB Output is correct
87 Correct 741 ms 82928 KB Output is correct
88 Correct 820 ms 87380 KB Output is correct
89 Correct 761 ms 84852 KB Output is correct
90 Correct 821 ms 84736 KB Output is correct
91 Correct 663 ms 84432 KB Output is correct
92 Correct 1759 ms 107072 KB Output is correct
93 Correct 1074 ms 90268 KB Output is correct
94 Correct 980 ms 86168 KB Output is correct
95 Correct 405 ms 90048 KB Output is correct
96 Correct 1355 ms 98688 KB Output is correct
97 Correct 1219 ms 95256 KB Output is correct
98 Correct 777 ms 85368 KB Output is correct
99 Correct 1861 ms 110348 KB Output is correct
100 Correct 1278 ms 95548 KB Output is correct
101 Correct 754 ms 85336 KB Output is correct
102 Correct 943 ms 86204 KB Output is correct
103 Correct 861 ms 86108 KB Output is correct
104 Correct 759 ms 85000 KB Output is correct
105 Correct 580 ms 87664 KB Output is correct
106 Correct 1199 ms 96636 KB Output is correct
107 Correct 1171 ms 97252 KB Output is correct
108 Correct 895 ms 90484 KB Output is correct
109 Correct 406 ms 91116 KB Output is correct
110 Correct 1210 ms 99692 KB Output is correct
111 Correct 1075 ms 96244 KB Output is correct