Submission #130103

# Submission time Handle Problem Language Result Execution time Memory
130103 2019-07-14T00:10:47 Z qkxwsm Unique Cities (JOI19_ho_t5) C++14
100 / 100
543 ms 39316 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 200013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, M, S, T, D;
int arr[MAXN];
vi edge[MAXN];
int depth[MAXN], h[MAXN];
int dist[2][MAXN];
int freq[MAXN];
int ans[2][MAXN];
int stor;
vi cur;

void ins(int u)
{
	cur.PB(u);
	if (freq[arr[u]]) stor--;
	freq[arr[u]]++;
	stor++;
}
void del()
{
	int u = cur.back(); cur.pop_back();
	stor--;
	freq[arr[u]]--;
	if (freq[arr[u]]) stor++;
}
void dfs(int u, int p)
{
	h[u] = 0;
	for (int v : edge[u])
	{
		if (v == p) continue;
		depth[v] = depth[u] + 1;
		dfs(v, u);
		ckmax(h[u], h[v] + 1);
	}
}
void solve(int u, int p, bool f)
{
	int mx2 = 0, c = -1;
	for (int v : edge[u])
	{
		if (v == p) continue;
		if (h[v] == h[u] - 1)
		{
			c = v;
			break;
		}
	}
	for (int v : edge[u])
	{
		if (v == p || v == c) continue;
		ckmax(mx2, h[v] + 1);
	}
	if (c != -1)
	{
		while(!cur.empty() && depth[cur.back()] >= depth[u] - mx2) del();
		ins(u);
		solve(c, u, f);
	}
	for (int v : edge[u])
	{
		if (v == p || v == c) continue;
		while(!cur.empty() && depth[cur.back()] >= depth[u] - h[u]) del();
		ins(u);
		solve(v, u, f);
	}
	while(!cur.empty() && depth[cur.back()] >= depth[u] - h[u]) del();
	ans[f][u] = stor;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> M;
	FOR(i, 0, N - 1)
	{
		int u, v; cin >> u >> v; u--; v--;
		edge[u].PB(v);
		edge[v].PB(u);
	}
	FOR(i, 0, N)
	{
		cin >> arr[i]; arr[i]--;
	}
	depth[0] = 0;
	dfs(0, N);
	FOR(i, 0, N)
	{
		if (depth[i] > depth[S]) S = i;
	}
	depth[S] = 0;
	dfs(S, N);
	FOR(i, 0, N)
	{
		if (depth[i] > depth[T]) T = i;
	}
	FOR(i, 0, N)
	{
		dist[0][i] = depth[i];
	}
	// cerr << "S = " << S << endl;
	solve(S, N, 0);
	while(!cur.empty()) del();
	depth[T] = 0;
	dfs(T, N);
	FOR(i, 0, N)
	{
		dist[1][i] = depth[i];
	}
	solve(T, N, 1);
	// cerr << "T = " << T << endl;
	FOR(i, 0, N)
	{
		// cerr << dist[0][i] << ' ' << dist[1][i] << ' ' << ans[0][i] << ' ' << ans[1][i] << endl;
		cout << (dist[0][i] >= dist[1][i] ? ans[0][i] : ans[1][i]) << '\n';
	}
	return 0;
	//then what do you do? dfsfrom S≤ and maintain unique cities
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 8 ms 5240 KB Output is correct
6 Correct 8 ms 5368 KB Output is correct
7 Correct 8 ms 5368 KB Output is correct
8 Correct 8 ms 5240 KB Output is correct
9 Correct 8 ms 5240 KB Output is correct
10 Correct 17 ms 5260 KB Output is correct
11 Correct 7 ms 5240 KB Output is correct
12 Correct 8 ms 5240 KB Output is correct
13 Correct 8 ms 5368 KB Output is correct
14 Correct 9 ms 5240 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 7 ms 5240 KB Output is correct
17 Correct 8 ms 5368 KB Output is correct
18 Correct 8 ms 5368 KB Output is correct
19 Correct 7 ms 5240 KB Output is correct
20 Correct 8 ms 5368 KB Output is correct
21 Correct 8 ms 5368 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 8 ms 5240 KB Output is correct
24 Correct 8 ms 5240 KB Output is correct
25 Correct 8 ms 5368 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 8 ms 5368 KB Output is correct
28 Correct 8 ms 5368 KB Output is correct
29 Correct 9 ms 5264 KB Output is correct
30 Correct 7 ms 5228 KB Output is correct
31 Correct 8 ms 5368 KB Output is correct
32 Correct 8 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 12152 KB Output is correct
2 Correct 217 ms 21676 KB Output is correct
3 Correct 36 ms 8044 KB Output is correct
4 Correct 382 ms 20216 KB Output is correct
5 Correct 482 ms 36700 KB Output is correct
6 Correct 459 ms 28308 KB Output is correct
7 Correct 335 ms 20428 KB Output is correct
8 Correct 344 ms 21888 KB Output is correct
9 Correct 394 ms 21352 KB Output is correct
10 Correct 355 ms 21280 KB Output is correct
11 Correct 183 ms 21024 KB Output is correct
12 Correct 419 ms 30320 KB Output is correct
13 Correct 362 ms 28272 KB Output is correct
14 Correct 379 ms 27784 KB Output is correct
15 Correct 182 ms 20840 KB Output is correct
16 Correct 393 ms 31576 KB Output is correct
17 Correct 350 ms 28532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 15408 KB Output is correct
2 Correct 448 ms 34256 KB Output is correct
3 Correct 40 ms 8440 KB Output is correct
4 Correct 374 ms 22008 KB Output is correct
5 Correct 503 ms 39316 KB Output is correct
6 Correct 447 ms 30248 KB Output is correct
7 Correct 353 ms 21880 KB Output is correct
8 Correct 404 ms 25428 KB Output is correct
9 Correct 398 ms 24312 KB Output is correct
10 Correct 401 ms 23160 KB Output is correct
11 Correct 295 ms 22260 KB Output is correct
12 Correct 452 ms 35824 KB Output is correct
13 Correct 460 ms 29300 KB Output is correct
14 Correct 376 ms 29296 KB Output is correct
15 Correct 186 ms 21864 KB Output is correct
16 Correct 380 ms 33800 KB Output is correct
17 Correct 369 ms 30452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 8 ms 5240 KB Output is correct
6 Correct 8 ms 5368 KB Output is correct
7 Correct 8 ms 5368 KB Output is correct
8 Correct 8 ms 5240 KB Output is correct
9 Correct 8 ms 5240 KB Output is correct
10 Correct 17 ms 5260 KB Output is correct
11 Correct 7 ms 5240 KB Output is correct
12 Correct 8 ms 5240 KB Output is correct
13 Correct 8 ms 5368 KB Output is correct
14 Correct 9 ms 5240 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 7 ms 5240 KB Output is correct
17 Correct 8 ms 5368 KB Output is correct
18 Correct 8 ms 5368 KB Output is correct
19 Correct 7 ms 5240 KB Output is correct
20 Correct 8 ms 5368 KB Output is correct
21 Correct 8 ms 5368 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 8 ms 5240 KB Output is correct
24 Correct 8 ms 5240 KB Output is correct
25 Correct 8 ms 5368 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 8 ms 5368 KB Output is correct
28 Correct 8 ms 5368 KB Output is correct
29 Correct 9 ms 5264 KB Output is correct
30 Correct 7 ms 5228 KB Output is correct
31 Correct 8 ms 5368 KB Output is correct
32 Correct 8 ms 5368 KB Output is correct
33 Correct 159 ms 12152 KB Output is correct
34 Correct 217 ms 21676 KB Output is correct
35 Correct 36 ms 8044 KB Output is correct
36 Correct 382 ms 20216 KB Output is correct
37 Correct 482 ms 36700 KB Output is correct
38 Correct 459 ms 28308 KB Output is correct
39 Correct 335 ms 20428 KB Output is correct
40 Correct 344 ms 21888 KB Output is correct
41 Correct 394 ms 21352 KB Output is correct
42 Correct 355 ms 21280 KB Output is correct
43 Correct 183 ms 21024 KB Output is correct
44 Correct 419 ms 30320 KB Output is correct
45 Correct 362 ms 28272 KB Output is correct
46 Correct 379 ms 27784 KB Output is correct
47 Correct 182 ms 20840 KB Output is correct
48 Correct 393 ms 31576 KB Output is correct
49 Correct 350 ms 28532 KB Output is correct
50 Correct 306 ms 15408 KB Output is correct
51 Correct 448 ms 34256 KB Output is correct
52 Correct 40 ms 8440 KB Output is correct
53 Correct 374 ms 22008 KB Output is correct
54 Correct 503 ms 39316 KB Output is correct
55 Correct 447 ms 30248 KB Output is correct
56 Correct 353 ms 21880 KB Output is correct
57 Correct 404 ms 25428 KB Output is correct
58 Correct 398 ms 24312 KB Output is correct
59 Correct 401 ms 23160 KB Output is correct
60 Correct 295 ms 22260 KB Output is correct
61 Correct 452 ms 35824 KB Output is correct
62 Correct 460 ms 29300 KB Output is correct
63 Correct 376 ms 29296 KB Output is correct
64 Correct 186 ms 21864 KB Output is correct
65 Correct 380 ms 33800 KB Output is correct
66 Correct 369 ms 30452 KB Output is correct
67 Correct 34 ms 7332 KB Output is correct
68 Correct 154 ms 19568 KB Output is correct
69 Correct 210 ms 20068 KB Output is correct
70 Correct 383 ms 20360 KB Output is correct
71 Correct 479 ms 36716 KB Output is correct
72 Correct 474 ms 28532 KB Output is correct
73 Correct 330 ms 20316 KB Output is correct
74 Correct 362 ms 23352 KB Output is correct
75 Correct 352 ms 21676 KB Output is correct
76 Correct 378 ms 21488 KB Output is correct
77 Correct 254 ms 20700 KB Output is correct
78 Correct 434 ms 32112 KB Output is correct
79 Correct 356 ms 30448 KB Output is correct
80 Correct 397 ms 26868 KB Output is correct
81 Correct 191 ms 20624 KB Output is correct
82 Correct 381 ms 31392 KB Output is correct
83 Correct 454 ms 28692 KB Output is correct
84 Correct 520 ms 20520 KB Output is correct
85 Correct 481 ms 37368 KB Output is correct
86 Correct 440 ms 28888 KB Output is correct
87 Correct 354 ms 20700 KB Output is correct
88 Correct 386 ms 24004 KB Output is correct
89 Correct 372 ms 22452 KB Output is correct
90 Correct 414 ms 21920 KB Output is correct
91 Correct 259 ms 20972 KB Output is correct
92 Correct 450 ms 36844 KB Output is correct
93 Correct 392 ms 26392 KB Output is correct
94 Correct 507 ms 24968 KB Output is correct
95 Correct 187 ms 21096 KB Output is correct
96 Correct 366 ms 31944 KB Output is correct
97 Correct 543 ms 29220 KB Output is correct
98 Correct 409 ms 21944 KB Output is correct
99 Correct 496 ms 37812 KB Output is correct
100 Correct 444 ms 30068 KB Output is correct
101 Correct 332 ms 21192 KB Output is correct
102 Correct 393 ms 23944 KB Output is correct
103 Correct 403 ms 22728 KB Output is correct
104 Correct 379 ms 22536 KB Output is correct
105 Correct 249 ms 21360 KB Output is correct
106 Correct 404 ms 30836 KB Output is correct
107 Correct 370 ms 31100 KB Output is correct
108 Correct 456 ms 27016 KB Output is correct
109 Correct 177 ms 21740 KB Output is correct
110 Correct 378 ms 32924 KB Output is correct
111 Correct 380 ms 29892 KB Output is correct