Submission #121337

# Submission time Handle Problem Language Result Execution time Memory
121337 2019-06-26T10:53:26 Z imyujin Unique Cities (JOI19_ho_t5) C++14
100 / 100
680 ms 34040 KB
#include<stdio.h>
#include<vector>
#include<algorithm>

using namespace std;

#define MAXN 200005

int N, M;
int A[MAXN], B[MAXN], C[MAXN];
vector<int> E[MAXN];
int dep[MAXN], par[MAXN], uni[MAXN], mxdep[MAXN][2], mxn[MAXN];
int cnts[MAXN];
int spe;
int ans[MAXN];

int guni(int x){
	if(uni[x]==x) return x;
	return uni[x]=guni(uni[x]);
}

void dfs(int x){
	//printf("dfs(%d)\n", x);
	mxdep[x][0]=mxdep[x][1]=dep[x];
	for(auto a:E[x]) if(dep[a]==-1){
		dep[a]=dep[x]+1;
		par[a]=x;
		dfs(a);
		if(mxdep[a][0]>mxdep[x][1]) mxdep[x][1]=mxdep[a][0];
		if(mxdep[x][1]>mxdep[x][0]){
			swap(mxdep[x][0], mxdep[x][1]);
			mxn[x]=a;
		}
	}
}

void gdfs(int x){
	//printf("gdfs(%d)\n", x);
	for(int i=0; i<=N; i++){
		dep[i]=-1;
		mxn[i]=0;
		par[i]=0;
	}
	dep[x]=0;
	dfs(x);
}

void cover(int x, int d){
	//printf("cover(x=%d, d=%d)\n", x, d);
	for(; x!=0&&dep[x]>=d; x=guni(par[x])){
		if(uni[x]==x&&--cnts[C[x]]==0) spe--;
		uni[x]=guni(par[x]);
	}
}

void dfs2(int x){
	/*
	printf("dfs2(%d), spe=%d\n", x, spe);
	printf("uni=[");
	for(int i=1; i<=N; i++) printf("%d ", uni[i]);
	printf("]\ncnts=[");
	for(int i=1; i<=M; i++) printf("%d ", cnts[i]);
	printf("]\n");
	*/
	//for(int i=1; i<=M; i++) printf("cnts[%d]=%d\n", i, cnts[i]);
	if(++cnts[C[x]]==1) spe++;
	if(dep[x]!=mxdep[x][0]){
		cover(guni(par[x]), 2*dep[x]-mxdep[x][1]);
		dfs2(mxn[x]);
		if(uni[x]!=x){
			uni[x]=x;
			if(++cnts[C[x]]==1) spe++;
		}
		cover(guni(par[x]), 2*dep[x]-mxdep[x][0]);
		for(auto a:E[x]) if(a!=par[x]&&a!=mxn[x]){
			dfs2(a);
			if(uni[x]!=x){
				uni[x]=x;
				if(++cnts[C[x]]==1) spe++;
			}
		}
	}
	if(uni[x]==x&&--cnts[C[x]]==0) spe--;
	ans[x]=max(ans[x], spe);
}

void solve(int x){
	//printf("solve(%d)\n", x);
	for(int i=0; i<=N; i++) uni[i]=i;
	spe=0;
	for(int i=1; i<=M; i++) cnts[i]=0;
	dfs2(x);
}

int main(){
	int X=1, Y=1;

	scanf("%d%d", &N, &M);
	for(int i=0; i<N-1; i++) scanf("%d%d", A+i, B+i);
	for(int i=1; i<=N; i++) scanf("%d", C+i);

	for(int i=0; i<N-1; i++){
		E[A[i]].push_back(B[i]);
		E[B[i]].push_back(A[i]);
	}

	gdfs(1);
	for(int i=1; i<=N; i++) if(dep[i]>dep[X]) X=i;
	gdfs(X);
	for(int i=1; i<=N; i++) if(dep[i]>dep[Y]) Y=i;
	//printf("X=%d, Y=%d\n", X, Y);
	//for(int i=1; i<=N; i++) printf("mxdep[%d]=%d, mxn[%d]=%d\n", i, mxdep[i][0], i, mxn[i]);
	solve(X);
	gdfs(Y);
	solve(Y);

	for(int i=1; i<=N; i++) printf("%d\n", ans[i]);
	return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:99:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0; i<N-1; i++) scanf("%d%d", A+i, B+i);
                           ~~~~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:100:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1; i<=N; i++) scanf("%d", C+i);
                          ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4992 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 7 ms 5248 KB Output is correct
