Submission #121146

# Submission time Handle Problem Language Result Execution time Memory
121146 2019-06-26T07:08:24 Z 김세빈(#2968) Unique Cities (JOI19_ho_t5) C++14
100 / 100
677 ms 39156 KB
#include <bits/stdc++.h>

using namespace std;

vector <int> T[202020];
int P[202020], L[202020], N[202020];
int K[202020], C[202020];
int D1[202020], D2[202020], A1[202020], A2[202020];
int n, m, s, mx, mi;

int dfs1(int p, int r, int *D)
{
	P[p] = r; D[p] = D[r] + 1; L[p] = 1;
	if(D[p] > mx) mx = D[p], mi = p;

	for(int &t: T[p]){
		if(t != r){
			L[p] = max(L[p], dfs1(t, p, D) + 1);
		}
	}

	return L[p];
}

int color(int p, int x, int *D)
{
	if(!p || x <= 0) return p;
	else if(N[p] == p){
		K[C[p]] --; s -= (K[C[p]] == 0);
		N[p] = color(P[p], x - 1, D);
	}
	else N[p] = color(N[p], x - D[p] + D[N[p]], D);
}

void dfs2(int p, int r, int *D, int *A)
{
	int m1, m2, v;

	s += (K[C[p]] == 0); K[C[p]] ++;

	m1 = m2 = v = 0;

	for(int &t: T[p]){
		if(t != r){
			if(L[t] > m1) m2 = m1, m1 = L[t], v = t;
			else if(L[t] > m2) m2 = L[t];
		}
	}

	color(P[p], m2, D);
	if(v) dfs2(v, p, D, A);
	color(P[p], m1, D);

	for(int &t: T[p]){
		if(t != r && t != v){
			if(N[p] != p){
				s += (K[C[p]] == 0); K[C[p]] ++;
				N[p] = p;
			}
			dfs2(t, p, D, A);
		}
	}

	if(N[p] == p){
		K[C[p]] --; s -= (K[C[p]] == 0);
		N[p] = P[p];
	}

	A[p] = s;
}

int main()
{
	int i, u, v;

	scanf("%d%d", &n, &m);

	for(i=1; i<n; i++){
		scanf("%d%d", &u, &v);
		T[u].push_back(v);
		T[v].push_back(u);
	}

	for(i=1; i<=n; i++){
		scanf("%d", C + i);
		N[i] = i;
	}

	mx = 0; dfs1(1, 0, D1); u = mi;
	mx = 0; dfs1(u, 0, D1); v = mi;
	dfs2(u, 0, D1, A1);

	s = 0;

	for(i=1; i<=n; i++){
		K[i] = 0; N[i] = i;
	}

	dfs1(v, 0, D2); dfs2(v, 0, D2, A2);

	for(i=1; i<=n; i++){
		if(D1[i] > D2[i]) printf("%d\n", A1[i]);
		else printf("%d\n", A2[i]);
	}

	return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int color(int, int, int*)':
joi2019_ho_t5.cpp:33:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:76: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:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", C + i);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 8 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 8 ms 5376 KB Output is correct
5 Correct 8 ms 5220 KB Output is correct
6 Correct 8 ms 5376 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 8 ms 5220 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 9 ms 5248 KB Output is correct
11 Correct 7 ms 5248 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 10 ms 5476 KB Output is correct
14 Correct 7 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 8 ms 5324 KB Output is correct
17 Correct 8 ms 5420 KB Output is correct
18 Correct 7 ms 5376 KB Output is correct
19 Correct 8 ms 5240 KB Output is correct
20 Correct 8 ms 5504 KB Output is correct
21 Correct 9 ms 5376 KB Output is correct
22 Correct 7 ms 5200 KB Output is correct
23 Correct 7 ms 5248 KB Output is correct
24 Correct 8 ms 5220 KB Output is correct
25 Correct 8 ms 5252 KB Output is correct
26 Correct 7 ms 5248 KB Output is correct
27 Correct 8 ms 5376 KB Output is correct
28 Correct 8 ms 5376 KB Output is correct
29 Correct 8 ms 5376 KB Output is correct
30 Correct 7 ms 5248 KB Output is correct
31 Correct 7 ms 5420 KB Output is correct
32 Correct 8 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 230 ms 13428 KB Output is correct
2 Correct 298 ms 24316 KB Output is correct
3 Correct 52 ms 8696 KB Output is correct
4 Correct 477 ms 19676 KB Output is correct
5 Correct 667 ms 38088 KB Output is correct
6 Correct 604 ms 28872 KB Output is correct
7 Correct 455 ms 19520 KB Output is correct
8 Correct 472 ms 21400 KB Output is correct
9 Correct 484 ms 20856 KB Output is correct
10 Correct 477 ms 20728 KB Output is correct
11 Correct 228 ms 20056 KB Output is correct
12 Correct 578 ms 31196 KB Output is correct
13 Correct 496 ms 28660 KB Output is correct
14 Correct 473 ms 28020 KB Output is correct
15 Correct 197 ms 20224 KB Output is correct
16 Correct 484 ms 32240 KB Output is correct
17 Correct 516 ms 29176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 349 ms 16372 KB Output is correct
2 Correct 645 ms 37508 KB Output is correct
3 Correct 41 ms 9380 KB Output is correct
4 Correct 479 ms 19576 KB Output is correct
5 Correct 677 ms 39156 KB Output is correct
6 Correct 629 ms 29048 KB Output is correct
7 Correct 445 ms 19436 KB Output is correct
8 Correct 520 ms 23544 KB Output is correct
9 Correct 511 ms 22136 KB Output is correct
10 Correct 513 ms 20984 KB Output is correct
11 Correct 369 ms 19784 KB Output is correct
12 Correct 629 ms 35092 KB Output is correct
13 Correct 557 ms 27892 KB Output is correct
14 Correct 550 ms 28120 KB Output is correct
15 Correct 240 ms 20160 KB Output is correct
16 Correct 556 ms 32776 KB Output is correct
17 Correct 537 ms 29288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 8 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 8 ms 5376 KB Output is correct
5 Correct 8 ms 5220 KB Output is correct
6 Correct 8 ms 5376 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 8 ms 5220 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 9 ms 5248 KB Output is correct
11 Correct 7 ms 5248 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 10 ms 5476 KB Output is correct
14 Correct 7 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 8 ms 5324 KB Output is correct
17 Correct 8 ms 5420 KB Output is correct
18 Correct 7 ms 5376 KB Output is correct
19 Correct 8 ms 5240 KB Output is correct
20 Correct 8 ms 5504 KB Output is correct
21 Correct 9 ms 5376 KB Output is correct
22 Correct 7 ms 5200 KB Output is correct
23 Correct 7 ms 5248 KB Output is correct
24 Correct 8 ms 5220 KB Output is correct
25 Correct 8 ms 5252 KB Output is correct
26 Correct 7 ms 5248 KB Output is correct
27 Correct 8 ms 5376 KB Output is correct
28 Correct 8 ms 5376 KB Output is correct
29 Correct 8 ms 5376 KB Output is correct
30 Correct 7 ms 5248 KB Output is correct
31 Correct 7 ms 5420 KB Output is correct
32 Correct 8 ms 5376 KB Output is correct
33 Correct 230 ms 13428 KB Output is correct
34 Correct 298 ms 24316 KB Output is correct
35 Correct 52 ms 8696 KB Output is correct
36 Correct 477 ms 19676 KB Output is correct
37 Correct 667 ms 38088 KB Output is correct
38 Correct 604 ms 28872 KB Output is correct
39 Correct 455 ms 19520 KB Output is correct
40 Correct 472 ms 21400 KB Output is correct
41 Correct 484 ms 20856 KB Output is correct
42 Correct 477 ms 20728 KB Output is correct
43 Correct 228 ms 20056 KB Output is correct
44 Correct 578 ms 31196 KB Output is correct
45 Correct 496 ms 28660 KB Output is correct
46 Correct 473 ms 28020 KB Output is correct
47 Correct 197 ms 20224 KB Output is correct
48 Correct 484 ms 32240 KB Output is correct
49 Correct 516 ms 29176 KB Output is correct
50 Correct 349 ms 16372 KB Output is correct
51 Correct 645 ms 37508 KB Output is correct
52 Correct 41 ms 9380 KB Output is correct
53 Correct 479 ms 19576 KB Output is correct
54 Correct 677 ms 39156 KB Output is correct
55 Correct 629 ms 29048 KB Output is correct
56 Correct 445 ms 19436 KB Output is correct
57 Correct 520 ms 23544 KB Output is correct
58 Correct 511 ms 22136 KB Output is correct
59 Correct 513 ms 20984 KB Output is correct
60 Correct 369 ms 19784 KB Output is correct
61 Correct 629 ms 35092 KB Output is correct
62 Correct 557 ms 27892 KB Output is correct
63 Correct 550 ms 28120 KB Output is correct
64 Correct 240 ms 20160 KB Output is correct
65 Correct 556 ms 32776 KB Output is correct
66 Correct 537 ms 29288 KB Output is correct
67 Correct 37 ms 7404 KB Output is correct
68 Correct 169 ms 19960 KB Output is correct
69 Correct 292 ms 20108 KB Output is correct
70 Correct 455 ms 19576 KB Output is correct
71 Correct 618 ms 38108 KB Output is correct
72 Correct 582 ms 28592 KB Output is correct
73 Correct 444 ms 19448 KB Output is correct
74 Correct 489 ms 23032 KB Output is correct
75 Correct 484 ms 21240 KB Output is correct
76 Correct 463 ms 20812 KB Output is correct
77 Correct 307 ms 19820 KB Output is correct
78 Correct 588 ms 33004 KB Output is correct
79 Correct 500 ms 30708 KB Output is correct
80 Correct 510 ms 26868 KB Output is correct
81 Correct 223 ms 20072 KB Output is correct
82 Correct 534 ms 31956 KB Output is correct
83 Correct 585 ms 28768 KB Output is correct
84 Correct 493 ms 19576 KB Output is correct
85 Correct 630 ms 38468 KB Output is correct
86 Correct 561 ms 28736 KB Output is correct
87 Correct 436 ms 19468 KB Output is correct
88 Correct 479 ms 23416 KB Output is correct
89 Correct 474 ms 21380 KB Output is correct
90 Correct 509 ms 20984 KB Output is correct
91 Correct 359 ms 19568 KB Output is correct
92 Correct 623 ms 37880 KB Output is correct
93 Correct 499 ms 25976 KB Output is correct
94 Correct 504 ms 24212 KB Output is correct
95 Correct 201 ms 19944 KB Output is correct
96 Correct 513 ms 32364 KB Output is correct
97 Correct 556 ms 29044 KB Output is correct
98 Correct 507 ms 19296 KB Output is correct
99 Correct 640 ms 38484 KB Output is correct
100 Correct 595 ms 28980 KB Output is correct
101 Correct 457 ms 19384 KB Output is correct
102 Correct 485 ms 22556 KB Output is correct
103 Correct 469 ms 21276 KB Output is correct
104 Correct 477 ms 20820 KB Output is correct
105 Correct 316 ms 19764 KB Output is correct
106 Correct 594 ms 30184 KB Output is correct
107 Correct 623 ms 30884 KB Output is correct
108 Correct 525 ms 25180 KB Output is correct
109 Correct 226 ms 20028 KB Output is correct
110 Correct 493 ms 32452 KB Output is correct
111 Correct 572 ms 29060 KB Output is correct