QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#331428#6127. Kawa ExamEnder32kAC ✓466ms39700kbC++173.6kb2024-02-18 08:52:092024-02-18 08:52:10

Judging History

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

  • [2024-02-18 08:52:10]
  • 评测
  • 测评结果:AC
  • 用时:466ms
  • 内存:39700kb
  • [2024-02-18 08:52:09]
  • 提交

answer

// Problem: P9886 [ICPC2018 Qingdao R] Kawa Exam
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P9886
// Memory Limit: 512 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define eb emplace_back
#define pb pop_back
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef tuple<int, int, int> tu;
bool Mbe;

const int N = 1e5 + 100;

pi e[N];
int n, m, mx, cur, res;
int a[N], c[N], ct[N], ans[N], tres[N];
int sz[N], son[N], rid[N], lp[N], rp[N];
int dfc, tot, tp, st[N], bel[N], dfn[N], low[N];

vector<int> b[N], bl;
vector<pi> g[N], t[N];

void tarjan(int u, int fa) {
	bl.eb(u), dfn[u] = low[u] = ++dfc, st[++tp] = u;
	for (auto [v, id] : g[u]) {
		if (id == fa) continue;
		if (dfn[v]) low[u] = min(low[u], dfn[v]);
		else tarjan(v, id), low[u] = min(low[u], low[v]);
	}
	if (low[u] == dfn[u]) {
		tot++;
		while (st[tp] != u) 
			bel[st[tp]] = tot, b[tot].eb(st[tp]), tp--;
		bel[u] = tot, b[tot].eb(u), tp--;
	}
}

void add(int x) { ct[c[x]]--, c[x]++, ct[c[x]]++, cur = max(cur, c[x]); }
void del(int x) { ct[c[x]]--, c[x]--, ct[c[x]]++; while (cur && !ct[cur]) cur--; }

void upd(int x, int op) {
	if (op == 1) add(x);
	else del(x);
}

void dfs1(int u, int fa) {
	rid[lp[u] = ++dfc] = u, sz[u] = b[u].size();
	for (auto [v, id] : t[u]) {
		if (v == fa) continue;
		dfs1(v, u), sz[u] += sz[v];
		if (sz[v] > sz[son[u]]) son[u] = v;
	}
	rp[u] = dfc;
}

void dfs2(int u, int fa, int fl, int op) {
	for (auto [v, id] : t[u]) {
		if (id == fa || v == son[u]) continue;
		dfs2(v, id, 0, op);
	}
	if (son[u]) {
		int sid = 0;
		for (auto [v, id] : t[u])
			if (v == son[u]) sid = id;
		dfs2(son[u], sid, 1, op);
	}
	for (auto [v, id] : t[u]) {
		if (id == fa || v == son[u]) continue;
		for (int i = lp[v]; i <= rp[v]; i++) 
			for (int j : b[rid[i]]) upd(a[j], op);
	}
	for (int i : b[u]) upd(a[i], op);
	ans[fa] += cur;
	if (fl) return;
	for (int i = lp[u]; i <= rp[u]; i++) 
		for (int j : b[rid[i]]) upd(a[j], op ^ 1);
}

void clr() {
	for (int i = 0; i <= mx; i++) c[i] = 0;
	for (int i = 1; i <= n; i++) 
		dfn[i] = low[i] = son[i] = ct[i] = 0, g[i].clear();
	for (int i = 1; i <= tot; i++)
		lp[i] = 0, t[i].clear(), b[i].clear();
	mx = cur = res = dfc = tot = tp = 0;
}

