QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#561284 | #7415. Fast Spanning Tree | dczissb | TL | 0ms | 17972kb | C++14 | 1.3kb | 2024-09-12 21:38:31 | 2024-09-12 21:38:31 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,s[300001],fa[300001];
vector<int> ans;
int gf(int x){
if(fa[x]==x) return x;
return fa[x]=gf(fa[x]);
}
struct sbdcz{
int u,d;
bool operator<(const sbdcz &jjrt)const{
return jjrt.d<d;
}
};
priority_queue<sbdcz> q,qn[300001];
struct dcz{
int x,y,z;
}a[300001];
void update(int x){
int fx=gf(a[x].x),fy=gf(a[x].y);
if(fx==fy) return;
if(s[fx]+s[fy]>=a[x].z){
q.push({0,x});
return;
}
int o=(a[x].z-s[fx]-s[fy]+1)/2;
qn[fx].push({x,o-s[fx]});
qn[fy].push({x,o-s[fy]});
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>s[i];
fa[i]=i;
}
for(int i=1;i<=m;i++){
cin>>a[i].x>>a[i].y>>a[i].z;
update(i);
}
while(q.size()){
int x=q.top().d;
q.pop();
int fx=gf(a[x].x),fy=gf(a[x].y);
if(fx==fy) continue;
ans.push_back(x);
if(qn[fx].size()<qn[fy].size()) swap(fx,fy);
fa[fy]=fx;
s[fx]+=s[fy];
while(qn[fy].size()){
sbdcz xx=qn[fy].top();
qn[fy].pop();
if(xx.d<=s[fx]){
update(xx.u);
}
else{
qn[fx].push(xx);
}
}
while(qn[fx].size()){
sbdcz xx=qn[fx].top();
qn[fx].pop();
if(xx.d<=s[fx]){
update(xx.u);
}
else{
qn[fx].push(xx);
break;
}
}
}
cout<<ans.size()<<"\n";
for(auto i:ans){
cout<<i<<" ";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15884kb
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: 17972kb
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: 0ms
memory: 15888kb
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