QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#792224#6127. Kawa ExamSin_WattML 2ms10548kbC++144.5kb2024-11-29 07:31:432024-11-29 07:31:45

Judging History

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

  • [2024-11-29 07:31:45]
  • 评测
  • 测评结果:ML
  • 用时:2ms
  • 内存:10548kb
  • [2024-11-29 07:31: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 = 0;
	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;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 10548kb

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: -100
Memory Limit Exceeded

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:


result: