QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#62541#1825. The King's Guards2pal1rak#TL 43ms3636kbC++203.8kb2022-11-20 02:12:302022-11-20 02:12:31

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-20 02:12:31]
  • 评测
  • 测评结果:TL
  • 用时:43ms
  • 内存:3636kb
  • [2022-11-20 02:12:30]
  • 提交

answer

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <random>
#include <cstdlib>

#define debug(x) std::cout << #x << " " << (x) << '\n';
#define pb push_back
#define mp std::make_pair
#define remax(a, b) a = std::max((a), (b));
#define remin(a, b) a = std::min((a), (b));

const int32_t INF = 2e9;

struct Edge {
	int32_t a, b, c;
};

struct maximum_bipartite_matching {
	int32_t nl, nr, mbm = 0;
	std::vector<bool> v;
	std::vector<int32_t> ml, mr, d;
	std::vector<std::vector<int32_t>> &e;

	maximum_bipartite_matching(int32_t nl, int32_t nr, std::vector<std::vector<int32_t>> &e)
			: nl(nl), nr(nr), ml(nl, -1), mr(nr, -1), e(e) {
		int32_t prv = 0;
		do {
			prv = mbm;
			d.assign(nr, INF);
			std::queue<int32_t> q;
			for(int32_t i = 0; i < nl; i++) {
				if(ml[i] == -1) {
					for(auto j : e[i]) {
						if(d[j] == INF) {
							d[j] = 0;
							q.push(j);
						}
					}
				}
			}

			while(!q.empty()) {
				int32_t i = q.front();
				q.pop();

				if(mr[i] == -1) {
					for(int32_t j = 0; j < nr; j++) {
						if(d[j] > d[i]) {
							d[j] = INF;
						}
					}
					break;
				}

				for(auto j : e[mr[i]]) {
					if(d[j] == INF) {
						d[j] = d[i] + 1;
						q.push(j);
					}
				}
			}

			for(int32_t i = 0; i < nl; i++) {
				if(ml[i] == -1) {
					mbm += findPath(i);
				}
			}
		} while(mbm > prv);
	}

	bool findPath(int32_t i, int32_t l = 0) {
		for(auto j : e[i]) {
			if(d[j] == l) {
				d[j] = INF;
				if(mr[j] == -1 || findPath(mr[j], l + 1)) {
					ml[i] = j;
					mr[j] = i;
					return true;
				}
			}
		}

		return false;
	}
};

bool check(std::vector<std::vector<int32_t>> &x, int32_t cntComps) {
	maximum_bipartite_matching mbm(x.size(), cntComps, x);
	return (mbm.mbm == x.size());
}

std::vector<std::vector<int32_t>> mergeComps(
			std::vector<std::vector<int32_t>> &comps, int32_t comp1, int32_t comp2) {
	std::vector<std::vector<int32_t>> newComps;

	for(int32_t i = 0; i < comps.size(); i++) {
		if(i != comp1 && i != comp2) {
			newComps.pb(comps[i]);
		}
	}

	newComps.pb(comps[comp1]);
	for(auto i : comps[comp2]) {
		newComps.back().pb(i);
	}

	return newComps;
}

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int32_t n, r, g;
	std::cin >> n >> r >> g;

	std::vector<Edge> edges(r);
	for(int32_t i = 0; i < r; i++) {
		std::cin >> edges[i].a >> edges[i].b >> edges[i].c;
	}

	std::vector<std::vector<int32_t>> v(g);
	for(int32_t i = 0; i < g; i++) {
		int32_t k;
		std::cin >> k;

		v[i].resize(k);
		for(int32_t j = 0; j < k; j++) {
			std::cin >> v[i][j];
		}
	}

	std::sort(edges.begin(), edges.end(), [](Edge e1, Edge e2) {
				return e1.c < e2.c;
			});

	std::vector<int32_t> compInd(n + 1);
	std::vector<std::vector<int32_t>> comps;
	for(int32_t i = 1; i <= n; i++) {
		comps.pb({ i });
		compInd[i] = i - 1;
	}

	int32_t ans = 0;
	for(int32_t i = 0; i < r && comps.size() > g; i++) {
		if(compInd[edges[i].a] == compInd[edges[i].b]) continue;

		int32_t comp1 = compInd[edges[i].a];
		int32_t comp2 = compInd[edges[i].b];

		std::vector<std::vector<int32_t>> x(g);
		for(int32_t i = 0; i < g; i++) {
			for(auto &y : v[i]) {
				if(compInd[y] == comp1) {
					x[i].pb(comp2);
				}
				else {
					x[i].pb(compInd[y]);
				}
			}
		}

		if(check(x, comps.size())) {
			comps = mergeComps(comps, comp1, comp2);
			for(int32_t c = 0; c < comps.size(); c++) {
				for(auto &y : comps[c]) {
					compInd[y] = c;
				}
			}
			ans += edges[i].c;
		}
	}
	
	std::cout << ans << '\n';
}


