답안 #157478

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
157478 2019-10-11T22:30:36 Z ZwariowanyMarcin Unique Cities (JOI19_ho_t5) C++14
100 / 100
595 ms 52472 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define ss(x) (int) x.size()
#define FOR(i, j, n) for(int i = j; i <= n; ++i)
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl;
#define ios cin.tie(0); ios_base::sync_with_stdio(0)
 
 
using namespace std;	

const int nax = 2e5 + 111;

int dif, n, a, b, m;
int ile[nax];
int h[nax];
int col[nax];
deque <int> q;
pair<int, int> maks;
int dp[nax];

vector <int> v[nax];
int ans[nax];

void add(int u) {
	dif += ile[col[u]] == 0;
	ile[col[u]] += 1;
	q.push_back(u);
}

void del() {
	int u = q.back();
	dif -= ile[col[u]] == 1;
	ile[col[u]] -= 1;
	q.pop_back();
}

void dfs(int u, int p) {
	h[u] = h[p] + 1;
	maks = max(maks, mp(h[u], u));
	dp[u] = 0;
	for(auto it : v[u]) {
		if(it != p) {
			dfs(it, u);
			dp[u] = max(dp[u], dp[it]);
		}
	}
	dp[u] += 1;
}

void solve(int u, int p) {
	vector <pair<int, int>> som;
	for(auto it : v[u])
		if(it != p) {
			som.pb(mp(dp[it], it));
		}
	sort(som.begin(), som.end());
	reverse(som.begin(), som.end());
	
	int maxi = (ss(som) > 1 ? som[1].fi : 0);
	for(auto it : som) {
		while(!q.empty() && h[u] - h[q.back()] <= maxi)
			del();
		add(u);
		solve(it.se, u);
		while(!q.empty() && h[q.back()] >= h[u])
			del();
		maxi = som[0].fi;
	}
	while(!q.empty() && h[u] - h[q.back()] <= maxi)
			del();
	ans[u] = max(ans[u], dif);
}
		

int main() {
	scanf("%d %d", &n, &m);
	FOR(i, 1, n - 1) {
		scanf("%d %d", &a, &b);
		v[a].pb(b);
		v[b].pb(a);
	}
	FOR(i, 1, n)
		scanf("%d", &col[i]);
	dfs(1, 0);
	
	int A = maks.se;
	
	fill(h + 1, h + n + 1, 0);
	maks = mp(-1, -1);
	dfs(A, 0);
	solve(A, 0);
	
	int B = maks.se;
	
	fill(h + 1, h + n + 1, 0);
	dfs(B, 0);
	solve(B, 0);
	
	FOR(i, 1, n)	
		printf("%d\n", ans[i]);
	
	
	
	
	
	return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:84: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:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &col[i]);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 8 ms 5240 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 8 ms 5496 KB Output is correct
