QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#625776 | #9349. Exchanging Gifts | Godwang# | TL | 0ms | 32484kb | C++14 | 1.6kb | 2024-10-09 21:00:00 | 2024-10-09 21:00:00 |
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;
unordered_map<int, long long> mp;
inline int read() {
int s = 0;
char c = getchar();
while (c < '0' || c > '9') {
c = getchar();
}
while (c >= '0' && c <= '9') {
s = s * 10 + c - '0';
c = getchar();
}
return s;
}
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 (unordered_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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 32484kb
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:
1160