QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#502377#2645. Collapsemakrav0 358ms7480kbC++204.1kb2024-08-03 04:54:252024-08-03 04:54:26

Judging History

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

  • [2024-08-03 04:54:26]
  • 评测
  • 测评结果:0
  • 用时:358ms
  • 内存:7480kb
  • [2024-08-03 04:54:25]
  • 提交

answer

#include "collapse.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define sc second

const int K = 5001;

struct dsu {
	int n, cmp;
	vector<int> par, siz;
	dsu() = default;
	dsu(int n_) {
		cmp = 0;
		n = n_;
		par.assign(n, 0);
		iota(all(par), 0);
		siz.assign(n, 1);
	}

	int get(int v) {
		return (par[v] == v ? v : par[v] = get(par[v]));
	}

	void merge(int u, int v) {
		u = get(u);
		v = get(v);
		if (u == v) return;
		cmp--;
		if (siz[v] > siz[u]) swap(u, v);
		par[v] = u;
		siz[u] += siz[v];
	}
};		

std::vector<int> simulateCollapse(
	int N,
	std::vector<int> T,
	std::vector<int> X,
	std::vector<int> Y,
	std::vector<int> W,
	std::vector<int> P
) {
	int n = N;
	int c = sz(X);
	vector<vector<pair<int, int>>> qrs(c);
	for (int i = 0; i < sz(W); i++) {
		qrs[W[i]].pb({P[i], i});
	}
	vector<int> ans(sz(W));
	vector<vector<int>> gr(n);
	vector<int> used1(n), used_dfs(n);
	int step = 0;
	for (int i = 0; i < c; i += K) {
		unordered_set<ll> ch_edg;
		unordered_set<ll> LOL;
		for (int j = i; j < min(c, i + K); j++) {
			if (T[j] == 0 && ch_edg.find(X[j] * 1ll * N + Y[j]) == ch_edg.end()) {
				LOL.insert(X[j] * 1ll * n + Y[j]);
			}
			ch_edg.insert(X[j] * 1ll * N + Y[j]);
		}
		vector<vector<int>> by_rig(n), by_left(n);
		for (int j = 0; j < i; j++) {
			if (ch_edg.find(X[j] * 1ll * n + Y[j]) == ch_edg.end()) {
				by_rig[max(X[j], Y[j])].pb(min(X[j], Y[j]));
				by_left[min(X[j], Y[j])].pb(max(X[j], Y[j]));
			}
		}
		vector<vector<pair<int, int>>> qnums(n - 1);
		for (int j = i; j < min(c, i + K); j++) {
			for (auto &[el, num] : qrs[j]) {
				qnums[el].pb({j, num});
			}
		}
		dsu d(n);
		for (int I = 0; I < n - 1; I++) {
			d.cmp++;
			for (auto &u : by_rig[I]) d.merge(u, I);
			for (auto &[time, numb] : qnums[I]) {
				step++;
				unordered_set<ll> add_edges = LOL;
				for (int j = i; j <= time; j++) {
					if (T[j] == 0) {
						add_edges.insert(X[j] * 1ll * n + Y[j]);
					} else {
						add_edges.erase(X[j] * 1ll * n + Y[j]);
					}
				}
				unordered_set<int> rdfs;
				for (auto E : add_edges) {
					ll u = E / n, v = E % n;
					if (max(u, v) <= I) {
						u = d.get(u);
						v = d.get(v);
						if (u != v) {
							if (used1[u] != step) gr[u].clear();
							if (used1[v] != step) gr[v].clear();
							used1[u] = step;
							used1[v] = step;
							gr[u].pb(v);
							gr[v].pb(u);
							rdfs.insert(u);
							rdfs.insert(v);
						}
					}
				}
				int ccmp = d.cmp;
				auto dfs = [&](int v, auto&&dfs) -> void {
					used_dfs[v] = step;
					for (auto &u : gr[v]) {
						if (used_dfs[u] != step) {
							ccmp--;
							dfs(u, dfs);
						}
					}
				};
				for (auto &u : rdfs) {
					if (used_dfs[u] != step) {
						dfs(u, dfs);
					}
				}
				ans[numb] += ccmp;
			}
		}
		iota(all(d.par), 0);
		d.siz.assign(n, 1);
		d.cmp = 0;
		for (int I = n - 1; I >= 1; I--) {
			d.cmp++;
			for (auto &u : by_left[I]) d.merge(u, I);
			for (auto &[time, numb] : qnums[I - 1]) {
				step++;
				unordered_set<ll> add_edges;
				for (int j = i; j <= time; j++) {
					if (T[j] == 0) {
						add_edges.insert(X[j] * 1ll * n + Y[j]);
					} else {
						add_edges.erase(X[j] * 1ll * n + Y[j]);
					}
				}
				unordered_set<int> rdfs;
				for (auto E : add_edges) {
					int u = E / n, v = E % n;
					if (min(u, v) >= I) {
						u = d.get(u);
						v = d.get(v);
						if (u != v) {
							if (used1[u] != step) gr[u].clear();
							if (used1[v] != step) gr[v].clear();
							used1[u] = step;
							used1[v] = step;
							gr[u].pb(v);
							gr[v].pb(u);
							rdfs.insert(u);
							rdfs.insert(v);
						}
					}
				}
				int ccmp = d.cmp;
				auto dfs = [&](int v, auto&&dfs) -> void {
					used_dfs[v] = step;
					for (auto &u : gr[v]) {
						if (used_dfs[u] != step) {
							ccmp--;
							dfs(u, dfs);
						}
					}
				};
				for (auto &u : rdfs) {
					if (used_dfs[u] != step) {
						dfs(u, dfs);
					}
				}
				ans[numb] += ccmp;
			}
		}
	}
	return ans;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 5
Accepted
time: 358ms
memory: 4632kb

input:

2 5000 5000
0 0 1
1 0 1
0 0 1
1 1 0
0 1 0
1 1 0
0 1 0
1 0 1
0 1 0
1 0 1
0 0 1
1 0 1
0 1 0
1 1 0
0 1 0
1 1 0
0 0 1
1 0 1
0 1 0
1 0 1
0 1 0
1 0 1
0 0 1
1 0 1
0 1 0
1 1 0
0 0 1
1 1 0
0 1 0
1 1 0
0 1 0
1 1 0
0 1 0
1 0 1
0 1 0
1 0 1
0 0 1
1 0 1
0 0 1
1 0 1
0 0 1
1 0 1
0 1 0
1 1 0
0 1 0
1 0 1
0 0 1
1 1 0
...

output:

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
...

result:

ok 5000 lines

Test #2:

score: 0
Wrong Answer
time: 0ms
memory: 4052kb

input:

5 7 5000
0 1 3
1 3 1
0 1 2
0 0 2
0 1 0
1 0 1
0 4 1
6 2
1 3
6 2
5 3
4 0
1 0
4 3
4 2
2 0
6 0
5 1
5 1
1 2
1 1
5 2
0 1
4 3
6 2
3 2
5 1
3 2
5 0
2 1
4 1
6 0
2 0
5 3
4 0
3 2
0 2
1 3
2 1
5 3
1 2
4 2
3 1
1 0
2 2
1 0
2 1
4 1
2 2
3 2
6 1
1 2
2 3
2 1
5 0
2 2
5 3
4 0
3 1
3 1
6 0
2 1
4 3
4 1
0 3
4 0
6 1
5 2
0 0
4...

output:

3
2
3
2
3
4
2
3
3
2
4
4
3
4
3
4
2
3
3
4
3
3
4
4
2
3
2
3
3
3
2
4
2
3
3
4
4
3
4
4
4
3
3
4
3
2
4
3
3
2
3
4
4
2
4
2
4
2
3
4
3
4
4
2
4
4
3
3
3
3
3
4
2
3
4
3
2
2
2
2
2
3
3
4
2
3
4
4
4
3
4
2
3
4
2
4
3
2
3
2
2
4
4
3
2
3
3
2
3
3
2
3
3
4
3
4
4
4
2
2
2
4
2
4
3
3
4
3
3
3
4
4
2
3
4
3
3
2
4
2
2
4
2
4
3
3
2
4
3
4
...

result:

wrong answer 2nd lines differ - expected: '5', found: '2'

Subtask #2:

score: 0
Wrong Answer

Test #13:

score: 30
Accepted
time: 19ms
memory: 7328kb

input:

2 1 100000
0 1 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0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wrong answer 1st lines differ - expected: '4', found: '3'

Subtask #3:

score: 0
Wrong Answer

Test #28:

score: 30
Accepted
time: 23ms
memory: 7480kb

input:

2 1 100000
0 1 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0...

output:

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
...

result:

ok 100000 lines

Test #29:

score: 0
Wrong Answer
time: 47ms
memory: 7008kb

input:

5 7 100000
0 3 1
0 4 1
0 2 3
0 2 1
0 2 4
0 2 0
0 4 0
1 0
3 0
4 0
0 3
1 2
2 0
4 0
2 1
5 1
4 0
2 3
0 2
0 3
6 0
4 2
5 2
4 3
5 0
0 0
5 3
0 1
4 0
4 0
3 1
3 0
1 1
6 3
0 0
0 0
0 3
5 3
3 2
6 0
5 0
4 1
0 0
5 0
0 3
2 3
1 2
2 1
6 2
0 0
3 2
0 0
2 2
1 0
1 1
0 2
1 0
5 3
5 2
2 1
2 2
3 0
2 3
1 3
1 0
3 1
5 2
1 2
5 0...

output:

3
2
2
2
3
2
2
4
3
2
2
3
2
2
3
3
2
2
4
2
5
2
2
4
2
5
2
4
4
2
2
3
2
2
3
4
2
2
2
3
4
3
4
3
4
3
3
5
3
3
2
3
4
3
2
2
2
3
4
3
3
2
3
3
5
2
3
2
3
2
3
5
2
3
2
3
3
3
2
4
3
2
5
3
2
2
3
2
3
3
2
3
2
3
2
2
4
4
2
5
3
2
4
3
4
4
3
3
2
2
3
2
5
3
3
2
3
4
3
3
3
3
2
3
2
3
3
2
3
2
2
3
2
3
4
2
5
3
2
3
2
2
3
3
4
2
2
5
5
4
...

result:

wrong answer 4th lines differ - expected: '4', found: '2'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%