QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#555692 | #7415. Fast Spanning Tree | czc | TL | 3ms | 22304kb | C++23 | 2.0kb | 2024-09-10 08:15:21 | 2024-09-10 08:15:23 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
int n,m,a[maxn];
int fa[maxn];
inline int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
priority_queue<int,vector<int>,greater<int> > q;
struct node{
int val,id,now;
};
inline bool operator < (node x,node y){
if(x.val+x.now==y.val+y.now) return x.id<y.id;
return x.val+x.now<y.val+y.now;
}
set<node> b[maxn];
struct cz{
int x,y,s;
}c[maxn];
int tag[maxn];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),fa[i]=i;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&c[i].x,&c[i].y,&c[i].s);
if(a[c[i].x]+a[c[i].y]>=c[i].s){
q.push(i);
}
else{
b[c[i].x].insert((node){c[i].s/2,i,0});
b[c[i].y].insert((node){c[i].s/2,i,0});
}
}
vector<int> ans;
while(!q.empty()){
int id=q.top();
q.pop();
if(find(c[id].x)==find(c[id].y)) continue;
ans.push_back(id);
int x=find(c[id].x),y=find(c[id].y);
if(b[x].size()<b[y].size()) swap(x,y);
a[x]+=a[y],fa[y]=x;
for(auto z:b[y]){
if(find(c[z.id].x)!=find(c[z.id].y)) b[x].insert(z);
}
b[y].clear();
while(b[x].size() && (*b[x].begin()).val+(*b[x].begin()).now<=a[x]){
node czc=*b[x].begin();
b[x].erase(b[x].begin());
if(find(c[czc.id].x)==find(c[czc.id].y)) continue;
if(a[find(c[czc.id].x)]+a[find(c[czc.id].y)]>=c[czc.id].s){
q.push(czc.id);
}
else{
b[find(c[czc.id].x)].insert((node){(c[czc.id].s-a[find(c[czc.id].x)]-a[find(c[czc.id].y)])/2,czc.id,a[find(c[czc.id].x)]});
b[find(c[czc.id].y)].insert((node){(c[czc.id].s-a[find(c[czc.id].x)]-a[find(c[czc.id].y)])/2,czc.id,a[find(c[czc.id].y)]});
}
}
}
printf("%d\n",(int)ans.size());
for(auto x:ans) printf("%d ",x);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 20192kb
input:
5 5 1 4 3 4 0 4 5 5 3 1 1 2 5 2 4 3 1 4 1 4
output:
4 2 3 1 4
result:
ok 5 number(s): "4 2 3 1 4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 22292kb
input:
3 5 3 2 2 1 2 6 1 2 6 1 2 3 1 2 6 2 3 6
output:
2 3 5
result:
ok 3 number(s): "2 3 5"
Test #3:
score: 0
Accepted
time: 3ms
memory: 22304kb
input:
10 20 4 6 6 1 7 9 1 8 7 9 5 3 2 1 10 10 5 6 7 9 6 9 3 1 1 6 8 1 5 7 0 9 5 4 10 3 4 8 6 8 3 10 6 5 3 8 3 7 9 1 9 10 10 1 0 5 7 6 6 10 1 6 5 9 5 8 2 9 2 4
output:
8 1 2 3 4 5 6 7 20
result:
ok 9 numbers
Test #4:
score: -100
Time Limit Exceeded
input:
10 20 0 10 4 6 2 0 2 0 2 8 5 10 8 7 1 6 10 5 0 9 8 0 5 8 4 5 1 8 10 3 6 5 4 3 9 2 6 10 3 6 4 3 1 3 10 3 6 1 3 3 2 5 6 9 2 4 2 5 10 6 5 8 6 3 2 1 0 2 3 6