QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625773 | #9349. Exchanging Gifts | Godwang# | TL | 4ms | 34352kb | C++14 | 1.4kb | 2024-10-09 20:58:07 | 2024-10-09 20:58:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n, T, t;
struct Node {
int op, x, y;
} ask[N];
vector<int> a[N];
int len[N];
long long tot[N];
bool vis[N];
priority_queue<int> q;
map<int, long long> mp;
int main()
{
ios::sync_with_stdio(false);cin.tie();cout.tie();
cin>>T;
while (T--) {
cin>>n;
for (int i = 1; i <= n; i++) {
cin>>ask[i].op;
if (ask[i].op == 1) {
a[i].clear();
cin>>len[i];
for (int j = 1; j <= len[i]; j++) {
cin>>t;
a[i].push_back(t);
}
}
else {
cin>>ask[i].x>>ask[i].y;
}
}
q.push(n); vis[n] = 1;
tot[n] = 1;
for (int i = n - 1; i >= 1; i--) {
int tp = q.top();
q.pop();
if (ask[tp].op == 1) continue;
if (!vis[ask[tp].x]) q.push(ask[tp].x), vis[ask[tp].x] = 1;
if (!vis[ask[tp].y]) q.push(ask[tp].y), vis[ask[tp].y] = 1;
tot[ask[tp].x] += tot[tp];
tot[ask[tp].y] += tot[tp];
}
for (int i = 1; i <= n; i++) {
if (ask[i].op == 2) continue;
for (int j = 0; j < len[i]; j++) {
mp[a[i][j]] += tot[i];
}
}
long long t=0,ans=0;
for (map<int,long long>::iterator it=mp.begin();it!=mp.end();it++) {
if (it->second >= n / 2)t=it->second;
ans+=it->second;
}
if(!t)cout<<ans<<endl;
else cout<<ans*2-t*2<<endl;
mp.clear();while(!q.empty())q.pop();
for(int i=1;i<=n;i++)tot[i]=vis[i]=0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 34352kb
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
Time Limit Exceeded
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:
1196