7 Correct 8 ms 5240 KB Output is correct
8 Correct 8 ms 5212 KB Output is correct
9 Correct 8 ms 5112 KB Output is correct
10 Correct 9 ms 5112 KB Output is correct
11 Correct 8 ms 5112 KB Output is correct
12 Correct 9 ms 5160 KB Output is correct
13 Correct 9 ms 5496 KB Output is correct
14 Correct 10 ms 5248 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 8 ms 5368 KB Output is correct
18 Correct 8 ms 5260 KB Output is correct
19 Correct 8 ms 5240 KB Output is correct
20 Correct 9 ms 5496 KB Output is correct
21 Correct 8 ms 5240 KB Output is correct
22 Correct 8 ms 5252 KB Output is correct
23 Correct 8 ms 5240 KB Output is correct
24 Correct 9 ms 5212 KB Output is correct
25 Correct 9 ms 5240 KB Output is correct
26 Correct 9 ms 5240 KB Output is correct
27 Correct 9 ms 5368 KB Output is correct
28 Correct 8 ms 5368 KB Output is correct
29 Correct 8 ms 5292 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 8 ms 5368 KB Output is correct
32 Correct 8 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 174 ms 12332 KB Output is correct
2 Correct 212 ms 30692 KB Output is correct
3 Correct 42 ms 9080 KB Output is correct
4 Correct 388 ms 17968 KB Output is correct
5 Correct 527 ms 50016 KB Output is correct
6 Correct 505 ms 33676 KB Output is correct
7 Correct 347 ms 18384 KB Output is correct
8 Correct 507 ms 21104 KB Output is correct
9 Correct 372 ms 20060 KB Output is correct
10 Correct 557 ms 19884 KB Output is correct
11 Correct 267 ms 21644 KB Output is correct
12 Correct 438 ms 37768 KB Output is correct
13 Correct 406 ms 33728 KB Output is correct
14 Correct 393 ms 32788 KB Output is correct
15 Correct 236 ms 21700 KB Output is correct
16 Correct 537 ms 40676 KB Output is correct
17 Correct 484 ms 34276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 16516 KB Output is correct
2 Correct 460 ms 50296 KB Output is correct
3 Correct 48 ms 9852 KB Output is correct
4 Correct 313 ms 19496 KB Output is correct
5 Correct 474 ms 52472 KB Output is correct
6 Correct 556 ms 35704 KB Output is correct
7 Correct 468 ms 19748 KB Output is correct
8 Correct 389 ms 26352 KB Output is correct
9 Correct 366 ms 23992 KB Output is correct
10 Correct 355 ms 21992 KB Output is correct
11 Correct 304 ms 21288 KB Output is correct
12 Correct 493 ms 45836 KB Output is correct
13 Correct 437 ms 33256 KB Output is correct
14 Correct 405 ms 33844 KB Output is correct
15 Correct 233 ms 22696 KB Output is correct
16 Correct 418 ms 42340 KB Output is correct
17 Correct 419 ms 35920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 8 ms 5240 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 8 ms 5496 KB Output is correct
7 Correct 8 ms 5240 KB Output is correct
8 Correct 8 ms 5212 KB Output is correct
9 Correct 8 ms 5112 KB Output is correct
10 Correct 9 ms 5112 KB Output is correct
11 Correct 8 ms 5112 KB Output is correct
12 Correct 9 ms 5160 KB Output is correct
13 Correct 9 ms 5496 KB Output is correct
14 Correct 10 ms 5248 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 8 ms 5368 KB Output is correct
18 Correct 8 ms 5260 KB Output is correct
19 Correct 8 ms 5240 KB Output is correct
20 Correct 9 ms 5496 KB Output is correct
21 Correct 8 ms 5240 KB Output is correct
22 Correct 8 ms 5252 KB Output is correct
23 Correct 8 ms 5240 KB Output is correct
24 Correct 9 ms 5212 KB Output is correct
25 Correct 9 ms 5240 KB Output is correct
26 Correct 9 ms 5240 KB Output is correct
27 Correct 9 ms 5368 KB Output is correct
28 Correct 8 ms 5368 KB Output is correct
29 Correct 8 ms 5292 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 8 ms 5368 KB Output is correct
32 Correct 8 ms 5368 KB Output is correct
33 Correct 174 ms 12332 KB Output is correct
34 Correct 212 ms 30692 KB Output is correct
35 Correct 42 ms 9080 KB Output is correct
36 Correct 388 ms 17968 KB Output is correct
37 Correct 527 ms 50016 KB Output is correct
38 Correct 505 ms 33676 KB Output is correct
39 Correct 347 ms 18384 KB Output is correct
40 Correct 507 ms 21104 KB Output is correct
41 Correct 372 ms 20060 KB Output is correct
42 Correct 557 ms 19884 KB Output is correct
43 Correct 267 ms 21644 KB Output is correct
44 Correct 438 ms 37768 KB Output is correct
45 Correct 406 ms 33728 KB Output is correct
46 Correct 393 ms 32788 KB Output is correct
47 Correct 236 ms 21700 KB Output is correct
48 Correct 537 ms 40676 KB Output is correct
49 Correct 484 ms 34276 KB Output is correct
50 Correct 254 ms 16516 KB Output is correct
51 Correct 460 ms 50296 KB Output is correct
52 Correct 48 ms 9852 KB Output is correct
53 Correct 313 ms 19496 KB Output is correct
54 Correct 474 ms 52472 KB Output is correct
55 Correct 556 ms 35704 KB Output is correct
56 Correct 468 ms 19748 KB Output is correct
57 Correct 389 ms 26352 KB Output is correct
58 Correct 366 ms 23992 KB Output is correct
59 Correct 355 ms 21992 KB Output is correct
60 Correct 304 ms 21288 KB Output is correct
61 Correct 493 ms 45836 KB Output is correct
62 Correct 437 ms 33256 KB Output is correct
63 Correct 405 ms 33844 KB Output is correct
64 Correct 233 ms 22696 KB Output is correct
65 Correct 418 ms 42340 KB Output is correct
66 Correct 419 ms 35920 KB Output is correct
67 Correct 37 ms 6904 KB Output is correct
68 Correct 151 ms 25052 KB Output is correct
69 Correct 218 ms 23288 KB Output is correct
70 Correct 339 ms 17928 KB Output is correct
71 Correct 455 ms 49880 KB Output is correct
72 Correct 442 ms 33772 KB Output is correct
73 Correct 467 ms 18128 KB Output is correct
74 Correct 402 ms 24200 KB Output is correct
75 Correct 380 ms 20588 KB Output is correct
76 Correct 379 ms 20308 KB Output is correct
77 Correct 297 ms 19720 KB Output is correct
78 Correct 485 ms 41188 KB Output is correct
79 Correct 430 ms 37656 KB Output is correct
80 Correct 423 ms 31028 KB Output is correct
81 Correct 246 ms 21620 KB Output is correct
82 Correct 395 ms 40696 KB Output is correct
83 Correct 432 ms 34276 KB Output is correct
84 Correct 361 ms 18376 KB Output is correct
85 Correct 505 ms 50552 KB Output is correct
86 Correct 474 ms 34220 KB Output is correct
87 Correct 343 ms 18304 KB Output is correct
88 Correct 347 ms 24960 KB Output is correct
89 Correct 382 ms 21792 KB Output is correct
90 Correct 344 ms 20688 KB Output is correct
91 Correct 296 ms 20084 KB Output is correct
92 Correct 479 ms 49520 KB Output is correct
93 Correct 373 ms 29396 KB Output is correct
94 Correct 355 ms 26284 KB Output is correct
95 Correct 269 ms 21956 KB Output is correct
96 Correct 473 ms 40800 KB Output is correct
97 Correct 433 ms 34664 KB Output is correct
98 Correct 338 ms 19576 KB Output is correct
99 Correct 595 ms 50964 KB Output is correct
100 Correct 470 ms 35252 KB Output is correct
101 Correct 317 ms 19260 KB Output is correct
102 Correct 465 ms 24096 KB Output is correct
103 Correct 334 ms 22052 KB Output is correct
104 Correct 378 ms 21128 KB Output is correct
105 Correct 264 ms 20620 KB Output is correct
106 Correct 434 ms 37220 KB Output is correct
107 Correct 442 ms 37528 KB Output is correct
108 Correct 420 ms 29040 KB Output is correct
109 Correct 233 ms 22588 KB Output is correct
110 Correct 389 ms 41560 KB Output is correct
111 Correct 439 ms 35404 KB Output is correct