Submission #211311

# Submission time Handle Problem Language Result Execution time Memory
211311 2020-03-20T03:32:19 Z Kevin_Zhang_TW Unique Cities (JOI19_ho_t5) C++17
100 / 100
418 ms 40812 KB
#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
const int maxn = 300010;
int n, m;
int c[maxn];
vector<int> edge[maxn];
int len[maxn], fp[maxn], dep[maxn], son[maxn];
namespace st{
	int st[maxn], stl;
	int cnt[maxn], _res;
	void _add_in(int a, int d) {
		if (cnt[a] == 0) ++_res;
		if ((cnt[a]+=d) == 0) --_res;
	}
	void push(int id) {
		_add_in(c[id], 1);
		st[stl++] = id;
	}
	void pop() {
		assert(--stl >= 0);
		_add_in(c[st[stl]], -1);
	}
	int back() {
		return st[stl-1];
	}
	void pop_dep(int d) {
		while (stl && dep[back()] >= d) pop();
	}
	int query() {
		return _res;
	}
}
int getfar(int now, int last = -1) {
	len[now] = 0;
	fp[now] = now;
	for (int u : edge[now]) if (u != last) {
		int v = getfar(u, now);
		if (len[u]+1 > len[now])
			fp[now] = v, len[now] = len[u]+1;
	}
	return fp[now];
}
int res[maxn];
void getinfo(int now, int last = -1) {
	static int d;
	len[now] = 0;
	dep[now] = ++d;
	son[now] = -1;
	for (int u : edge[now]) if (u != last) {
		getinfo(u, now);
		if (len[u]+1 > len[now]) {
			son[now] = u;
			len[now] = len[u]+1;
		}
	}
	--d;
}
void getres(int now, int last = -1) {
	if (son[now] == -1) {
		res[now] = max(res[now], st::query());
		//cerr << "now " << now << " : " << st::query() << " son : " << son[now] << '\n';
		return;
	}
	int mx = 0;
	for (int u : edge[now]) if (u != last && u != son[now])
		mx = max(mx, len[u]+1);
	st::pop_dep(dep[now] - mx);
	st::push(now);
	getres(son[now], now);
	for (int u : edge[now]) if (u != last && u != son[now]) {
		st::pop_dep(dep[now] - len[now]);
		st::push(now);
		getres(u, now);
	}
	st::pop_dep(dep[now] - len[now]);
	//cerr << "now " << now << " : " << st::query() << " son : " << son[now] << '\n';
	res[now] = max(res[now], st::query());
}
void get_res(int root) {
	getinfo(root);
	getres(root);
	//cerr << '\n';
}
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> m;
	for (int a, b, i = 1;i < n;++i) {
		cin >> a >> b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	for (int i = 1;i <= n;++i)
		cin >> c[i];
	int a = getfar(1), b = getfar(a);
	//cerr << "dia " << a << ' ' << b << '\n';
	get_res(a), get_res(b);
	for (int i = 1;i <= n;++i)
		cout << res[i] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7424 KB Output is correct
2 Correct 9 ms 7552 KB Output is correct
3 Correct 9 ms 7552 KB Output is correct
4 Correct 12 ms 7680 KB Output is correct
5 Correct 10 ms 7552 KB Output is correct
6 Correct 10 ms 7808 KB Output is correct
7 Correct 10 ms 7680 KB Output is correct
8 Correct 10 ms 7552 KB Output is correct
9 Correct 10 ms 7552 KB Output is correct
10 Correct 10 ms 7552 KB Output is correct
11 Correct 9 ms 7552 KB Output is correct
12 Correct 9 ms 7552 KB Output is correct
13 Correct 9 ms 7680 KB Output is correct
14 Correct 10 ms 7680 KB Output is correct
15 Correct 10 ms 7680 KB Output is correct
16 Correct 11 ms 7552 KB Output is correct
17 Correct 10 ms 7680 KB Output is correct
18 Correct 9 ms 7680 KB Output is correct
19 Correct 10 ms 7552 KB Output is correct
20 Correct 9 ms 7808 KB Output is correct
21 Correct 10 ms 7680 KB Output is correct
22 Correct 9 ms 7552 KB Output is correct
23 Correct 9 ms 7680 KB Output is correct
24 Correct 9 ms 7680 KB Output is correct
25 Correct 11 ms 7552 KB Output is correct
26 Correct 9 ms 7552 KB Output is correct
27 Correct 9 ms 7680 KB Output is correct
28 Correct 10 ms 7680 KB Output is correct
29 Correct 9 ms 7680 KB Output is correct
30 Correct 9 ms 7552 KB Output is correct
31 Correct 9 ms 7680 KB Output is correct
32 Correct 10 ms 7680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 15608 KB Output is correct
2 Correct 176 ms 24952 KB Output is correct
3 Correct 32 ms 10616 KB Output is correct
4 Correct 292 ms 21880 KB Output is correct
5 Correct 389 ms 38264 KB Output is correct
6 Correct 364 ms 29796 KB Output is correct
7 Correct 278 ms 21876 KB Output is correct
8 Correct 311 ms 23416 KB Output is correct
9 Correct 297 ms 22904 KB Output is correct
10 Correct 305 ms 23076 KB Output is correct
11 Correct 148 ms 22504 KB Output is correct
12 Correct 363 ms 31920 KB Output is correct
13 Correct 309 ms 29684 KB Output is correct
14 Correct 323 ms 29404 KB Output is correct
15 Correct 133 ms 22380 KB Output is correct
16 Correct 303 ms 33016 KB Output is correct
17 Correct 334 ms 30068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 20088 KB Output is correct
2 Correct 386 ms 39288 KB Output is correct
3 Correct 37 ms 11256 KB Output is correct
4 Correct 305 ms 23544 KB Output is correct
5 Correct 418 ms 40812 KB Output is correct
6 Correct 375 ms 31736 KB Output is correct
7 Correct 300 ms 23544 KB Output is correct
8 Correct 342 ms 27000 KB Output is correct
9 Correct 336 ms 25848 KB Output is correct
10 Correct 320 ms 24700 KB Output is correct
11 Correct 247 ms 23924 KB Output is correct
12 Correct 397 ms 37112 KB Output is correct
13 Correct 333 ms 30812 KB Output is correct
14 Correct 347 ms 30832 KB Output is correct
15 Correct 151 ms 23532 KB Output is correct
16 Correct 342 ms 35184 KB Output is correct
17 Correct 358 ms 31952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7424 KB Output is correct
2 Correct 9 ms 7552 KB Output is correct
3 Correct 9 ms 7552 KB Output is correct
4 Correct 12 ms 7680 KB Output is correct
5 Correct 10 ms 7552 KB Output is correct
6 Correct 10 ms 7808 KB Output is correct
7 Correct 10 ms 7680 KB Output is correct
8 Correct 10 ms 7552 KB Output is correct
9 Correct 10 ms 7552 KB Output is correct
10 Correct 10 ms 7552 KB Output is correct
11 Correct 9 ms 7552 KB Output is correct
12 Correct 9 ms 7552 KB Output is correct
13 Correct 9 ms 7680 KB Output is correct
14 Correct 10 ms 7680 KB Output is correct
15 Correct 10 ms 7680 KB Output is correct
16 Correct 11 ms 7552 KB Output is correct
17 Correct 10 ms 7680 KB Output is correct
18 Correct 9 ms 7680 KB Output is correct
19 Correct 10 ms 7552 KB Output is correct
20 Correct 9 ms 7808 KB Output is correct
21 Correct 10 ms 7680 KB Output is correct
22 Correct 9 ms 7552 KB Output is correct
23 Correct 9 ms 7680 KB Output is correct
24 Correct 9 ms 7680 KB Output is correct
25 Correct 11 ms 7552 KB Output is correct
26 Correct 9 ms 7552 KB Output is correct
27 Correct 9 ms 7680 KB Output is correct
28 Correct 10 ms 7680 KB Output is correct
29 Correct 9 ms 7680 KB Output is correct
30 Correct 9 ms 7552 KB Output is correct
31 Correct 9 ms 7680 KB Output is correct
32 Correct 10 ms 7680 KB Output is correct
33 Correct 136 ms 15608 KB Output is correct
34 Correct 176 ms 24952 KB Output is correct
35 Correct 32 ms 10616 KB Output is correct
36 Correct 292 ms 21880 KB Output is correct
37 Correct 389 ms 38264 KB Output is correct
38 Correct 364 ms 29796 KB Output is correct
39 Correct 278 ms 21876 KB Output is correct
40 Correct 311 ms 23416 KB Output is correct
41 Correct 297 ms 22904 KB Output is correct
42 Correct 305 ms 23076 KB Output is correct
43 Correct 148 ms 22504 KB Output is correct
44 Correct 363 ms 31920 KB Output is correct
45 Correct 309 ms 29684 KB Output is correct
46 Correct 323 ms 29404 KB Output is correct
47 Correct 133 ms 22380 KB Output is correct
48 Correct 303 ms 33016 KB Output is correct
49 Correct 334 ms 30068 KB Output is correct
50 Correct 229 ms 20088 KB Output is correct
51 Correct 386 ms 39288 KB Output is correct
52 Correct 37 ms 11256 KB Output is correct
53 Correct 305 ms 23544 KB Output is correct
54 Correct 418 ms 40812 KB Output is correct
55 Correct 375 ms 31736 KB Output is correct
56 Correct 300 ms 23544 KB Output is correct
57 Correct 342 ms 27000 KB Output is correct
58 Correct 336 ms 25848 KB Output is correct
59 Correct 320 ms 24700 KB Output is correct
60 Correct 247 ms 23924 KB Output is correct
61 Correct 397 ms 37112 KB Output is correct
62 Correct 333 ms 30812 KB Output is correct
63 Correct 347 ms 30832 KB Output is correct
64 Correct 151 ms 23532 KB Output is correct
65 Correct 342 ms 35184 KB Output is correct
66 Correct 358 ms 31952 KB Output is correct
67 Correct 35 ms 9472 KB Output is correct
68 Correct 128 ms 21468 KB Output is correct
69 Correct 204 ms 21880 KB Output is correct
70 Correct 307 ms 21880 KB Output is correct
71 Correct 387 ms 38136 KB Output is correct
72 Correct 368 ms 29784 KB Output is correct
73 Correct 277 ms 21880 KB Output is correct
74 Correct 322 ms 24824 KB Output is correct
75 Correct 307 ms 23288 KB Output is correct
76 Correct 322 ms 23288 KB Output is correct
77 Correct 213 ms 22128 KB Output is correct
78 Correct 382 ms 33736 KB Output is correct
79 Correct 321 ms 31732 KB Output is correct
80 Correct 330 ms 28532 KB Output is correct
81 Correct 134 ms 22248 KB Output is correct
82 Correct 325 ms 32880 KB Output is correct
83 Correct 363 ms 29812 KB Output is correct
84 Correct 298 ms 22064 KB Output is correct
85 Correct 406 ms 38776 KB Output is correct
86 Correct 370 ms 30200 KB Output is correct
87 Correct 281 ms 22136 KB Output is correct
88 Correct 329 ms 25592 KB Output is correct
89 Correct 305 ms 23928 KB Output is correct
90 Correct 304 ms 23544 KB Output is correct
91 Correct 219 ms 22608 KB Output is correct
92 Correct 397 ms 38332 KB Output is correct
93 Correct 334 ms 28016 KB Output is correct
94 Correct 326 ms 26232 KB Output is correct
95 Correct 151 ms 22760 KB Output is correct
96 Correct 311 ms 33520 KB Output is correct
97 Correct 322 ms 30452 KB Output is correct
98 Correct 303 ms 23548 KB Output is correct
99 Correct 396 ms 39332 KB Output is correct
100 Correct 375 ms 31480 KB Output is correct
101 Correct 276 ms 22772 KB Output is correct
102 Correct 347 ms 25468 KB Output is correct
103 Correct 311 ms 24440 KB Output is correct
104 Correct 327 ms 24104 KB Output is correct
105 Correct 220 ms 22896 KB Output is correct
106 Correct 377 ms 32504 KB Output is correct
107 Correct 319 ms 32628 KB Output is correct
108 Correct 352 ms 28536 KB Output is correct
109 Correct 143 ms 23404 KB Output is correct
110 Correct 328 ms 34540 KB Output is correct
111 Correct 330 ms 31348 KB Output is correct