QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#640901 | #9349. Exchanging Gifts | KeeperHihi# | WA | 300ms | 3744kb | C++20 | 1.8kb | 2024-10-14 16:49:12 | 2024-10-14 16:49:12 |
Judging History
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 x = 0;
i64 mx = q.top();
q.pop();
while (!q.empty()) {
x += q.top();
q.pop();
}
i64 ans = 0;
if (mx >= x) {
ans = x;
} else {
ans = (x + mx) / 2;
}
cout << ans * 2 << "\n";
// return;
// while (q.size() > 1) {
// i64 u = q.top();
// q.pop();
// i64 v = q.top();
// q.pop();
// if (q.empty()) {
// ans += min(u, v);
// if (max(u, v) - min(u, v)) {
// q.emplace(max(u, v) - min(u, v));
// }
// } else {
// i64 x = q.top();
// }
// }
// 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: 3744kb
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: 300ms
memory: 3544kb
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'