Submission #937609

# Submission time Handle Problem Language Result Execution time Memory
937609 2024-03-04T09:38:08 Z guechotjrhh Unique Cities (JOI19_ho_t5) C++14
68 / 100
451 ms 51280 KB
#include<iostream>
#include<vector>
using namespace std;



#include<iostream>
#include<vector>
#include<set>
#include<stack>
using namespace std;
#define pi pair<int,int>
#define x first
#define y second
#define maxn (1<<17)

vector<int> v(2 * maxn, 1e9), l(2 * maxn), r(2 * maxn);
void bld() {
	for (int i = maxn; i < 2 * maxn; i++) {
		l[i] = r[i] = i - maxn;
	}
	for (int i = maxn - 1; i > 0; i--) {
		l[i] = l[2 * i];
		r[i] = r[2 * i + 1];
	}
}
void up(int i, int u) {
	i += maxn;
	v[i] = u;
	while (i >>= 1) v[i] = min(v[i << 1], v[(i << 1) + 1]);
}
int qu(int i, int s, int e) {
	if (l[i] > e || r[i] < s) return 1e9;
	if (l[i] >= s && r[i] <= e) return v[i];
	return min(qu(i << 1, s, e), qu((i << 1) + 1, s, e));
}

pi dfs0(int i, int p, vector<vector<int>>& g) {
	pi pr = { 0,i };
	for (int j : g[i]) {
		if (j == p) continue;
		pi r = dfs0(j, i, g);
		r.x++;
		if (r > pr) pr = r;
	}
	return pr;
}
bool dfs1(int i, int p, vector<vector<int>>& g, int d, vector<int>&dep, vector<int>&row, int x) {
	bool flag = 0;
	if (i == x) flag = 1;
	dep[i] = 0;
	for (int j : g[i]) {
		if (j == p) continue;
		if (dfs1(j, i, g, d + 1, dep, row, x)) flag = 1;
		else dep[i] = max(dep[i], dep[j] + 1);
	}
	if (flag) row[d] = i;
	return flag;
}
void dfs2(int i, int p, vector<vector<int>>& g, vector<int>&par, int f) {
	par[i] = f;
	for (int j : g[i]) {
		if (j == p) continue;
		dfs2(j, i, g, par, f);
	}
}
void dfs3(int i, int p, vector<vector<int>>& g, vector<int>&dep, int lev, vector<int> &vert, vector<int>&add) {
	int mx1 = -1, mx2 =-1;
	for (int j : g[i]) {
		if (j == p) continue;
		if (dep[j]+1 > mx1) {
			mx2 = mx1;
			mx1 = dep[j]+1;
		}
		else if (dep[j]+1 > mx2) mx2 = dep[j]+1;
	}
	vert[lev] = i;
	

	int l = lev - 1;
	if (mx2 > 0) {
		if (lev - mx2 >= 0) l = qu(1, lev - mx2, lev - 1);
		else l = -1;
	}
	if (l > -1) add[i] = add[vert[l]] + 1;
	else add[i] = 0;
	up(lev, l);
	for (int j : g[i]) {
		if (j == p) continue;
		if (dep[j] + 1 == mx1) dfs3(j, i, g, dep, lev + 1, vert, add);
	}

	l = lev - 1;
	if (dep[i] > 0) {
		if (lev - dep[i] >= 0) l = qu(1, lev - dep[i], lev - 1);
		else l = -1;
	}
	if (l > -1) add[i] = add[vert[l]] + 1;
	else add[i] = 0;
	up(lev, l);

	for (int j : g[i]) {
		if (j == p) continue;
		if (dep[j] + 1 == mx1) continue;
		dfs3(j, i, g, dep, lev + 1, vert, add);
	}
}
void dfs(int i, int p, int d, vector<vector<int>>& vec, vector<vector<int>>& g) {
	vec[d].push_back(i);
	for (int j : g[i]) {
		if (j != p) dfs(j, i, d + 1, vec, g);
	}
}
vector<int> find(int N, int M, vector<int> A, vector<int> B, vector<int> W) {
	if (N <= 3000) {
		vector<int> res(N, -1);
		vector<vector<int>> g(N);
		for (int i = 0; i < N - 1; i++) {
			g[A[i] - 1].push_back(B[i] - 1);
			g[B[i] - 1].push_back(A[i] - 1);
		}
		for (int i = 0; i < N; i++) {
			vector<vector<int>> vec(N);
			dfs(i, -1, 0, vec, g);
			set<int> st;
			for (int j = 1; j < N; j++) if (vec[j].size() == 1) st.insert(W[vec[j][0]]);
			res[i] = st.size();
		}

		return res;
	}
	bld();
	vector<int> res(N, -1);
	vector<vector<int>> g(N);
	for (int i = 0; i < N - 1; i++) {
		g[A[i] - 1].push_back(B[i] - 1);
		g[B[i] - 1].push_back(A[i] - 1);
	}
	int rt = dfs0(0, -1, g).y;
	pi pr = dfs0(rt, -1, g);
	int ort = pr.y, len = pr.x + 1;
	vector<int> dep(N, 0), row(len, -1);
	dfs1(rt, -1, g, 0, dep, row, ort);
	//for (int i : dep) cout << i << ' '; cout << endl;
	//for (int i : row) cout << i << ' '; cout << endl;
	vector<int> par(N, -1);
	for (int i = 1; i < len - 1; i++) {
		for (int& j : g[row[i]]) if (j == row[i - 1] || j == row[i + 1]) j = -1;
		dfs2(row[i], -1, g, par, i);
	}
	//for (int i : par) cout << i << ' '; cout << endl;
	
	vector<int> le(len, -1), ri(len, len);
	stack<int> sk;
	//monotonic stack
	for (int i = 0; i < len; i++) {
		int u = row[i];

		int to = i - dep[u];
		while (sk.size() && to <= sk.top()) {
			if (le[sk.top()]+1 < to) to = le[sk.top()]+1;
			sk.pop();
		}
		le[i] = (to >= 0) ? to - 1 : -1;

		sk.push(i);
	}
	for (int i = len - 1; i >= 0; i--) {
		int u = row[i];

		int to = i + dep[u];
		while (sk.size() && to >= sk.top()) {
			if (ri[sk.top()]-1 > to) to = ri[sk.top()]-1;
			sk.pop();
		}
		ri[i] = to < len ? to + 1 : len;

		sk.push(i);
	}
	//for (int i = 0; i < len; i++) cout << i << ' ' << le[i] << ' ' << ri[i] << endl;

	vector<int> cntl(N, 0), cntr(N, 0);
	for (int i = 0; i < len; i++) {
		if (le[i] > -1) cntl[i] = cntl[le[i]] + 1;
	}
	for (int i = len-1; i >= 0; i--) {
		if (ri[i] <  len) cntr[i] = cntr[ri[i]] + 1;
	}
	//cout<<"count:\n";
	//for (int i = 0; i < len; i++) cout << i << ' ' << cntl[i] << ' ' << cntr[i] << endl;

	int lm = len / 2 - 1, rm = (len + 1) / 2;
	
	vector<int> rcl(N, 0), rcr(N, 0);
	int rto = -1, cto = -1;
	for (int i = 0; i <= lm; i++) {
		cto = max(cto, 2*i + 1);
		while (rto < cto - 1) {
			rto++;
			if (rto + dep[row[rto]] + 1 > cto) cto = rto + dep[row[rto]] + 1;
			if (cto > len) cto = len;
		}
		if (cto < len) res[row[i]] = cntr[cto] + 1;
		else res[row[i]] = 0;
	}
	int lto = len;
	cto = len;
	for (int i = len-1; i >= rm; i--) {
		cto = min(cto, 2*i - len);
		while (lto > cto + 1) {
			lto--;
			if (lto - dep[row[lto]] - 1 < cto) cto = lto - dep[row[lto]] - 1;
			if (cto < 0) cto = -1;
		}
		if (cto >= 0) res[row[i]] = cntl[cto] + 1;
		else res[row[i]] = 0;
	}
	if (len & 1) res[row[len / 2]] = 0;

	vector<int> vert(N), add(N, 0);
	for (int i = 1; i < len - 1; i++) {
		dfs3(row[i], -1, g, dep, 0, vert, add);
	}
	for (int i = 0; i < N; i++) {
		if (res[i] == -1) res[i] = res[row[par[i]]] + add[i];
	}

	/*vector<int> am(len), ap(len);
	vector<vector<int>> vp(18, vector<int>(len, 1e9)), vm(18, vector<int>(len, 1e9));
	for (int i = 0; i < len; i++) {
		am[i] = i - dep[i];
		ap[i] = i + dep[i];
		vm[0][i] = am[i];
		vp[0][i] = ap[i];
	}
	for (int j = 1; j < 18; j++) {
		for (int i = 0; i < len; i++) {
			vp[j][i] = vp[j - 1][i];
			vm[j][i] = vm[j - 1][i];
		}
		for (int i = 0; i + (1 << (j - 1)) < len; i++) {
			vp[j][i] = min(vp[j - 1][i], vp[j - 1][i + (1 << (j - 1))]);
			vm[j][i] = min(vm[j - 1][i], vm[j - 1][i + (1 << (j - 1))]);
		}
	}
	vector<int> lg(len+1, 0);
	lg[1] = 0;
	for (int i = 2; i <= len; i++) lg[i] = lg[i / 2] + 1;*/
	
	if (W[0] == W[1]) for (int &i : res) if (i > 1) i = 1;

	return res;
}