6 Correct 7 ms 5376 KB Output is correct
7 Correct 8 ms 5376 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 8 ms 5376 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 7 ms 5220 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 8 ms 5376 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 6 ms 5376 KB Output is correct
17 Correct 7 ms 5248 KB Output is correct
18 Correct 8 ms 5248 KB Output is correct
19 Correct 8 ms 5248 KB Output is correct
20 Correct 8 ms 5376 KB Output is correct
21 Correct 8 ms 5248 KB Output is correct
22 Correct 7 ms 5248 KB Output is correct
23 Correct 7 ms 5248 KB Output is correct
24 Correct 7 ms 5248 KB Output is correct
25 Correct 7 ms 5248 KB Output is correct
26 Correct 7 ms 5248 KB Output is correct
27 Correct 8 ms 5376 KB Output is correct
28 Correct 7 ms 5248 KB Output is correct
29 Correct 8 ms 5248 KB Output is correct
30 Correct 7 ms 5248 KB Output is correct
31 Correct 8 ms 5376 KB Output is correct
32 Correct 7 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 13532 KB Output is correct
2 Correct 253 ms 21240 KB Output is correct
3 Correct 35 ms 8312 KB Output is correct
4 Correct 409 ms 20324 KB Output is correct
5 Correct 566 ms 32476 KB Output is correct
6 Correct 510 ms 26176 KB Output is correct
7 Correct 409 ms 20080 KB Output is correct
8 Correct 402 ms 21420 KB Output is correct
9 Correct 409 ms 20984 KB Output is correct
10 Correct 383 ms 20856 KB Output is correct
11 Correct 237 ms 20584 KB Output is correct
12 Correct 473 ms 27828 KB Output is correct
13 Correct 415 ms 26228 KB Output is correct
14 Correct 448 ms 25828 KB Output is correct
15 Correct 164 ms 20588 KB Output is correct
16 Correct 424 ms 28784 KB Output is correct
17 Correct 478 ms 26484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 301 ms 17192 KB Output is correct
2 Correct 680 ms 32668 KB Output is correct
3 Correct 40 ms 8952 KB Output is correct
4 Correct 453 ms 20856 KB Output is correct
5 Correct 516 ms 34040 KB Output is correct
6 Correct 670 ms 27284 KB Output is correct
7 Correct 548 ms 20912 KB Output is correct
8 Correct 585 ms 23672 KB Output is correct
9 Correct 491 ms 22724 KB Output is correct
10 Correct 489 ms 21748 KB Output is correct
11 Correct 403 ms 21084 KB Output is correct
12 Correct 501 ms 31608 KB Output is correct
13 Correct 441 ms 26612 KB Output is correct
14 Correct 543 ms 26484 KB Output is correct
15 Correct 200 ms 21484 KB Output is correct
16 Correct 480 ms 29872 KB Output is correct
17 Correct 533 ms 27584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4992 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 7 ms 5248 KB Output is correct
6 Correct 7 ms 5376 KB Output is correct
7 Correct 8 ms 5376 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 8 ms 5376 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 7 ms 5220 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 8 ms 5376 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 6 ms 5376 KB Output is correct
17 Correct 7 ms 5248 KB Output is correct
18 Correct 8 ms 5248 KB Output is correct
19 Correct 8 ms 5248 KB Output is correct
20 Correct 8 ms 5376 KB Output is correct
21 Correct 8 ms 5248 KB Output is correct
22 Correct 7 ms 5248 KB Output is correct
23 Correct 7 ms 5248 KB Output is correct
24 Correct 7 ms 5248 KB Output is correct
25 Correct 7 ms 5248 KB Output is correct
26 Correct 7 ms 5248 KB Output is correct
27 Correct 8 ms 5376 KB Output is correct
28 Correct 7 ms 5248 KB Output is correct
29 Correct 8 ms 5248 KB Output is correct
30 Correct 7 ms 5248 KB Output is correct
31 Correct 8 ms 5376 KB Output is correct
32 Correct 7 ms 5248 KB Output is correct
33 Correct 167 ms 13532 KB Output is correct
34 Correct 253 ms 21240 KB Output is correct
35 Correct 35 ms 8312 KB Output is correct
36 Correct 409 ms 20324 KB Output is correct
37 Correct 566 ms 32476 KB Output is correct
38 Correct 510 ms 26176 KB Output is correct
39 Correct 409 ms 20080 KB Output is correct
40 Correct 402 ms 21420 KB Output is correct
41 Correct 409 ms 20984 KB Output is correct
42 Correct 383 ms 20856 KB Output is correct
43 Correct 237 ms 20584 KB Output is correct
44 Correct 473 ms 27828 KB Output is correct
45 Correct 415 ms 26228 KB Output is correct
46 Correct 448 ms 25828 KB Output is correct
47 Correct 164 ms 20588 KB Output is correct
48 Correct 424 ms 28784 KB Output is correct
49 Correct 478 ms 26484 KB Output is correct
50 Correct 301 ms 17192 KB Output is correct
51 Correct 680 ms 32668 KB Output is correct
52 Correct 40 ms 8952 KB Output is correct
53 Correct 453 ms 20856 KB Output is correct
54 Correct 516 ms 34040 KB Output is correct
55 Correct 670 ms 27284 KB Output is correct
56 Correct 548 ms 20912 KB Output is correct
57 Correct 585 ms 23672 KB Output is correct
58 Correct 491 ms 22724 KB Output is correct
59 Correct 489 ms 21748 KB Output is correct
60 Correct 403 ms 21084 KB Output is correct
61 Correct 501 ms 31608 KB Output is correct
62 Correct 441 ms 26612 KB Output is correct
63 Correct 543 ms 26484 KB Output is correct
64 Correct 200 ms 21484 KB Output is correct
65 Correct 480 ms 29872 KB Output is correct
66 Correct 533 ms 27584 KB Output is correct
67 Correct 31 ms 7544 KB Output is correct
68 Correct 212 ms 17728 KB Output is correct
69 Correct 301 ms 18596 KB Output is correct
70 Correct 465 ms 20016 KB Output is correct
71 Correct 626 ms 32504 KB Output is correct
72 Correct 590 ms 26228 KB Output is correct
73 Correct 447 ms 20060 KB Output is correct
74 Correct 447 ms 22396 KB Output is correct
75 Correct 428 ms 21240 KB Output is correct
76 Correct 453 ms 21032 KB Output is correct
77 Correct 305 ms 20448 KB Output is correct
78 Correct 584 ms 29180 KB Output is correct
79 Correct 448 ms 27680 KB Output is correct
80 Correct 461 ms 25096 KB Output is correct
81 Correct 159 ms 20716 KB Output is correct
82 Correct 410 ms 28528 KB Output is correct
83 Correct 485 ms 26324 KB Output is correct
84 Correct 457 ms 19832 KB Output is correct
85 Correct 552 ms 32920 KB Output is correct
86 Correct 489 ms 26360 KB Output is correct
87 Correct 382 ms 19960 KB Output is correct
88 Correct 452 ms 22904 KB Output is correct
89 Correct 465 ms 21624 KB Output is correct
90 Correct 468 ms 21112 KB Output is correct
91 Correct 315 ms 20364 KB Output is correct
92 Correct 581 ms 32460 KB Output is correct
93 Correct 481 ms 24560 KB Output is correct
94 Correct 461 ms 23300 KB Output is correct
95 Correct 178 ms 20716 KB Output is correct
96 Correct 477 ms 28836 KB Output is correct
97 Correct 542 ms 26536 KB Output is correct
98 Correct 500 ms 20884 KB Output is correct
99 Correct 622 ms 33020 KB Output is correct
100 Correct 612 ms 27000 KB Output is correct
101 Correct 370 ms 20212 KB Output is correct
102 Correct 455 ms 22392 KB Output is correct
103 Correct 522 ms 21628 KB Output is correct
104 Correct 433 ms 21220 KB Output is correct
105 Correct 260 ms 20464 KB Output is correct
106 Correct 446 ms 27820 KB Output is correct
107 Correct 431 ms 27924 KB Output is correct
108 Correct 447 ms 24708 KB Output is correct
109 Correct 176 ms 21096 KB Output is correct
110 Correct 436 ms 29268 KB Output is correct
111 Correct 544 ms 26996 KB Output is correct