QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#792232#6127. Kawa ExamSin_WattAC ✓549ms30216kbC++144.5kb2024-11-29 07:50:432024-11-29 07:50:43

Judging History

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

  • [2024-11-29 07:50:43]
  • 评测
  • 测评结果:AC
  • 用时:549ms
  • 内存:30216kb
  • [2024-11-29 07:50:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long lnt;

const int N = 1e5 + 7;

int n, m;
int a[N];
int va[N], vn;
int *am[N];

struct E {
	int to, ne;
} e[N << 1];
int idx = 1;
int h[N];

inline void add(int x, int y) {
	e[ ++ idx] = (E){y, h[x]}; h[x] = idx;
	e[ ++ idx] = (E){x, h[y]}; h[y] = idx;
}

int dnt;
int dfn[N];
int low[N];
int sta[N], stt;

int frm[N];
int scc;
vector<int> hav[N];

void tarjan(int u, int fe) {
	u[dfn] = u[low] = ++ dnt;
	stt[sta] = u; ++ stt;
	for (int i = h[u]; i; i = e[i].ne) {
		if (i == fe) continue;
		int v = e[i].to;
		if (v[dfn]) {
			u[low] = min(u[low], v[dfn]);
		} else {
			tarjan(v, i ^ 1);
			u[low] = min(u[low], v[low]);
		}
	}
	if (u[low] == u[dfn]) {
		++ scc;
		do {
			int v = sta[ -- stt];
			frm[v] = scc;
			scc[hav].push_back(v);
		} while (sta[stt] != u);
	}
}

struct Tree {
	int val[N << 2];
	
	#define ls (u << 1)
	#define rs (u << 1 | 1)
	#define all u = 1, int l = 1, int r = vn
	#define lq ls, l, mid
	#define rq rs, mid + 1, r
	
	void modify(int P, int V, int all) {
		if (l == r) {
			//cerr << "\t\t\t\t[Tree]  " << P << ' ' << V << '\n';
			u[val] += V;
			return;
		}
		int mid = (l + r) >> 1;
		if (P <= mid) {
			modify(P, V, lq);
		} else {
			modify(P, V, rq);
		}
		u[val] = max(ls[val], rs[val]);
	}
	
	int query() {
		return val[1];
	}
} T[2];

vector<pair<int, int>> G[N];
int siz[N];
int son[N];

void dfs1(int u, int fa) {
	u[siz] = u[hav].size();
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa) continue;
		dfs1(v, u);
		u[siz] += v[siz];
		if (u[son][siz] < v[siz]) {
			u[son] = v;
		}
	}
}

void dfs2(int u, int fa, int t) {
	for (auto i : hav[u]) {
		T[0].modify(i[a], t);
	}
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa) continue;
		dfs2(v, u, t);
	}
}

void dfs3(int u, int fa, int t) {
	for (auto i : hav[u]) {
		T[t    ].modify(i[a], -1);
		T[t ^ 1].modify(i[a], 1);
	}
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa) continue;
		dfs3(v, u, t);
	}
}

int All;
int Ans;
int ans[N];

void dfs4(int u, int fa, int t) {
	//cerr << "dfs4 [in]\t" << u << ' ' << fa << ' ' << t << '\n';
	int fe = 0;
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa) {
			fe = pir.second;
			continue;
		} else if (v == u[son]) continue;
		dfs4(v, u, 1);
	}
	if (u[son]) {
		dfs4(u[son], u, 0);
	}
	for (auto i : hav[u]) {
		T[0].modify(i[a], -1);
		T[1].modify(i[a], 1);
	}
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa || v == u[son]) continue;
		dfs3(v, u, 0);
	}
	if (fe) {
		ans[fe] = T[0].query() + T[1].query() - All;
		//cerr << "     [get]\t" << u << ' ' << fe << ' ' << ans[fe] << '\n';
	}
	if (t) {
		for (auto i : hav[u]) {
			T[1].modify(i[a], -1);
			T[0].modify(i[a], 1);
		}
		for (auto pir : G[u]) {
			int v = pir.first;
			if (v == fa) continue;
			dfs3(v, u, 1);
		}
	}
	//cerr << "dfs4 [out]\t" << u << '\n';
}

inline void INIT() { }

inline void WORK() {
	cin >> n >> m;
	for (int i = 1; i <= n; ++ i) {
		cin >> a[i];
		am[i] = a + i;
	}
	sort(am + 1, am + n + 1, [](const int *a, const int *b) -> bool {
		return *a < *b;
	});
	for (int i = 1; i <= n; ++ i) {
		if (va[vn] != *am[i]) {
			va[ ++ vn] = *am[i];
		}
		*am[i] = vn;
	}
	for (int i = 1; i <= m; ++ i) {
		int x, y;
		cin >> x >> y;
		add(x, y);
	}
	for (int i = 1; i <= n; ++ i) {
		if (!dfn[i]) {
			tarjan(i, 0);
		}
	}
	for (int u = 1; u <= n; ++ u) {
		for (int i = h[u]; i; i = e[i].ne) {
			int v = e[i].to;
			if (u[frm] != v[frm]) {
				u[frm][G].push_back({ v[frm], i >> 1 });
			}
		}
	}
	for (int i = 1; i <= scc; ++ i) {
		if (!siz[i]) {
			dfs1(i, 0);
			dfs2(i, 0, 1);
			Ans += All = T[0].query();
			dfs4(i, 0, 1);
			dfs2(i, 0, -1);
		}
	}
	cout << Ans + ans[1];
	for (int i = 2; i <= m; ++ i) {
		cout << ' ' << Ans + ans[i];
	}
	cout << '\n';
	
	vn = 0;
	idx = 1;
	dnt = 0;
	for (int i = 1; i <= n; ++ i) {
		h[i] = 0;
		dfn[i] = 0;
	}
	for (int i = 1; i <= m; ++ i) {
		ans[i] = 0;
	}
	for (int i = 1; i <= scc; ++ i) {
		G[i].clear();
		hav[i].clear();
		siz[i] = 0;
		son[i] = 0;
	}
	scc = 0;
	Ans = 0;
}

//#define filename ""

int main() {
	#ifdef filename
	freopen(filename ".in", "r", stdin);
	freopen(filename ".out", "w", stdout);
	#endif
	cin.tie(0); cout.tie(0);
	ios::sync_with_stdio(0);
	
	int Turn = 1;
	cin >> Turn;
	INIT();
	while (Turn -- ) {
		WORK();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 549ms
memory: 30216kb

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: 294ms
memory: 27956kb

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: 289ms
memory: 24584kb

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: 353ms
memory: 23900kb

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: 366ms
memory: 25348kb

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: 353ms
memory: 22860kb

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: 350ms
memory: 24464kb

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