QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#293749#7069. FarmYaoBIGWA 115ms16276kbC++174.3kb2023-12-29 18:01:152023-12-29 18:01:15

Judging History

This is the latest submission verdict.

  • [2023-12-29 18:01:15]
  • Judged
  • Verdict: WA
  • Time: 115ms
  • Memory: 16276kb
  • [2023-12-29 18:01:15]
  • Submitted

answer

#include "bits/stdc++.h"
#define rep(i, a, n) for (auto i = a; i <= (n); ++i)
#define revrep(i, a, n) for (auto i = n; i >= (a); --i)
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a).size()
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; }
using namespace std;

template<class A, class B> string to_string(const pair<A, B> &p);
template<class A, class B, class C> string to_string(const tuple<A, B, C> &p);
template<class A, class B, class C, class D> string to_string(const tuple<A, B, C, D> &p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string) s); }
string to_string(char c) { return "'" + string(1, c) + "'"; }
string to_string(bool x) { return x ? "true" : "false"; }
template<class A, class T = typename A::value_type> string to_string(const A &v) {
	bool first = 1;
	string res = "{";
	for (const auto &x: v) {
		if (!first) res += ", ";
		first = 0;
		res += to_string(x);
	}
	res += "}";
	return res;
}
template<class A, class B> string to_string(const pair<A, B> &p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template<class A, class B, class C> string to_string(const tuple<A, B, C> &p) {
	return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template<class A, class B, class C, class D> string to_string(const tuple<A, B, C, D> &p) {
	return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }
template<class H, class... T> void debug_out(const H& h, const T&... t) {
	cerr << " " << to_string(h);
	debug_out(t...);
}
#ifndef ONLINE_JUDGE
	#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
	#define debug(...) if (0) puts("No effect.")
#endif

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;


int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n, m; cin >> n >> m;
	vector<tuple<int, int, int>> es(m);
	for (auto &[x, y, w] : es) {
		cin >> x >> y >> w;
		x--, y--;
	}
	int q; cin >> q;
	vector<pii> qs(q);
	for (auto &[x, y] : qs) {
		cin >> x >> y;
		x--, y--;
	}

	vector<int> ps(m); iota(all(ps), 0);
	sort(all(ps), [&](int i, int j) { return get<2>(es[i]) < get<2>(es[j]); });
	vector<int> rank(m);
	rep(i, 0, m - 1) rank[ps[i]] = i;

	vector<int> fa(n), siz(n, 1), up_inds(n, -1);
	iota(all(fa), 0);
	auto getfa = [&](auto &dfs, int x) {
		if (x == fa[x]) return x;
		else return dfs(dfs, fa[x]);
	};

	int sum = 0;
	for (auto ind : ps) {
		auto [x, y, w] = es[ind];
		int fx = getfa(getfa, x);
		int fy = getfa(getfa, y);
		if (fx != fy) {
			if (siz[fx] < siz[fy]) swap(fx, fy);
			siz[fx] += siz[fy];
			fa[fy] = fx;
			up_inds[fy] = ind;
			sum += w;
		}
	}

	bool conn = false;
	rep(i, 0, n - 1) if (siz[i] == n) conn = true;

	if (!conn) {
		puts("-1");
	} else {
		vector<int> mp(m, -1);
		auto cal = [&](int ind) {
			auto [x, y, w] = es[ind];
			auto get = [&](int x) {
				vector<int> res;
				while (x != fa[x]) {
					res.push_back(up_inds[x]);
					x = fa[x];
				};
				return res;
			};
			auto as = get(x);
			auto bs = get(y);
			while (sz(as) > 0 && sz(bs) > 0 && as.back() == bs.back()) {
				as.pop_back();
				bs.pop_back();
			}
			as.insert(as.end(), all(bs));
			if (sz(as) == 0) return;
			mp[ind] = *max_element(all(as), [&](int i, int j) { return rank[i] < rank[j]; });
			return;
		};
		for (auto [i, j] : qs) {
			cal(i);
			cal(j);
		}

		int ans = 0x3f3f3f3f;
		rep(msk, 0, (1 << q) - 1) {
			vector<int> as;
			vector<int> bs;
			rep(i, 0, q - 1) {
				auto [x, y] = qs[i];
				if (msk >> i & 1) {
					as.push_back(y);
				} else {
					as.push_back(x);
				}
			}
			for (auto x : as) if (mp[x] != -1) bs.push_back(mp[x]);
			sort(all(as)); as.erase(unique(all(as)), as.end());
			sort(all(bs)); bs.erase(unique(all(bs)), bs.end());
			int s = 0;
			for (auto i : as) s += get<2>(es[i]);
			for (auto i : bs) s -= get<2>(es[i]);
			chmin(ans, s);
		}
		assert(ans >= 0);
		ans += sum;
		printf("%d\n", ans);
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3780kb

input:

4 6
1 1 2
2 4 3
1 1 4
2 4 4
3 2 4
1 3 4
1
1 2

output:

11

result:

ok single line: '11'

Test #2:

score: 0
Accepted
time: 109ms
memory: 14024kb

input:

100000 500000
2516 13348 191
37713 25720 216
41568 13765 877
2116 27917 895
76904 65435 37
73053 24687 44
97127 44338 700
2251 85769 378
95166 20208 42
59303 57463 158
26863 18030 31
58613 6818 2
15455 18106 254
3232 13720 610
85677 16778 650
25618 72746 813
80365 162 47
10930 7403 645
79272 54568 6...

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 103ms
memory: 16276kb

input:

100000 500000
34497 87538 658
69862 2776 861
93620 16992 904
77910 81200 149
83935 83752 880
17602 75791 259
85887 53289 710
4200 79358 181
8518 19264 737
94665 47462 822
50632 51994 143
55224 59127 656
615 92858 150
48450 9465 58
35713 45287 140
64861 32248 517
70296 45113 153
11189 90316 809
40673...

output:

12148224

result:

ok single line: '12148224'

Test #4:

score: 0
Accepted
time: 94ms
memory: 14880kb

input:

1 500000
1 1 963
1 1 349
1 1 157
1 1 6
1 1 312
1 1 377
1 1 783
1 1 42
1 1 18
1 1 327
1 1 499
1 1 824
1 1 343
1 1 798
1 1 193
1 1 667
1 1 378
1 1 641
1 1 692
1 1 622
1 1 584
1 1 590
1 1 324
1 1 858
1 1 914
1 1 601
1 1 734
1 1 61
1 1 559
1 1 681
1 1 825
1 1 888
1 1 585
1 1 55
1 1 818
1 1 190
1 1 278
1...

output:

1605

result:

ok single line: '1605'

Test #5:

score: 0
Accepted
time: 108ms
memory: 14932kb

input:

5 500000
5 1 817
2 1 273
3 5 674
1 5 15
5 2 872
3 4 728
3 2 807
5 3 28
2 5 96
1 5 100
4 2 224
4 4 980
5 5 727
2 2 520
4 1 29
2 1 142
4 2 963
4 4 118
4 4 615
4 3 719
5 3 200
5 2 746
4 2 68
5 4 859
1 3 182
3 4 286
3 1 229
4 1 895
2 1 730
1 2 622
2 4 913
2 1 697
5 5 130
4 5 507
5 2 425
2 4 716
2 1 884
...

output:

3097

result:

ok single line: '3097'

Test #6:

score: -100
Wrong Answer
time: 115ms
memory: 14836kb

input:

10 500000
3 8 138
10 7 593
4 3 8
7 5 516
10 4 49
3 8 601
6 7 481
8 5 429
6 4 241
1 6 504
6 2 252
7 1 656
5 1 350
5 9 485
7 8 669
5 8 630
9 9 324
1 3 427
1 2 309
5 10 236
4 6 926
8 7 34
5 1 336
7 5 581
4 5 228
10 3 909
2 9 726
4 2 444
10 1 55
1 2 244
5 8 261
2 7 556
10 2 165
6 3 657
7 5 580
7 1 827
1...

output:

1534

result:

wrong answer 1st lines differ - expected: '1533', found: '1534'