void solve() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) 
		cin >> a[i], mx = max(mx, a[i]);
	for (int i = 1, u, v; i <= m; i++)
		cin >> u >> v, e[i] = mp(u, v), g[u].eb(mp(v, i)), g[v].eb(mp(u, i));
	for (int i = 1; i <= n; i++) {
		if (dfn[i]) continue;
		bl.clear(), tarjan(i, 0);
		for (int u : bl) add(a[u]);
		res += cur;
		for (int u : bl) tres[u] = cur;
		for (int u : bl) del(a[u]);
	}
	for (int i = 1; i <= m; i++) {
		auto [u, v] = e[i];
		ans[i] = res;
		if (bel[u] != bel[v]) {
			t[bel[u]].eb(mp(bel[v], i));
			t[bel[v]].eb(mp(bel[u], i));
			ans[i] -= tres[u];
		} 
	}
	dfc = 0;
	for (int i = 1; i <= tot; i++) {
		if (lp[i]) continue;
		dfs1(i, 0), dfs2(i, 0, 0, 1);
		for (int j = lp[i]; j <= rp[i]; j++)
			for (int k : b[j]) add(a[k]);
		dfs2(i, 0, 1, 0);
	}
	for (int i = 1; i <= m - 1; i++) cout << ans[i] << ' ';
	cout << ans[m] << '\n', clr();
}

bool Med;
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cerr << (&Mbe - &Med) / 1048576.0 << " MB\n";
	#ifdef FILE
		freopen("1.in", "r", stdin);
		freopen("1.out", "w", stdout);
	#endif
	int T = 1;
	cin >> T;
	while (T--) solve();
	cerr << (int)(1e3 * clock() / CLOCKS_PER_SEC) << " ms\n";
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 16676kb

input:

3
7 5
1 2 1 2 1 2 1
1 2
1 3
2 4
5 6
5 7
3 3
1 2 3
1 2
1 3
2 3
2 3
12345 54321
1 2
1 2
1 1

output:

6 5 5 5 4
1 1 1
1 1 1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 466ms
memory: 37212kb

input:

5557
2 7
79960 79960
2 2
1 1
1 1
2 2
1 1
2 1
1 2
9 8
21881 70740 70740 21881 22458 22458 639 21881 70740
3 3
1 6
5 8
7 5
5 7
2 3
5 1
7 6
6 7
13064 20716 6746 13064 6746 69225
5 5
4 1
4 1
1 6
4 5
3 2
3 2
8 4
45146 14400 45146 45146 14400 72969 14400 45146
8 6
1 3
4 6
8 3
18 13
48132 37949 92338 92338...

output:

2 2 2 2 2 2 2
6 6 7 6 6 6 6 6
3 3 3 4 4 3 3
7 7 7 7
9 9 9 8 9 8 9 8 9 9 10 9 9
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
7 8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
9 10 9
16 16 16 16 16 17 16 16
10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11
10 9 9 9 9 9 9 9 9 9 9 9 9 9 10 ...

result:

ok 5557 lines

Test #3:

score: 0
Accepted
time: 289ms
memory: 36356kb

input:

10
100000 99999
3983 3983 20157 97983 20157 20157 3983 3983 97983 20157 20157 3983 97983 20157 3983 20157 20157 3983 3983 3983 97983 97983 20157 3983 3983 97983 20157 97983 20157 97983 3983 97983 97983 3983 20157 3983 20157 20157 97983 3983 3983 3983 3983 97983 97983 3983 97983 97983 3983 20157 3983...

output:

33392 33393 33393 33393 33393 33392 33392 33393 33393 33393 33392 33393 33393 33392 33393 33393 33392 33392 33392 33393 33393 33393 33392 33392 33393 33393 33393 33393 33393 33392 33393 33393 33392 33393 33392 33393 33393 33393 33392 33392 33392 33392 33393 33393 33392 33393 33393 33392 33393 33392 ...

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 296ms
memory: 39700kb

input:

10
100000 99999
27534 27534 3780 3780 27534 53544 27534 3780 3780 53544 53544 27534 53544 53544 3780 3780 3780 3780 53544 27534 3780 3780 53544 27534 27534 53544 27534 27534 53544 27534 27534 27534 3780 27534 27534 3780 3780 3780 27534 53544 3780 53544 27534 3780 3780 3780 27534 27534 27534 3780 275...

