QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#640858#9349. Exchanging GiftsKeeperHihi#WA 304ms3776kbC++201.5kb2024-10-14 16:32:532024-10-14 16:32:54

Judging History

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

  • [2024-10-14 16:32:54]
  • 评测
  • 测评结果:WA
  • 用时:304ms
  • 内存:3776kb
  • [2024-10-14 16:32:53]
  • 提交

answer

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

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++) {
				int 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];
		}
	}	
	priority_queue<i64> q;
	for (auto [a, b] : map) {
		q.emplace(b);
	}
	i64 ans = 0;
	while (q.size() > 1) {
		i64 u = q.top();
		q.pop();
		i64 v = q.top();
		q.pop();
		ans += min(u, v);
		if (max(u, v) - min(u, v)) {
			q.emplace(max(u, v) - min(u, v));
		}
	}
	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: 3528kb

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: 304ms
memory: 3776kb

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:

660
68
330
284
130
956
194
664
704
0
484
250
34
266
1184
236
980
310
372
566
3756
330
1044
196
448
188
0
68
624
390
244
882
134
1560
90
3306
2420
1278
430
604
394
302
380
2838
0
496
1198
52
0
414
654
378
40
18
90
502
816
590
204
760
974
1790
168
170
650
1452
452
296
230
236
732
0
576
1696
726
100
0
...

result:

wrong answer 1st lines differ - expected: '700', found: '660'