QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#640917#9349. Exchanging GiftsKeeperHihi#WA 291ms3668kbC++201.4kb2024-10-14 16:54:172024-10-14 16:54:18

Judging History

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

  • [2024-10-14 16:54:18]
  • 评测
  • 测评结果:WA
  • 用时:291ms
  • 内存:3668kb
  • [2024-10-14 16:54:17]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define int i64

void solve() {
	int n;
	cin >> n;
	vector<vector<int>> adj(n + 1);
	vector<vector<i64>> input(n + 1);
	vector<i64> cnt(n + 1);
	for (int i = 1; i <= n; i++) {
		int op;
		cin >> op;
		if (op == 1) {
			int k;
			cin >> k;
			for (int j = 0; j < k; j++) {
				i64 x;
				cin >> x;
				input[i].emplace_back(x);
			}
		} else {
			int x, y;
			cin >> x >> y;
			adj[x].emplace_back(i);
			adj[y].emplace_back(i);
			// adj[i].emplace_back(x);
			// adj[i].emplace_back(y);
		}
	}
	vector<bool> vis(n + 1);
	cnt[n] = 1;
	vis[n] = 1;
	auto dfs = [&](auto self, int u, int fa) -> void {
		if (vis[u]) return;
		vis[u] = 1;
		for (auto v : adj[u]) {
			if (!vis[v]) {
				self(self, v, u);
			}
			cnt[u] += cnt[v];
		}
	};
	for (int i = 1; i <= n; i++) {
		if (!input[i].empty()) {
			dfs(dfs, i, 0);
		}
	}
	map<i64, i64> map;
	for (int i = 1; i <= n; i++) {
		if (!cnt[i]) continue;
		for (auto t : input[i]) {
			map[t] += cnt[i];
		}
	}	
	i64 x = 0;
	i64 mx = 0;
	for (auto [a, b] : map) {
		x += b;
		mx = max(mx, b);
	}
	x -= mx;
	i64 ans = 0;
	if (mx >= x) {
		ans = x;
	} else {
		ans = (x + mx) / 2;
	}
	cout << ans * 2 << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int t = 1;
	cin >> t;

	while (t--) {
		solve();
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
1
1 5 3 3 2 1 3
3
1 3 3 3 2
1 4 2 2 3 3
2 1 2

output:

4
6

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 291ms
memory: 3668kb

input:

10000
100
1 30 371028678 371028678 371028678 716418076 398221499 591504380 398221499 398221499 591504380 777141390 398221499 591504380 591504380 777141390 287847807 716418076 777141390 716418076 777141390 287847807 287847807 287847807 371028678 371028678 398221499 777141390 371028678 6827702 6827702...

output:

700
68
332
284
130
1048
194
666
704
0
484
252
34
350
1228
238
1024
354
382
570
4272
340
1044
198
448
190
0
68
840
546
246
882
138
1632
90
3308
2556
1280
488
618
406
380
382
2864
0
496
1202
52
0
414
662
380
40
18
90
504
818
602
240
764
1226
1802
176
186
816
1488
460
296
238
236
1028
0
606
1696
746
10...

result:

wrong answer 5th lines differ - expected: '131', found: '130'