답안 #519520

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519520 2022-01-26T13:43:48 Z AriaH Unique Cities (JOI19_ho_t5) C++14
100 / 100
782 ms 75796 KB
/* I can do this all day */

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
typedef __int128 INT;

#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;

template < typename T > using Tree = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 2e5 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;

ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, m, fen[N], check[N], C[N], H[N], dp[N], mark[N], S[N], Ans[2][N], node[N];

void add(int i, int x)
{
	for(i += 3; i < N; i += i & -i) fen[i] += x;
}

int ask(int i, int ret = 0)
{
	if(i < 1) return 1;
	for(i += 3; i; i -= i & -i) ret += fen[i];
	return ret;
}

void upd(int l, int r, int x)
{
	l = max(1, l);
	if(l > r) return;
	add(l, x);
	add(r + 1, -x);
}

set < int > st;

vector < int > G[N];

void pre(int v, int P = 0)
{
	H[v] = H[P] + 1;
	dp[v] = 1;
	for(auto u : G[v])
	{
		if(u == P) continue;
		pre(u, v);
		dp[v] = max(dp[v], dp[u] + 1);
	}
}

bool cmp(int i, int j)
{
	return dp[i] < dp[j];
}

inline int Add(int v)
{
	if(!mark[C[v]])
	{
		mark[C[v]] = 1;
		st.insert({-H[v]});
		return 1;
	}
	return 0;
}

inline void Del(int v)
{
	if(st.count(-H[v]))
	{
		st.erase(-H[v]);
		mark[C[v]] = 0;
	}
}

void dfs(int v, int P, int tp)
{
	node[H[v]] = v;
	sort(all(G[v]), cmp);
	int Mx1 = 0, Mx2 = 0, id1 = -1;
	for(auto u : G[v])
	{
		if(u == P) continue;
		if(dp[u] > Mx1)
		{
			Mx2 = Mx1;
			Mx1 = dp[u];
			id1 = u;
		}
		else if(dp[u] > Mx2)
		{
			Mx2 = dp[u];
		}
		upd(H[v] - dp[u], H[v] - 1, 1);
	}
	for(auto u : G[v])
	{
		if(u == P || u == id1) continue;
		Ans[tp][u] = Ans[tp][v];
		upd(H[v] - dp[u], H[v] - 1, -1);
		dfs(u, v, tp);
		upd(H[v] - dp[u], H[v] - 1, 1);
	}
	if(id1 == -1)
	{
		Ans[tp][v] += Add(P);
		Del(P);
	}
	else
	{
		Ans[tp][id1] = Ans[tp][v];
		upd(H[v] - Mx1, H[v] - 1, -1);
		vector < int > vec;
		///printf("here v = %d id1 = %d Mx1 = %d get = %d %d\n", v, id1, Mx1, ask(H[v] - Mx1), ask(H[v] - Mx1 + 1));
		if(ask(H[v] - Mx1) == 0)
		{
			Ans[tp][id1] += Add(node[H[v] - Mx1]);
			vec.push_back(node[H[v] - Mx1]);
		}
		if(ask(H[v] - Mx1 + 1) == 0 && Mx1 > 1)
		{
			Ans[tp][id1] += Add(node[H[v] - Mx1 + 1]);
			vec.push_back(node[H[v] - Mx1 + 1]);
		}
		dfs(id1, v, tp);
		while(SZ(vec))
		{
			Del(vec.back());
			vec.pop_back();
		}
		upd(H[v] - Mx1, H[v] - 1, 1);
	}
	for(auto u : G[v])
	{
		if(u == P) continue;
		upd(H[v] - dp[u], H[v] - 1, -1);
	}
	///printf("v = %d Ans = %d\n", v, Ans[tp][v]);
}

int fir[N], sec[N];

int main()
{
	fast_io;
	cin >> n >> m;
	for(int i = 1; i < n; i ++)
	{
		int a, b;
		cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	for(int i = 1; i <= n; i ++)
	{
		cin >> C[i];
	}
	pre(1);
	int L = 0, R = 0;
	for(int i = 1; i <= n; i ++)
	{
		if(H[i] > H[L]) L = i;
	}
	pre(L);
	for(int i = 1; i <= n; i ++)
	{
		fir[i] = H[i];
		if(H[i] > H[R]) R = i;
	}
	pre(R);
	for(int i = 1; i <= n; i ++)
	{
		sec[i] = H[i];
		if(fir[i] > sec[i])
		{
			S[i] = 0;
		}
		else
		{
			S[i] = 1;
		}
	}
	pre(L);
	dfs(L, 0, 0);
	st.clear();
	memset(mark, 0, sizeof mark);
	memset(check, 0, sizeof check);
	pre(R);
	dfs(R, 0, 1);
	for(int i = 1; i <= n; i ++)
	{
		cout << Ans[S[i]][i] << "\n";
	}

	return 0;
}

/* check corner case(n = 1?), watch for negetive index or overflow */
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6604 KB Output is correct
2 Correct 7 ms 6732 KB Output is correct
3 Correct 6 ms 6952 KB Output is correct
4 Correct 6 ms 6960 KB Output is correct
5 Correct 6 ms 6732 KB Output is correct
6 Correct 6 ms 7212 KB Output is correct
7 Correct 6 ms 6988 KB Output is correct
8 Correct 6 ms 6700 KB Output is correct
9 Correct 6 ms 6828 KB Output is correct
10 Correct 7 ms 6812 KB Output is correct
11 Correct 8 ms 6732 KB Output is correct
12 Correct 7 ms 6692 KB Output is correct
13 Correct 7 ms 7116 KB Output is correct
14 Correct 7 ms 6948 KB Output is correct
15 Correct 6 ms 6860 KB Output is correct
16 Correct 5 ms 6732 KB Output is correct
17 Correct 6 ms 6988 KB Output is correct
18 Correct 7 ms 6988 KB Output is correct
19 Correct 6 ms 6732 KB Output is correct
20 Correct 7 ms 7208 KB Output is correct
21 Correct 6 ms 6956 KB Output is correct
22 Correct 8 ms 6704 KB Output is correct
23 Correct 9 ms 6924 KB Output is correct
24 Correct 6 ms 6860 KB Output is correct
25 Correct 6 ms 6732 KB Output is correct
26 Correct 5 ms 6828 KB Output is correct
27 Correct 7 ms 7116 KB Output is correct
28 Correct 6 ms 7056 KB Output is correct
29 Correct 6 ms 6952 KB Output is correct
30 Correct 6 ms 6732 KB Output is correct
31 Correct 6 ms 7064 KB Output is correct
32 Correct 8 ms 6988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 15700 KB Output is correct
2 Correct 283 ms 39888 KB Output is correct
3 Correct 41 ms 11700 KB Output is correct
4 Correct 404 ms 22596 KB Output is correct
5 Correct 599 ms 64676 KB Output is correct
6 Correct 557 ms 42880 KB Output is correct
7 Correct 382 ms 22592 KB Output is correct
8 Correct 433 ms 26524 KB Output is correct
9 Correct 396 ms 25248 KB Output is correct
10 Correct 446 ms 25120 KB Output is correct
11 Correct 310 ms 23276 KB Output is correct
12 Correct 530 ms 48460 KB Output is correct
13 Correct 459 ms 42896 KB Output is correct
14 Correct 498 ms 41064 KB Output is correct
15 Correct 252 ms 23076 KB Output is correct
16 Correct 461 ms 50908 KB Output is correct
17 Correct 504 ms 42820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 308 ms 19828 KB Output is correct
2 Correct 673 ms 72780 KB Output is correct
3 Correct 49 ms 12844 KB Output is correct
4 Correct 446 ms 23468 KB Output is correct
5 Correct 782 ms 75796 KB Output is correct
6 Correct 606 ms 46276 KB Output is correct
7 Correct 436 ms 23372 KB Output is correct
8 Correct 454 ms 31580 KB Output is correct
9 Correct 449 ms 28756 KB Output is correct
10 Correct 440 ms 26468 KB Output is correct
11 Correct 343 ms 23760 KB Output is correct
12 Correct 611 ms 63384 KB Output is correct
13 Correct 482 ms 43452 KB Output is correct
14 Correct 532 ms 43200 KB Output is correct
15 Correct 236 ms 24112 KB Output is correct
16 Correct 586 ms 58648 KB Output is correct
17 Correct 569 ms 46416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6604 KB Output is correct
2 Correct 7 ms 6732 KB Output is correct
3 Correct 6 ms 6952 KB Output is correct
4 Correct 6 ms 6960 KB Output is correct
5 Correct 6 ms 6732 KB Output is correct
6 Correct 6 ms 7212 KB Output is correct
7 Correct 6 ms 6988 KB Output is correct
8 Correct 6 ms 6700 KB Output is correct
9 Correct 6 ms 6828 KB Output is correct
10 Correct 7 ms 6812 KB Output is correct
11 Correct 8 ms 6732 KB Output is correct
12 Correct 7 ms 6692 KB Output is correct
13 Correct 7 ms 7116 KB Output is correct
14 Correct 7 ms 6948 KB Output is correct
15 Correct 6 ms 6860 KB Output is correct
16 Correct 5 ms 6732 KB Output is correct
17 Correct 6 ms 6988 KB Output is correct
18 Correct 7 ms 6988 KB Output is correct
19 Correct 6 ms 6732 KB Output is correct
20 Correct 7 ms 7208 KB Output is correct
21 Correct 6 ms 6956 KB Output is correct
22 Correct 8 ms 6704 KB Output is correct
23 Correct 9 ms 6924 KB Output is correct
24 Correct 6 ms 6860 KB Output is correct
25 Correct 6 ms 6732 KB Output is correct
26 Correct 5 ms 6828 KB Output is correct
27 Correct 7 ms 7116 KB Output is correct
28 Correct 6 ms 7056 KB Output is correct
29 Correct 6 ms 6952 KB Output is correct
30 Correct 6 ms 6732 KB Output is correct
31 Correct 6 ms 7064 KB Output is correct
32 Correct 8 ms 6988 KB Output is correct
33 Correct 199 ms 15700 KB Output is correct
34 Correct 283 ms 39888 KB Output is correct
35 Correct 41 ms 11700 KB Output is correct
36 Correct 404 ms 22596 KB Output is correct
37 Correct 599 ms 64676 KB Output is correct
38 Correct 557 ms 42880 KB Output is correct
39 Correct 382 ms 22592 KB Output is correct
40 Correct 433 ms 26524 KB Output is correct
41 Correct 396 ms 25248 KB Output is correct
42 Correct 446 ms 25120 KB Output is correct
43 Correct 310 ms 23276 KB Output is correct
44 Correct 530 ms 48460 KB Output is correct
45 Correct 459 ms 42896 KB Output is correct
46 Correct 498 ms 41064 KB Output is correct
47 Correct 252 ms 23076 KB Output is correct
48 Correct 461 ms 50908 KB Output is correct
49 Correct 504 ms 42820 KB Output is correct
50 Correct 308 ms 19828 KB Output is correct
51 Correct 673 ms 72780 KB Output is correct
52 Correct 49 ms 12844 KB Output is correct
53 Correct 446 ms 23468 KB Output is correct
54 Correct 782 ms 75796 KB Output is correct
55 Correct 606 ms 46276 KB Output is correct
56 Correct 436 ms 23372 KB Output is correct
57 Correct 454 ms 31580 KB Output is correct
58 Correct 449 ms 28756 KB Output is correct
59 Correct 440 ms 26468 KB Output is correct
60 Correct 343 ms 23760 KB Output is correct
61 Correct 611 ms 63384 KB Output is correct
62 Correct 482 ms 43452 KB Output is correct
63 Correct 532 ms 43200 KB Output is correct
64 Correct 236 ms 24112 KB Output is correct
65 Correct 586 ms 58648 KB Output is correct
66 Correct 569 ms 46416 KB Output is correct
67 Correct 40 ms 8832 KB Output is correct
68 Correct 209 ms 33732 KB Output is correct
69 Correct 309 ms 30280 KB Output is correct
70 Correct 458 ms 22616 KB Output is correct
71 Correct 586 ms 64804 KB Output is correct
72 Correct 548 ms 42764 KB Output is correct
73 Correct 387 ms 22624 KB Output is correct
74 Correct 491 ms 30196 KB Output is correct
75 Correct 432 ms 26088 KB Output is correct
76 Correct 450 ms 25512 KB Output is correct
77 Correct 360 ms 22820 KB Output is correct
78 Correct 525 ms 53040 KB Output is correct
79 Correct 448 ms 47964 KB Output is correct
80 Correct 486 ms 38860 KB Output is correct
81 Correct 239 ms 22960 KB Output is correct
82 Correct 441 ms 50748 KB Output is correct
83 Correct 462 ms 42788 KB Output is correct
84 Correct 430 ms 22836 KB Output is correct
85 Correct 580 ms 65308 KB Output is correct
86 Correct 567 ms 43224 KB Output is correct
87 Correct 439 ms 22968 KB Output is correct
88 Correct 445 ms 30992 KB Output is correct
89 Correct 460 ms 27088 KB Output is correct
90 Correct 513 ms 25996 KB Output is correct
91 Correct 339 ms 23252 KB Output is correct
92 Correct 597 ms 63900 KB Output is correct
93 Correct 461 ms 36940 KB Output is correct
94 Correct 449 ms 33032 KB Output is correct
95 Correct 247 ms 23440 KB Output is correct
96 Correct 503 ms 51436 KB Output is correct
97 Correct 506 ms 43456 KB Output is correct
98 Correct 463 ms 23608 KB Output is correct
99 Correct 597 ms 65812 KB Output is correct
100 Correct 591 ms 45796 KB Output is correct
101 Correct 439 ms 23612 KB Output is correct
102 Correct 466 ms 29776 KB Output is correct
103 Correct 481 ms 27212 KB Output is correct
104 Correct 464 ms 26412 KB Output is correct
105 Correct 353 ms 23584 KB Output is correct
106 Correct 590 ms 48380 KB Output is correct
107 Correct 497 ms 48824 KB Output is correct
108 Correct 529 ms 36400 KB Output is correct
109 Correct 245 ms 24072 KB Output is correct
110 Correct 488 ms 53392 KB Output is correct
111 Correct 562 ms 45796 KB Output is correct