output:

33613 33601 33601 33600 33600 33601 33601 33601 33600 33601 33600 33600 33601 33601 33601 33601 33601 33601 33600 33600 33601 33601 33601 33601 33600 33601 33601 33600 33601 33600 33601 33600 33601 33601 33601 33601 33600 33601 33601 33601 33601 33601 33601 33601 33601 33601 33600 33601 33600 33601 ...

result:

ok 10 lines

Test #5:

score: 0
Accepted
time: 301ms
memory: 30544kb

input:

10
100000 99999
92499 92270 92270 92499 92499 92499 92270 54017 92270 92270 92270 54017 54017 54017 54017 92270 92499 54017 92270 54017 92499 92499 92270 92270 54017 54017 54017 54017 92270 92270 92499 54017 54017 92499 92499 54017 92270 92270 54017 92499 92270 92270 54017 54017 54017 92499 92499 54...

output:

33506 33482 33507 33482 33508 33483 33508 33483 33508 33483 33507 33483 33506 33483 33505 33483 33503 33483 33503 33482 33504 33483 33505 33483 33504 33483 33502 33483 33501 33483 33500 33482 33502 33483 33500 33483 33501 33482 33502 33483 33501 33483 33500 33482 33500 33483 33498 33483 33499 33483 ...

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 316ms
memory: 29052kb

input:

10
100000 99999
76207 76207 88551 88551 98176 76207 98176 88551 88551 98176 88551 76207 76207 98176 98176 76207 76207 88551 76207 88551 76207 88551 88551 76207 88551 76207 98176 88551 76207 98176 88551 88551 76207 88551 98176 88551 76207 76207 98176 88551 76207 98176 76207 88551 88551 88551 88551 76...

output:

33484 33484 33476 33484 33477 33485 33476 33485 33477 33485 33477 33486 33477 33484 33477 33485 33476 33485 33476 33485 33476 33483 33477 33483 33477 33485 33476 33485 33477 33485 33476 33487 33476 33487 33476 33486 33477 33486 33476 33486 33477 33486 33476 33486 33476 33486 33477 33487 33477 33487 ...

result:

ok 10 lines

Test #7:

score: 0
Accepted
time: 308ms
memory: 28228kb

input:

10
100000 99999
70486 49904 70486 49904 87935 49904 49904 87935 87935 49904 49904 87935 49904 87935 87935 70486 49904 87935 87935 49904 70486 87935 49904 70486 87935 87935 49904 49904 49904 87935 70486 70486 70486 49904 70486 87935 87935 87935 70486 87935 70486 49904 87935 49904 49904 87935 70486 87...

output:

33491 33486 33489 33486 33489 33486 33489 33486 33487 33486 33487 33486 33486 33485 33486 33486 33486 33486 33485 33486 33485 33485 33486 33486 33485 33486 33485 33486 33485 33485 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33485 33486 33485 ...

result:

ok 10 lines

Test #8:

score: 0
Accepted
time: 318ms
memory: 28324kb

input:

10
100000 99999
98004 33580 98004 98004 98004 92291 92291 98004 98004 92291 92291 33580 98004 92291 33580 98004 98004 33580 98004 92291 92291 33580 92291 92291 98004 33580 98004 33580 33580 98004 33580 92291 33580 33580 92291 92291 92291 98004 33580 98004 92291 92291 33580 92291 98004 98004 92291 92...

output:

33462 33463 33421 33463 33422 33465 33421 33463 33422 33464 33422 33462 33422 33464 33421 33464 33422 33464 33422 33465 33422 33463 33422 33462 33422 33463 33422 33465 33421 33464 33422 33464 33422 33463 33422 33463 33421 33463 33421 33462 33422 33460 33422 33461 33421 33461 33422 33460 33422 33459 ...

result:

ok 10 lines