QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#293883#7069. FarmYaoBIGWA 177ms14060kbC++175.5kb2023-12-29 21:47:112023-12-29 21:47:12

Judging History

This is the latest submission verdict.

  • [2023-12-29 21:47:12]
  • Judged
  • Verdict: WA
  • Time: 177ms
  • Memory: 14060kb
  • [2023-12-29 21:47:11]
  • 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>;

/**
 * Author: Yuhao Yao
 * Date: 22-10-24
 * Description: Disjoint set union. $merge(x, y)$ merges components which $x$ and $y$ are in respectively and returns $1$ if $x$ and $y$ are in different components.
 * Time: amortized O(\alpha(M, N)) where $M$ is the number of operations. Almost constant in competitive programming.
 */
struct DSU {
	vi fa, siz;

	DSU(int n): fa(n), siz(n, 1) { iota(all(fa), 0); }

	int getcomp(int x) { 
		return fa[x] == x ? x : fa[x] = getcomp(fa[x]);
	}

	bool merge(int x, int y) {
		int fx = getcomp(x), fy = getcomp(y);
		if (fx == fy) return 0;
		if (siz[fx] < siz[fy]) swap(fx, fy);
		fa[fy] = fx;
		siz[fx] += siz[fy];
		return 1;
	}
};

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 {
		auto cal = [&](int x, int y) {
			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));
			assert(sz(as) > 0);
			return *max_element(all(as), [&](int i, int j) { return rank[i] < rank[j]; });
		};
		vector<int> vec;
		for (auto [i, j] : qs) {
			vec.push_back(get<0>(es[i]));
			vec.push_back(get<1>(es[i]));
			vec.push_back(get<0>(es[j]));
			vec.push_back(get<1>(es[j]));
		}
		sort(all(vec)); vec.erase(unique(all(vec)), vec.end());
		int tot = sz(vec);
		vector mp(tot, vector<int>(tot, -1));
		rep(i, 0, tot - 1) rep(j, i + 1, tot - 1) {
			mp[i][j] = mp[j][i] = cal(vec[i], vec[j]);
		}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 110ms
memory: 14032kb

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: 113ms
memory: 14060kb

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: 106ms
memory: 13064kb

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: 122ms
memory: 12940kb

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: 0
Accepted
time: 132ms
memory: 12948kb

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:

1533

result:

ok single line: '1533'

Test #7:

score: -100
Wrong Answer
time: 177ms
memory: 12948kb

input:

100 500000
10 46 133
79 13 987
26 2 743
8 47 390
79 19 737
11 64 197
16 65 207
73 9 944
77 58 841
50 3 245
81 100 293
21 12 713
60 65 155
89 87 865
88 67 278
9 15 920
46 52 704
26 26 731
44 98 525
20 68 346
14 95 932
84 19 697
41 21 290
83 24 750
3 71 369
54 80 396
20 70 208
25 55 456
40 22 938
90 1...

output:

2210

result:

wrong answer 1st lines differ - expected: '2209', found: '2210'