/*

15 15
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
5 10
10 11
10 14
11 12 
11 13
13 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

*/

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int N, M;
	cin >> N >> M;
	vector<int> A(N - 1), B(N - 1);
	for (int i = 0; i < N - 1; i++) {
		cin >> A[i] >> B[i];
	}
	vector<int> W(N);
	for (int i = 0; i < N; i++) {
		cin >> W[i];
	}

	vector<int> ans = find(N, M, A, B, W);

	for (int i = 0; i < N; i++) {
		cout << ans[i] << endl;
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Correct 101 ms 3804 KB Output is correct
3 Correct 85 ms 3796 KB Output is correct
4 Correct 174 ms 3864 KB Output is correct
5 Correct 123 ms 3676 KB Output is correct
6 Correct 217 ms 3980 KB Output is correct
7 Correct 159 ms 3676 KB Output is correct
8 Correct 70 ms 3828 KB Output is correct
9 Correct 154 ms 3804 KB Output is correct
10 Correct 140 ms 3676 KB Output is correct
11 Correct 135 ms 3676 KB Output is correct
12 Correct 38 ms 3676 KB Output is correct
13 Correct 212 ms 3932 KB Output is correct
14 Correct 164 ms 3880 KB Output is correct
15 Correct 168 ms 3848 KB Output is correct
16 Correct 34 ms 3676 KB Output is correct
17 Correct 145 ms 3932 KB Output is correct
18 Correct 134 ms 3676 KB Output is correct
19 Correct 120 ms 3676 KB Output is correct
20 Correct 369 ms 4044 KB Output is correct
21 Correct 195 ms 3672 KB Output is correct
22 Correct 71 ms 3672 KB Output is correct
23 Correct 175 ms 3672 KB Output is correct
24 Correct 127 ms 3672 KB Output is correct
25 Correct 140 ms 3800 KB Output is correct
26 Correct 37 ms 3676 KB Output is correct
27 Correct 306 ms 3932 KB Output is correct
28 Correct 217 ms 3944 KB Output is correct
29 Correct 162 ms 3672 KB Output is correct
30 Correct 34 ms 3824 KB Output is correct
31 Correct 205 ms 3936 KB Output is correct
32 Correct 162 ms 3920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 17608 KB Output is correct
2 Correct 204 ms 31060 KB Output is correct
3 Correct 55 ms 8284 KB Output is correct
4 Correct 391 ms 26188 KB Output is correct
5 Correct 451 ms 51280 KB Output is correct
6 Correct 410 ms 38480 KB Output is correct
7 Correct 357 ms 26216 KB Output is correct
8 Correct 414 ms 28752 KB Output is correct
9 Correct 366 ms 27984 KB Output is correct
10 Correct 368 ms 27988 KB Output is correct
11 Correct 327 ms 26824 KB Output is correct
12 Correct 401 ms 41732 KB Output is correct
13 Correct 369 ms 38312 KB Output is correct
14 Correct 421 ms 37788 KB Output is correct
15 Correct 302 ms 27064 KB Output is correct
16 Correct 397 ms 43288 KB Output is correct
17 Correct 388 ms 38764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 294 ms 21432 KB Output is correct
2 Correct 391 ms 49248 KB Output is correct
3 Correct 64 ms 9308 KB Output is correct
4 Correct 375 ms 26432 KB Output is correct
5 Correct 417 ms 51280 KB Output is correct
6 Correct 400 ms 38400 KB Output is correct
7 Correct 444 ms 26452 KB Output is correct
8 Correct 376 ms 30992 KB Output is correct
9 Correct 410 ms 29404 KB Output is correct
10 Correct 419 ms 27984 KB Output is correct
11 Correct 349 ms 26620 KB Output is correct
12 Correct 412 ms 45904 KB Output is correct
13 Correct 377 ms 36556 KB Output is correct
14 Correct 427 ms 37072 KB Output is correct
15 Correct 317 ms 26820 KB Output is correct
16 Correct 403 ms 43524 KB Output is correct
17 Correct 395 ms 38608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Correct 101 ms 3804 KB Output is correct
3 Correct 85 ms 3796 KB Output is correct
4 Correct 174 ms 3864 KB Output is correct
5 Correct 123 ms 3676 KB Output is correct
6 Correct 217 ms 3980 KB Output is correct
7 Correct 159 ms 3676 KB Output is correct
8 Correct 70 ms 3828 KB Output is correct
9 Correct 154 ms 3804 KB Output is correct
10 Correct 140 ms 3676 KB Output is correct
11 Correct 135 ms 3676 KB Output is correct
12 Correct 38 ms 3676 KB Output is correct
13 Correct 212 ms 3932 KB Output is correct
14 Correct 164 ms 3880 KB Output is correct
15 Correct 168 ms 3848 KB Output is correct
16 Correct 34 ms 3676 KB Output is correct
17 Correct 145 ms 3932 KB Output is correct
18 Correct 134 ms 3676 KB Output is correct
19 Correct 120 ms 3676 KB Output is correct
20 Correct 369 ms 4044 KB Output is correct
21 Correct 195 ms 3672 KB Output is correct
22 Correct 71 ms 3672 KB Output is correct
23 Correct 175 ms 3672 KB Output is correct
24 Correct 127 ms 3672 KB Output is correct
25 Correct 140 ms 3800 KB Output is correct
26 Correct 37 ms 3676 KB Output is correct
27 Correct 306 ms 3932 KB Output is correct
28 Correct 217 ms 3944 KB Output is correct
29 Correct 162 ms 3672 KB Output is correct
30 Correct 34 ms 3824 KB Output is correct
31 Correct 205 ms 3936 KB Output is correct
32 Correct 162 ms 3920 KB Output is correct
33 Correct 224 ms 17608 KB Output is correct
34 Correct 204 ms 31060 KB Output is correct
35 Correct 55 ms 8284 KB Output is correct
36 Correct 391 ms 26188 KB Output is correct
37 Correct 451 ms 51280 KB Output is correct
38 Correct 410 ms 38480 KB Output is correct
39 Correct 357 ms 26216 KB Output is correct
40 Correct 414 ms 28752 KB Output is correct
41 Correct 366 ms 27984 KB Output is correct
42 Correct 368 ms 27988 KB Output is correct
43 Correct 327 ms 26824 KB Output is correct
44 Correct 401 ms 41732 KB Output is correct
45 Correct 369 ms 38312 KB Output is correct
46 Correct 421 ms 37788 KB Output is correct
47 Correct 302 ms 27064 KB Output is correct
48 Correct 397 ms 43288 KB Output is correct
49 Correct 388 ms 38764 KB Output is correct
50 Correct 294 ms 21432 KB Output is correct
51 Correct 391 ms 49248 KB Output is correct
52 Correct 64 ms 9308 KB Output is correct
53 Correct 375 ms 26432 KB Output is correct
54 Correct 417 ms 51280 KB Output is correct
55 Correct 400 ms 38400 KB Output is correct
56 Correct 444 ms 26452 KB Output is correct
57 Correct 376 ms 30992 KB Output is correct
58 Correct 410 ms 29404 KB Output is correct
59 Correct 419 ms 27984 KB Output is correct
60 Correct 349 ms 26620 KB Output is correct
61 Correct 412 ms 45904 KB Output is correct
62 Correct 377 ms 36556 KB Output is correct
63 Correct 427 ms 37072 KB Output is correct
64 Correct 317 ms 26820 KB Output is correct
65 Correct 403 ms 43524 KB Output is correct
66 Correct 395 ms 38608 KB Output is correct
67 Incorrect 51 ms 7004 KB Output isn't correct
68 Halted 0 ms 0 KB -