QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#177303#4580. Bicycle Tourucup-team288#WA 9ms4120kbC++202.3kb2023-09-12 20:01:312023-09-12 20:01:31

Judging History

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

  • [2023-09-12 20:01:31]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:4120kb
  • [2023-09-12 20:01:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ll = long long;
#define pb emplace_back
#define X first
#define Y second
#define AI(i) begin(i), end(i)
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true);}
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true);}
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l++ << " \n"[l==r]; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

struct DSU {
	int n;
	vector<int> fa, sz;
	DSU(int n) : n(n), fa(n), sz(n, 1) {
		iota(fa.begin(), fa.end(), 0);
	}
	int find(int u) {
		return u == fa[u] ? u : find(fa[u]);
	}
	bool join(int u, int v) {
		u = find(u), v = find(v);
		if (u == v) {
			return false;
		}
		sz[u] += sz[v];
		fa[v] = u;
		return true;
	}
};

const int MAX_N = 300010;
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);

	int n, m;
	cin >> n >> m;

	vector<int> h(n);
	for (int i = 0; i < n; i++) {
		cin >> h[i];
	}

	vector<array<int, 3>> e(m);
	for (auto &[w, u, v] : e) {
		cin >> u >> v;
		u--, v--;
		w = abs(h[u] - h[v]);
	}

	sort(e.begin(), e.end());

	vector<vector<int>> tree(n + 1);
	vector<array<int, 3>> nontree;
	DSU d(n);

	for (auto [w, u, v] : e) {
		if (d.join(u, v)) {
			tree[u].push_back(v);
			tree[v].push_back(u);
		} else {
			nontree.push_back({w, u, v});
		}
	}
	tree[n].push_back(0);

	vector<int> par(n + 1, -1), dep(n + 1);
	auto dfs = [&](auto dfs, int u, int p) -> void {
		par[u] = p;
		for (auto v : tree[u]) {
			if (v == p) {
				continue;
			}
			dep[v] = dep[u] + 1;
			dfs(dfs, v, u);
		}
	};
	dfs(dfs, n, -1);

	debug(dep.begin(), dep.end());

	vector<int> ans(n, -1);
	d = DSU(n + 1);

	for (auto [w, u, v] : nontree) {
		DE(w, u + 1, v + 1);
		u = d.find(u), v = d.find(v);
		while (u != v) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
			ans[u] = w;
			d.join(par[u], u);
			u = d.find(u);
		}
		if (u != n) {
			ans[u] = w;
			d.join(par[u], u);
		}
	}

	for (int i = 0; i < n; i++) {
		cout << ans[i] << " \n"[i == n - 1];
	}
}


詳細信息

Test #1:

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

input:

8 11
5 2 7 0 10 6 6 6
1 2
1 3
2 3
2 4
2 5
2 7
3 5
1 6
6 7
6 8
7 8

output:

4 4 5 -1 8 0 0 0

result:

ok single line: '4 4 5 -1 8 0 0 0'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

4 5
10 20 30 40
1 2
1 3
1 4
2 3
3 4

output:

20 20 20 30

result:

ok single line: '20 20 20 30'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

5 4
72 35 22 49 108
1 2
2 3
3 4
4 5

output:

-1 -1 -1 -1 -1

result:

ok single line: '-1 -1 -1 -1 -1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

2 1
10 20
1 2

output:

-1 -1

result:

ok single line: '-1 -1'

Test #5:

score: -100
Wrong Answer
time: 9ms
memory: 4120kb

input:

252 31626
825 5234 3578 4723 2145 4362 1861 2413 7203 1939 3210 7153 2155 4559 4403 1466 887 3786 6529 719 4272 3287 5703 6708 2390 4987 4214 770 3487 6230 3498 6255 4963 1093 3065 2961 1663 4857 3224 4284 4228 106 1614 1010 145 1557 4510 1032 4632 155 5570 154 884 1204 2876 6163 5023 4593 7261 3729...

output:

18 33 31 24 10 30 31 30 22 30 24 32 10 9 26 34 14 30 17 9 13 15 47 33 23 22 13 17 11 32 11 26 22 35 18 13 14 0 21 13 11 42 33 22 9 33 22 22 29 7 49 7 14 34 13 34 14 8 15 31 10 20 21 24 66 9 20 24 29 6 30 16 26 9 6 27 8 31 21 17 12 26 18 17 10 33 28 9 33 15 14 14 24 22 49 66 45 24 16 7 23 22 15 18 22...

result:

wrong answer 1st lines differ - expected: '55 33 46 34 10 33 34 30 22 33 ...45 15 71 10 52 18 16 30 10 34 4', found: '18 33 31 24 10 30 31 30 22 30 ...22 15 34 10 48 18 16 30 10 27 4'