QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#718874#9609. 幽默还是夢zlt0 0ms0kbC++144.1kb2024-11-06 21:39:442024-11-06 21:39:46

Judging History

你现在查看的是最新测评结果

  • [2024-11-06 21:39:46]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-11-06 21:39:44]
  • 提交

answer

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define uint unsigned
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;

const int maxn = 500100;
const int logn = 21;

int test, n, m, ans;
vector<int> G[maxn];
pii a[maxn];

int st[logn][maxn], dfn[maxn], tim, b[maxn], fa[maxn], rnk[maxn];

inline int get(int i, int j) {
	return dfn[i] < dfn[j] ? i : j;
}

inline int qlca(int x, int y) {
	if (x == y) {
		return x;
	}
	x = dfn[x];
	y = dfn[y];
	if (x > y) {
		swap(x, y);
	}
	++x;
	int k = __lg(y - x + 1);
	return get(st[k][x], st[k][y - (1 << k) + 1]);
}

void dfs(int u, int fa) {
	dfn[u] = ++tim;
	st[0][tim] = fa;
	::fa[u] = fa;
	for (int v : G[u]) {
		if (v == fa) {
			continue;
		}
		dfs(v, u);
	}
}

void dfs2(int u, int fa) {
	for (int v : G[u]) {
		if (v == fa) {
			continue;
		}
		dfs2(v, u);
		b[u] += b[v];
	}
}

int c[maxn];
vector<int> vc[maxn];

int sz[maxn], son[maxn], dep[maxn], top[maxn];

int dfs3(int u, int f, int d) {
	sz[u] = 1;
	dep[u] = d;
	son[u] = 0;
	int mx = -1;
	for (int v : G[u]) {
		if (v == f) {
			continue;
		}
		sz[u] += dfs3(v, u, d + 1);
		if (sz[v] > mx) {
			son[u] = v;
			mx = sz[v];
		}
	}
	return sz[u];
}

void dfs4(int u, int tp) {
	top[u] = tp;
	dfn[u] = ++tim;
	rnk[tim] = u;
	if (!son[u]) {
		return;
	}
	dfs4(son[u], tp);
	for (int v : G[u]) {
		if (!dfn[v]) {
			dfs4(v, v);
		}
	}
}

namespace SGT {
	pii a[maxn << 2];
	int tag[maxn << 2];
	
	inline pii get(pii a, pii b) {
		return (a.fst > b.fst || (a.fst == b.fst && a.scd < b.scd)) ? a : b;
	}
	
	inline void pushup(int x) {
		a[x] = get(a[x << 1], a[x << 1 | 1]);
	}
	
	inline void pushtag(int x, int y) {
		a[x].fst += y;
		tag[x] += y;
	}
	
	inline void pushdown(int x) {
		if (!tag[x]) {
			return;
		}
		pushtag(x << 1, tag[x]);
		pushtag(x << 1 | 1, tag[x]);
		tag[x] = 0;
	}
	
	void build(int rt, int l, int r) {
		tag[rt] = 0;
		if (l == r) {
			a[rt] = mkp(b[rnk[l]], l);
			return;
		}
		int mid = (l + r) >> 1;
		build(rt << 1, l, mid);
		build(rt << 1 | 1, mid + 1, r);
		pushup(rt);
	}
	
	void update(int rt, int l, int r, int ql, int qr, int x) {
		if (ql <= l && r <= qr) {
			pushtag(rt, x);
			return;
		}
		pushdown(rt);
		int mid = (l + r) >> 1;
		if (ql <= mid) {
			update(rt << 1, l, mid, ql, qr, x);
		}
		if (qr > mid) {
			update(rt << 1 | 1, mid + 1, r, ql, qr, x);
		}
		pushup(rt);
	}
}

inline void update(int x, int y, int z) {
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) {
			swap(x, y);
		}
		SGT::update(1, 1, n, dfn[top[x]], dfn[x], z);
		x = fa[top[x]];
	}
	if (dep[x] > dep[y]) {
		swap(x, y);
	}
	SGT::update(1, 1, n, dfn[x], dfn[y], z);
}

void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		vector<int>().swap(G[i]);
		vector<int>().swap(vc[i]);
		b[i] = 0;
	}
	for (int i = 1, u, v; i < n; ++i) {
		scanf("%d%d", &u, &v);
		G[u].pb(v);
		G[v].pb(u);
	}
	tim = 0;
	dfs(1, 0);
	for (int j = 1; (1 << j) <= n; ++j) {
		for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
			st[j][i] = get(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
		}
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d", &a[i].fst, &a[i].scd);
		++b[a[i].fst];
		++b[a[i].scd];
		int z = qlca(a[i].fst, a[i].scd);
		--b[z];
		if (z > 1) {
			--b[fa[z]];
		}
		vc[z].pb(i);
		c[i] = 0;
	}
	dfs2(1, -1);
	ans = *max_element(b + 1, b + n + 1);
	printf("%d\n", ans);
	dfs3(1, -1, 1);
	tim = 0;
	for (int i = 1; i <= n; ++i) {
		dfn[i] = 0;
	}
	dfs4(1, 1);
	SGT::build(1, 1, n);
	while (SGT::a[1].fst) {
		int u = rnk[SGT::a[1].scd];
		int i = vc[u].back();
		vc[u].pop_back();
		c[i] = SGT::a[1].fst;
		update(a[i].fst, a[i].scd, -1);
	}
	for (int i = 1; i <= m; ++i) {
		printf("%d%c", c[i], " \n"[i == m]);
	}
}

int main() {
	int T = 1;
	scanf("%d%d", &test, &T);
	while (T--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

3 5
3 6
2 7
3 6
1 1 1 6
1 0 2 3
1 1 4 9
2 0 1 4
2 0 2 4

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Runtime Error

Test #18:

score: 0
Runtime Error

input:

100000 100000
14 26
20 23
12 27
12 26
19 31
19 36
19 31
4 20
6 10
10 20
3 6
5 8
12 23
2 14
11 15
7 12
4 8
3 11
17 32
5 21
20 37
14 23
11 15
1 8
10 18
9 26
15 34
14 20
4 19
1 20
13 18
1 5
9 21
3 6
12 18
18 24
2 12
10 22
7 21
4 7
13 20
11 23
19 37
5 18
15 18
12 26
8 10
7 23
7 26
17 23
4 15
15 22
2 10
...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #3:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

0%