Submission #114289

# Submission time Handle Problem Language Result Execution time Memory
114289 2019-05-31T17:25:51 Z RockyB Unique Cities (JOI19_ho_t5) C++17
100 / 100
764 ms 44536 KB
/// In The Name Of God

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)

#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define nl '\n'
#define ioi exit(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int N = (int)2e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;

const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

using namespace std;

int n, m;
int a[N];

vector <int> g[N];

struct S {
	int sz, df;
	int st[N], cnt[N];

	void add(int c) {
		df += cnt[c]++ == 0;
	}
	void del(int c) {
		df -= --cnt[c] == 0;
	}
	void push(int v) {
		st[++sz] = v;
		add(a[v]);
	}
	void pop() {
		del(a[st[sz--]]);
	}
	bool empty() {
		return sz == 0;
	}
	int back() {
		return st[sz];
	}
} T;

inline void dfs(int v, int p, vector <int> &dis) {
	dis[v] = dis[p] + 1;
	for (auto to : g[v]) {
		if (to != p) dfs(to, v, dis);
	}
}
int d[N], ans[N];
inline void prec(int v, int p) {
	d[v] = 1;
	for (auto to : g[v]) {
		if (to != p) {
			prec(to, v);
			d[v] = max(d[v], d[to] + 1);
		}
	}
}
inline void solve(int v, int p, vector <int> &dep) {
	vector < pair <int, int > > ch;
	for (auto to : g[v]) {
		if (to != p) {
			ch.pb({d[to], to});
		}
	}
	if (sz(ch)) swap(ch[0], *max_element(all(ch)));
	int len = (sz(ch) > 1) ? max_element(ch.begin() + 1, ch.end()) -> f : 0;
	if (sz(ch)) {
		for (auto to : ch) {
			while (!T.empty() && dep[T.back()] >= dep[v] - len) T.pop();
			T.push(v);
			solve(to.s, v, dep);
			if (!T.empty() && T.back() == v) T.pop();
			len = max(len, to.f);
		}
	}
	while (!T.empty() && dep[T.back()] >= dep[v] - len) T.pop();
	// if (v == 1) {
	// 	cerr << T.back() << ' ' << dep[T.back()] << ' ' << dep[v] << nl;
	// 	cerr << len << " -> " << sz(ch) << " -> " << T.sz << " -> ";
	// 	rep(i, 1, T.sz) cerr << T.st[i] << ' ';
	// 	cerr << nl;
	// }
	ans[v] = max(ans[v], T.df);
}
int main() {
	#ifdef IOI
		freopen ("in.txt", "r", stdin);
		freopen ("res.txt", "w", stdout);
	#endif
	Kazakhstan
	cin >> n >> m;
	rep(i, 2, n) {
		int v, u;
		cin >> v >> u;
		g[v].pb(u);
		g[u].pb(v);
	}
	rep(i, 1, n) {
		cin >> a[i];
	}
	vector <int> d1(n + 1);
	dfs(1, 0, d1);
	
	int A = max_element(all(d1)) - d1.begin();
	prec(A, 0);
	dfs(A, 0, d1);
	solve(A, 0, d1);

	int B = max_element(all(d1)) - d1.begin();
	prec(B, 0);
	dfs(B, 0, d1);
	solve(B, 0, d1);

	rep(i, 1, n) {
		cout << ans[i] << nl;
	}
	ioi
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 6 ms 5248 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 8 ms 5248 KB Output is correct
6 Correct 8 ms 5504 KB Output is correct
7 Correct 8 ms 5376 KB Output is correct
8 Correct 8 ms 5248 KB Output is correct
9 Correct 9 ms 5248 KB Output is correct
10 Correct 8 ms 5248 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 8 ms 5504 KB Output is correct
14 Correct 7 ms 5248 KB Output is correct
15 Correct 10 ms 5248 KB Output is correct
16 Correct 7 ms 5248 KB Output is correct
17 Correct 8 ms 5376 KB Output is correct
18 Correct 11 ms 5248 KB Output is correct
19 Correct 8 ms 5248 KB Output is correct
20 Correct 7 ms 5504 KB Output is correct
21 Correct 8 ms 5376 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5248 KB Output is correct
24 Correct 7 ms 5248 KB Output is correct
25 Correct 9 ms 5248 KB Output is correct
26 Correct 8 ms 5248 KB Output is correct
27 Correct 7 ms 5376 KB Output is correct
28 Correct 7 ms 5376 KB Output is correct
29 Correct 7 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 8 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 239 ms 11384 KB Output is correct
2 Correct 300 ms 26996 KB Output is correct
3 Correct 36 ms 8700 KB Output is correct
4 Correct 518 ms 15708 KB Output is correct
5 Correct 716 ms 43052 KB Output is correct
6 Correct 698 ms 29048 KB Output is correct
7 Correct 514 ms 16080 KB Output is correct
8 Correct 517 ms 18588 KB Output is correct
9 Correct 566 ms 17504 KB Output is correct
10 Correct 577 ms 17540 KB Output is correct
11 Correct 263 ms 19344 KB Output is correct
12 Correct 660 ms 32584 KB Output is correct
13 Correct 602 ms 28800 KB Output is correct
14 Correct 623 ms 28168 KB Output is correct
15 Correct 211 ms 19520 KB Output is correct
16 Correct 589 ms 34156 KB Output is correct
17 Correct 615 ms 29324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 389 ms 14200 KB Output is correct
2 Correct 741 ms 42872 KB Output is correct
3 Correct 42 ms 9464 KB Output is correct
4 Correct 502 ms 16380 KB Output is correct
5 Correct 764 ms 44536 KB Output is correct
6 Correct 712 ms 30132 KB Output is correct
7 Correct 480 ms 16672 KB Output is correct
8 Correct 532 ms 22264 KB Output is correct
9 Correct 537 ms 20256 KB Output is correct
10 Correct 509 ms 18564 KB Output is correct
11 Correct 332 ms 18136 KB Output is correct
12 Correct 727 ms 38856 KB Output is correct
13 Correct 529 ms 27884 KB Output is correct
14 Correct 559 ms 28056 KB Output is correct
15 Correct 198 ms 19528 KB Output is correct
16 Correct 620 ms 34976 KB Output is correct
17 Correct 578 ms 30024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 6 ms 5248 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 8 ms 5248 KB Output is correct
6 Correct 8 ms 5504 KB Output is correct
7 Correct 8 ms 5376 KB Output is correct
8 Correct 8 ms 5248 KB Output is correct
9 Correct 9 ms 5248 KB Output is correct
10 Correct 8 ms 5248 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 8 ms 5504 KB Output is correct
14 Correct 7 ms 5248 KB Output is correct
15 Correct 10 ms 5248 KB Output is correct
16 Correct 7 ms 5248 KB Output is correct
17 Correct 8 ms 5376 KB Output is correct
18 Correct 11 ms 5248 KB Output is correct
19 Correct 8 ms 5248 KB Output is correct
20 Correct 7 ms 5504 KB Output is correct
21 Correct 8 ms 5376 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5248 KB Output is correct
24 Correct 7 ms 5248 KB Output is correct
25 Correct 9 ms 5248 KB Output is correct
26 Correct 8 ms 5248 KB Output is correct
27 Correct 7 ms 5376 KB Output is correct
28 Correct 7 ms 5376 KB Output is correct
29 Correct 7 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 8 ms 5504 KB Output is correct
33 Correct 239 ms 11384 KB Output is correct
34 Correct 300 ms 26996 KB Output is correct
35 Correct 36 ms 8700 KB Output is correct
36 Correct 518 ms 15708 KB Output is correct
37 Correct 716 ms 43052 KB Output is correct
38 Correct 698 ms 29048 KB Output is correct
39 Correct 514 ms 16080 KB Output is correct
40 Correct 517 ms 18588 KB Output is correct
41 Correct 566 ms 17504 KB Output is correct
42 Correct 577 ms 17540 KB Output is correct
43 Correct 263 ms 19344 KB Output is correct
44 Correct 660 ms 32584 KB Output is correct
45 Correct 602 ms 28800 KB Output is correct
46 Correct 623 ms 28168 KB Output is correct
47 Correct 211 ms 19520 KB Output is correct
48 Correct 589 ms 34156 KB Output is correct
49 Correct 615 ms 29324 KB Output is correct
50 Correct 389 ms 14200 KB Output is correct
51 Correct 741 ms 42872 KB Output is correct
52 Correct 42 ms 9464 KB Output is correct
53 Correct 502 ms 16380 KB Output is correct
54 Correct 764 ms 44536 KB Output is correct
55 Correct 712 ms 30132 KB Output is correct
56 Correct 480 ms 16672 KB Output is correct
57 Correct 532 ms 22264 KB Output is correct
58 Correct 537 ms 20256 KB Output is correct
59 Correct 509 ms 18564 KB Output is correct
60 Correct 332 ms 18136 KB Output is correct
61 Correct 727 ms 38856 KB Output is correct
62 Correct 529 ms 27884 KB Output is correct
63 Correct 559 ms 28056 KB Output is correct
64 Correct 198 ms 19528 KB Output is correct
65 Correct 620 ms 34976 KB Output is correct
66 Correct 578 ms 30024 KB Output is correct
67 Correct 35 ms 6912 KB Output is correct
68 Correct 241 ms 22396 KB Output is correct
69 Correct 337 ms 20560 KB Output is correct
70 Correct 532 ms 15736 KB Output is correct
71 Correct 703 ms 43000 KB Output is correct
72 Correct 585 ms 29052 KB Output is correct
73 Correct 488 ms 15716 KB Output is correct
74 Correct 504 ms 20984 KB Output is correct
75 Correct 524 ms 17900 KB Output is correct
76 Correct 515 ms 17604 KB Output is correct
77 Correct 337 ms 17584 KB Output is correct
78 Correct 642 ms 35576 KB Output is correct
79 Correct 556 ms 32116 KB Output is correct
80 Correct 565 ms 26244 KB Output is correct
81 Correct 190 ms 19276 KB Output is correct
82 Correct 518 ms 34032 KB Output is correct
83 Correct 579 ms 29260 KB Output is correct
84 Correct 525 ms 15584 KB Output is correct
85 Correct 653 ms 43132 KB Output is correct
86 Correct 643 ms 29176 KB Output is correct
87 Correct 529 ms 15872 KB Output is correct
88 Correct 508 ms 21368 KB Output is correct
89 Correct 499 ms 18504 KB Output is correct
90 Correct 557 ms 17584 KB Output is correct
91 Correct 337 ms 17392 KB Output is correct
92 Correct 672 ms 42232 KB Output is correct
93 Correct 524 ms 24580 KB Output is correct
94 Correct 510 ms 22132 KB Output is correct
95 Correct 205 ms 19400 KB Output is correct
96 Correct 543 ms 34024 KB Output is correct
97 Correct 579 ms 29216 KB Output is correct
98 Correct 526 ms 16348 KB Output is correct
99 Correct 656 ms 43384 KB Output is correct
100 Correct 627 ms 29740 KB Output is correct
101 Correct 471 ms 16188 KB Output is correct
102 Correct 601 ms 20316 KB Output is correct
103 Correct 521 ms 18400 KB Output is correct
104 Correct 497 ms 17808 KB Output is correct
105 Correct 320 ms 17512 KB Output is correct
106 Correct 640 ms 31508 KB Output is correct
107 Correct 487 ms 31492 KB Output is correct
108 Correct 586 ms 24104 KB Output is correct
109 Correct 200 ms 19560 KB Output is correct
110 Correct 509 ms 34024 KB Output is correct
111 Correct 621 ms 29296 KB Output is correct