详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 3312kb

input:

5 6 2
1 2 1
1 3 4
2 4 2
2 5 5
3 4 7
4 5 3
2 1 2
2 2 4

output:

8

result:

ok answer is '8'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3536kb

input:

10 19 6
1 5 761
6 8 606
3 9 648
2 4 115
5 8 814
1 2 712
4 10 13
5 10 797
3 4 956
1 7 73
5 7 192
2 7 110
5 9 302
3 6 120
6 9 494
1 3 668
3 7 966
6 10 974
3 8 41
2 10 5
3 6 4 3
2 1 7
2 10 8
3 10 7 8
2 2 1

output:

429

result:

ok answer is '429'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3336kb

input:

10 43 3
1 3 656
2 6 856
4 10 99
5 6 900
2 7 766
4 7 582
2 8 135
5 7 831
3 5 12
3 10 789
1 8 66
4 9 390
1 7 238
6 7 960
1 4 624
3 9 602
7 10 366
5 8 526
2 9 561
6 10 722
2 5 904
3 4 35
1 9 768
5 9 457
6 8 61
4 6 192
4 5 96
5 10 747
8 9 611
7 8 953
3 8 449
2 4 228
1 6 197
9 10 160
3 6 869
1 2 785
4 8 ...

output:

526

result:

ok answer is '526'

Test #4:

score: 0
Accepted
time: 7ms
memory: 3636kb

input:

277 9038 1
226 260 740
44 226 376
151 263 611
67 269 241
120 181 677
259 271 782
37 52 310
48 152 452
168 266 823
85 234 100
46 201 738
129 153 301
69 147 434
13 72 764
13 234 316
171 222 398
214 255 21
112 158 430
20 118 407
45 152 971
205 214 272
221 275 362
198 268 472
117 176 207
31 75 652
139 1...

output:

5375

result:

ok answer is '5375'

Test #5:

score: 0
Accepted
time: 43ms
memory: 3604kb

input:

297 27966 132
15 197 980
226 259 950
161 168 142
118 176 834
157 221 806
24 210 432
212 242 838
110 166 177
78 170 801
52 166 3
89 213 448
45 170 626
250 251 268
93 222 679
7 128 839
5 7 320
132 191 1
192 295 717
36 231 542
162 175 508
173 178 458
211 272 926
46 168 145
19 150 805
165 262 198
50 179...

output:

775

result:

ok answer is '775'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3440kb

input:

7 7 4
1 3 7
1 4 6
2 3 5
2 4 6
4 5 10
4 6 10
4 7 10
5 4 3 2 7 5
6 5 2 6 7 4 1
2 2 3
6 7 2 5 3 1 6

output:

17

result:

ok answer is '17'

Test #7:

score: -100
Time Limit Exceeded

input:

300 44850 299
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
...

output:


result: