답안 #439166

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
439166 2021-06-29T10:56:05 Z flappybird Unique Cities (JOI19_ho_t5) C++14
4 / 100
2000 ms 119432 KB
#include <bits/stdc++.h>
#include <unordered_map>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef int ll;
typedef pair<ll, ll> pll;
#define MAX 201010
#define MAXS 18
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
struct segtree {
	ll N;
	ll s;
	vector<ll> tree, l, r;
	void update(ll x, ll a) {
		x += s - 1;
		tree[x] = a;
		x /= 2;
		while (x) tree[x] = max(tree[x << 1], tree[(x << 1) + 1]), x >>= 1;
	}
	ll query(ll low, ll high, ll loc = 1) {
		if (l[loc] == low && r[loc] == high) return tree[loc];
		if (r[loc << 1] >= high) return query(low, high, loc << 1);
		if (l[(loc << 1) + 1] <= low) return query(low, high, (loc << 1) + 1);
		return max(query(low, r[loc << 1], loc << 1), query(l[(loc << 1) + 1], high, (loc << 1) + 1));
	}
	void init(ll x = 1) {
		if (x >= s) {
			l[x] = r[x] = x - s + 1;
			return;
		}
		init(x * 2);
		init(x * 2 + 1);
		l[x] = l[x << 1];
		r[x] = r[x * 2 + 1];
	}
	void initf(ll n) {
		N = n;
		s = (ll)1 << (ll)ceil(log2(N));
		tree.resize(2 * s + 1);
		l.resize(2 * s + 1);
		r.resize(2 * s + 1);
		init();
	}
	segtree() {}
};
vector<ll> adj[MAX], sav[MAX], savdis[MAX], rev[MAX];
ll dir[MAX], ddir[MAX], dddir[MAX];
vector<vector<ll>> chain;
vector<set<ll>> subtree, revtree;
vector<segtree> chainseg;
ll C[MAX], depth[MAX], mxdepv[MAX];
ll mxdep[MAX];
ll sp[MAX][MAXS];
ll cnt;
ll num[MAX];
pll cnum[MAX];
ll ans[MAX];
ll arr[MAX];
ll prtind[MAX];
ll init(ll x, ll p = 0, ll d = 0) {
	sp[x][0] = p;
	ll i;
	for (i = 1; i < MAXS; i++) sp[x][i] = sp[sp[x][i - 1]][i - 1];
	depth[x] = d;
	ll sum = 1;
	sav[x].resize(adj[x].size());
	savdis[x].resize(adj[x].size());
	rev[x].resize(adj[x].size());
	for (i = 0; i < adj[x].size(); i++) {
		if (adj[x][i] == p) prtind[x] = i;
		else sum += init(adj[x][i], x, d + 1);
	}
	return num[x] = sum;
}
void calc(ll x, ll p = 0) {
	ll i;
	ll mx = 0;
	mxdepv[x] = x;
	for (i = 0; i < adj[x].size(); i++) {
		if (adj[x][i] == p) continue;
		calc(adj[x][i], x);
		if (mx < depth[mxdepv[adj[x][i]]]) mx = depth[mxdepv[adj[x][i]]], mxdepv[x] = mxdepv[adj[x][i]];
		sav[x][i] = mxdepv[adj[x][i]];
		savdis[x][i] = depth[sav[x][i]] - depth[x];
	}
	ll cnt = 0;
	ll vv, vvv;
	vv = vvv = -1;
	ll nv = -1;
	ll nmx = 0;
	for (auto v : adj[x]) {
		if (v == p) continue;
		if (depth[mxdepv[v]] == mx) cnt++, vvv = vv, vv = v;
		else if (depth[mxdepv[v]] > nmx) nmx = depth[mxdepv[v]], nv = v;
	}
	if (cnt >= 2) {
		for (i = 0; i < adj[x].size(); i++) {
			if (adj[x][i] != p) {
				rev[x][i] = (adj[x][i] == vv ? vvv : vv);
			}
		}
	}
	else {
		for (i = 0; i < adj[x].size(); i++) {
			if (adj[x][i] != p) {
				rev[x][i] = (adj[x][i] == vv ? nv : vv);
			}
		}
	}
	for (i = 0; i < adj[x].size(); i++) {
		if (adj[x][i] == p) continue;
		if (rev[x][i] == -1) rev[x][i] = x;
		else rev[x][i] = mxdepv[rev[x][i]];
	}
}
void make_chain(ll x, ll p = 0) {
	ll mx, mv;
	mx = mv = 0;
	chain[cnt].push_back(x);
	cnum[x] = { cnt, chain[cnt].size() - 1 };
	for (auto v : adj[x]) {
		if (v == p) continue;
		if (mx < num[v]) mx = num[v], mv = v;
	}
	if (mv) make_chain(mv, x);
	for (auto v : adj[x]) {
		if (v == p || v == mv) continue;
		cnt++;
		chain.push_back(vector<ll>());
		make_chain(v, x);
	}
}
void make_tree() {
	ll i;
	chainseg.resize(chain.size());
	for (i = 0; i < chain.size(); i++) chainseg[i].initf(chain[i].size());
}
void update(ll v, ll x) {
	chainseg[cnum[v].first].update(cnum[v].second + 1, x);
}
//1을 루트로 하는 LCA
ll lca(ll u, ll v) {
	if (depth[u] != depth[v]) {
		if (depth[u] < depth[v]) swap(u, v);
		ll i;
		for (i = MAXS - 1; i >= 0; i--) if (depth[sp[u][i]] >= depth[v]) u = sp[u][i];
	}
	if (u == v) return u;
	ll i;
	for (i = MAXS - 1; i >= 0; i--) if (sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
	return sp[v][0];
}
//HLD query
ll mxval(ll u, ll v) {
	ll ans = 0;
	ll l = lca(u, v);
	while (cnum[u].first != cnum[l].first) ans = max(ans, chainseg[cnum[u].first].query(1, cnum[u].second + 1)), u = sp[chain[cnum[u].first][0]][0];
	while (cnum[v].first != cnum[l].first) ans = max(ans, chainseg[cnum[v].first].query(1, cnum[v].second + 1)), v = sp[chain[cnum[v].first][0]][0];
	ans = max(ans, chainseg[cnum[l].first].query(cnum[l].second + 1, cnum[u].second + 1));
	ans = max(ans, chainseg[cnum[l].first].query(cnum[l].second + 1, cnum[v].second + 1));
	return ans;
}
//두 정점 사이 거리
ll dis(ll u, ll v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
ll dis(ll u, ll v, ll l) { return depth[u] + depth[v] - 2 * depth[l]; }
//r이 루트, v의 x번째 부모
ll prtx(ll r, ll v, ll x) {
	if (x == 0) return v;
	ll l = lca(r, v);
	ll rv = dis(r, v, l);
	if (rv < x) return 0;
	if (dis(l, v, l) < x) {
		ll d = rv - x;
		ll i;
		for (i = MAXS - 1; i >= 0; i--) if (d - (1 << i) >= 0) d -= (1 << i), r = sp[r][i];
		return r;
	}
	else {
		ll i;
		for (i = MAXS - 1; i >= 0; i--) if (x - (1 << i) >= 0) x -= (1 << i), v = sp[v][i];
		return v;
	}
}
ll getfar(ll v, ll ban) {
	if (dir[v] != ban) return dir[v];
	return ddir[v];
}
ll getfar(ll v, ll ban1, ll ban2) {
	if (ban1 > ban2) swap(ban1, ban2);
	if (ban2 == -1) return dir[v];
	if (ban1 == -1) return getfar(v, ban2);
	if (dir[v] != ban1 && dir[v] != ban2) return dir[v];
	if (ddir[v] != ban1 && ddir[v] != ban2) return ddir[v];
	return dddir[v];
}
ll getind(vector<ll>& v, ll c) {
	return lower_bound(v.begin(), v.end(), c) - v.begin();
}
//r1 : previous root, adj[r1][ind]=r2
void prop(ll r1, ll r2, ll ind) {
	arr[r2] = 0, update(r2, arr[r2]);
	ll f1 = getfar(r1, ind);
	ll f2 = getfar(r1, ind, f1);
	if (f1 == -1) arr[r1] = 0, update(r1, 0);
	else if (f2 == -1) arr[r1] = savdis[r1][f1], update(r1, arr[r1]);
	else arr[r1] = savdis[r1][f1] + savdis[r1][f2], update(r1, arr[r1]);
}
void dfs(ll x, ll p = 0) {
	ll i;
	for (i = 0; i < adj[x].size(); i++) {
		ll farv = sav[x][i];
		if (mxval(farv, adj[x][i]) >= savdis[x][i]) continue;
		ll xx = (savdis[x][i] - 1) / 2;
		ll root = prtx(x, farv, xx);
		if (lca(root, x) == root) revtree[prtx(x, root, 1)].insert(C[x]);
		else subtree[root].insert(C[x]);
	}
	ll v;
	for (i = 0; i < adj[x].size(); i++) {
		v = adj[x][i];
		if (v == p) continue;
		ll p1, p2;
		p1 = arr[x];
		p2 = arr[v];
		prop(x, v, i);
		dfs(v, x);
		arr[x] = p1;
		arr[v] = p2;
		update(x, arr[x]);
		update(v, arr[v]);
	}
}
ll mp[MAX];
ll anscnt;
void getans(ll x, ll p = 0) {
	for (auto c : subtree[x]) {
		if (!mp[c]) anscnt++;
		mp[c]++;
	}
	for (auto c : revtree[x]) {
		mp[c]--;
		if (!mp[c]) anscnt--;
	}
	ans[x] = anscnt;
	for (auto v : adj[x]) {
		if (v == p) continue;
		getans(v, x);
	}
	for (auto c : revtree[x]) {
		if (!mp[c]) anscnt++;
		mp[c]++;
	}
	for (auto c : subtree[x]) {
		mp[c]--;
		if (!mp[c]) anscnt--;
	}
}
void calcp(ll x, ll p = 0, ll ind = 0) {
	if (x != 1) {
		ll tmp = prtind[x];
		if (p == 1) sav[x][tmp] = rev[p][ind];
		else {
			ll v1 = rev[p][ind];
			ll v2 = sav[p][prtind[p]];
			if (v1 > v2) swap(v1, v2);
			if (dis(x, v1) >= dis(x, v2)) sav[x][tmp] = v1;
			else sav[x][tmp] = v2;
		}
		savdis[x][tmp] = dis(x, sav[x][tmp]);
	}
	ll i;
	for (i = 0; i < adj[x].size(); i++) if (adj[x][i] != p) calcp(adj[x][i], x, i);
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	depth[0] = -1;
	ll N, M;
	cin >> N >> M;
	ll i, j;
	ll a, b;
	for (i = 1; i < N; i++) cin >> a >> b, adj[a].push_back(b), adj[b].push_back(a);
	for (i = 1; i <= N; i++) cin >> C[i];
	for (i = 1; i <= N; i++) sort(adj[i].begin(), adj[i].end());
	init(1);
	calc(1);
	calcp(1);
	cnt = 0;
	chain.push_back(vector<ll>());
	make_chain(1);
	make_tree();
	//400ms
	for (i = 1; i <= N; i++) {
		ll m1, m2, m3;
		m1 = m2 = m3 = 0;
		dir[i] = ddir[i] = dddir[i] = -1;
		for (j = adj[i].size() - 1; j >= 0; j--) {
			if (m1 <= savdis[i][j]) dddir[i] = ddir[i], ddir[i] = dir[i], dir[i] = j, m3 = m2, m2 = m1, m1 = savdis[i][j];
			else if (m2 <= savdis[i][j]) dddir[i] = ddir[i], ddir[i] = j, m3 = m2, m2 = savdis[i][j];
			else if (m3 <= savdis[i][j]) dddir[i] = j, m3 = savdis[i][j];
		}
		if (i != 1) {
			ll p = prtind[i];
			ll r = getfar(i, p);
			ll rr = getfar(i, p, r);
			ll xx = 0;
			if (r != -1) xx += savdis[i][r];
			if (rr != -1) xx += savdis[i][rr];
			arr[i] = xx;
			update(i, xx);
		}
	}
	subtree.resize(N + 1);
	revtree.resize(N + 1);
	dfs(1);
	for (i = 1; i <= N; i++) for (auto c : revtree[i]) if (!(mp[c]++)) anscnt++;
	getans(1);
	for (i = 1; i <= N; i++) cout << ans[i] << ln;
}

Compilation message

joi2019_ho_t5.cpp: In function 'll init(ll, ll, ll)':
joi2019_ho_t5.cpp:74:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |  for (i = 0; i < adj[x].size(); i++) {
      |              ~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void calc(ll, ll)':
joi2019_ho_t5.cpp:84:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |  for (i = 0; i < adj[x].size(); i++) {
      |              ~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:102:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |   for (i = 0; i < adj[x].size(); i++) {
      |               ~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:109:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |   for (i = 0; i < adj[x].size(); i++) {
      |               ~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:115:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |  for (i = 0; i < adj[x].size(); i++) {
      |              ~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void make_tree()':
joi2019_ho_t5.cpp:141:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |  for (i = 0; i < chain.size(); i++) chainseg[i].initf(chain[i].size());
      |              ~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void dfs(ll, ll)':
joi2019_ho_t5.cpp:215:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  215 |  for (i = 0; i < adj[x].size(); i++) {
      |              ~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:224:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  224 |  for (i = 0; i < adj[x].size(); i++) {
      |              ~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void calcp(ll, ll, ll)':
joi2019_ho_t5.cpp:277:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  277 |  for (i = 0; i < adj[x].size(); i++) if (adj[x][i] != p) calcp(adj[x][i], x, i);
      |              ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19148 KB Output is correct
2 Correct 17 ms 20172 KB Output is correct
3 Correct 15 ms 19708 KB Output is correct
4 Correct 19 ms 20044 KB Output is correct
5 Correct 18 ms 20252 KB Output is correct
6 Correct 20 ms 20296 KB Output is correct
7 Correct 21 ms 20136 KB Output is correct
8 Correct 18 ms 20172 KB Output is correct
9 Correct 18 ms 20220 KB Output is correct
10 Correct 18 ms 20240 KB Output is correct
11 Correct 21 ms 20180 KB Output is correct
12 Correct 19 ms 20500 KB Output is correct
13 Correct 23 ms 20248 KB Output is correct
14 Correct 21 ms 20300 KB Output is correct
15 Correct 19 ms 20300 KB Output is correct
16 Correct 16 ms 20480 KB Output is correct
17 Correct 23 ms 20384 KB Output is correct
18 Correct 19 ms 20252 KB Output is correct
19 Correct 18 ms 20172 KB Output is correct
20 Correct 22 ms 20392 KB Output is correct
21 Correct 21 ms 20120 KB Output is correct
22 Correct 20 ms 20252 KB Output is correct
23 Correct 19 ms 20228 KB Output is correct
24 Correct 20 ms 20172 KB Output is correct
25 Correct 21 ms 20260 KB Output is correct
26 Correct 20 ms 20384 KB Output is correct
27 Correct 21 ms 20300 KB Output is correct
28 Correct 20 ms 20376 KB Output is correct
29 Correct 18 ms 20300 KB Output is correct
30 Correct 16 ms 20472 KB Output is correct
31 Correct 21 ms 20528 KB Output is correct
32 Correct 20 ms 20252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 841 ms 75144 KB Output is correct
2 Correct 1358 ms 79416 KB Output is correct
3 Correct 194 ms 32660 KB Output is correct
4 Correct 1535 ms 116476 KB Output is correct
5 Execution timed out 2082 ms 112992 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1184 ms 98660 KB Output is correct
2 Execution timed out 2090 ms 119432 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19148 KB Output is correct
2 Correct 17 ms 20172 KB Output is correct
3 Correct 15 ms 19708 KB Output is correct
4 Correct 19 ms 20044 KB Output is correct
5 Correct 18 ms 20252 KB Output is correct
6 Correct 20 ms 20296 KB Output is correct
7 Correct 21 ms 20136 KB Output is correct
8 Correct 18 ms 20172 KB Output is correct
9 Correct 18 ms 20220 KB Output is correct
10 Correct 18 ms 20240 KB Output is correct
11 Correct 21 ms 20180 KB Output is correct
12 Correct 19 ms 20500 KB Output is correct
13 Correct 23 ms 20248 KB Output is correct
14 Correct 21 ms 20300 KB Output is correct
15 Correct 19 ms 20300 KB Output is correct
16 Correct 16 ms 20480 KB Output is correct
17 Correct 23 ms 20384 KB Output is correct
18 Correct 19 ms 20252 KB Output is correct
19 Correct 18 ms 20172 KB Output is correct
20 Correct 22 ms 20392 KB Output is correct
21 Correct 21 ms 20120 KB Output is correct
22 Correct 20 ms 20252 KB Output is correct
23 Correct 19 ms 20228 KB Output is correct
24 Correct 20 ms 20172 KB Output is correct
25 Correct 21 ms 20260 KB Output is correct
26 Correct 20 ms 20384 KB Output is correct
27 Correct 21 ms 20300 KB Output is correct
28 Correct 20 ms 20376 KB Output is correct
29 Correct 18 ms 20300 KB Output is correct
30 Correct 16 ms 20472 KB Output is correct
31 Correct 21 ms 20528 KB Output is correct
32 Correct 20 ms 20252 KB Output is correct
33 Correct 841 ms 75144 KB Output is correct
34 Correct 1358 ms 79416 KB Output is correct
35 Correct 194 ms 32660 KB Output is correct
36 Correct 1535 ms 116476 KB Output is correct
37 Execution timed out 2082 ms 112992 KB Time limit exceeded
38 Halted 0 ms 0 KB -