QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#736043 | #7415. Fast Spanning Tree | wsc2008 | TL | 5ms | 40704kb | C++14 | 2.6kb | 2024-11-11 23:48:00 | 2024-11-11 23:48:01 |
Judging History
answer
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pii pair<ll,ll>
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
using namespace std;
bool Mbe;
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void write(ll x){
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
#define PQ priority_queue<array<ll,3>,vector<array<ll,3> >,greater<array<ll,3> > >
const ll N=6e5+9;
ll n,m,W[N],A[N],B[N],S[N],rS[N],fa[N],sz[N],tag[N],psh[N],in[N];
PQ q[N];
priority_queue<ll,vector<ll>,greater<ll> >Q;
ll find(ll x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
queue<ll>wq;
void Remake(ll y){
in[y]=0;
while(!q[y].empty()&&q[y].top()[0]-tag[y]<=0){
array<ll,3> p=q[y].top();q[y].pop();
// cout<<p[0]<<" "<<tag[y]<<" "<<p[1]<<" "<<p[2]<<" "<<rS[p[2]]<<endl;
ll id=p[2],fx=find(A[id]),fy=find(B[id]);
if(psh[id])continue;
if(tag[fx]+tag[fy]>=S[id]){
Q.push(id),psh[id]=1;
continue;
}
if(rS[id]!=p[1]||fx==fy)continue;
rS[id]=p[0];
ll mid=(S[id]-tag[fx]-tag[fy]+1)/2;
q[fx].push({mid+tag[fx],rS[id],id});
q[fy].push({mid+tag[fx],rS[id],id});
if(!in[fx])wq.push(fx),in[fx]=1;
if(!in[fy])wq.push(fy),in[fy]=1;
}
}
void Remake(){
while(!wq.empty())Remake(wq.front()),wq.pop();
}
bool Unite(ll x,ll y){
x=find(x),y=find(y);
if(x==y)return 0;
if(sz[x]>sz[y])swap(x,y);
while(!q[x].empty()){
array<ll,3> p=q[x].top();q[x].pop();
q[y].push({p[0]-tag[y],p[1],p[2]});
}
tag[y]+=tag[x];
PQ().swap(q[x]),sz[y]+=sz[x],fa[x]=y,tag[x]=0;
return wq.push(y),in[y]=1,Remake(),1;
}
bool Med;
int main(){
cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
n=read(),m=read();
rep(i,1,n)W[i]=read(),fa[i]=i,sz[i]=1,tag[i]=W[i];
rep(i,1,m)A[i]=read(),B[i]=read(),S[i]=read(),rS[i]=S[i];
rep(i,1,m){
if(W[A[i]]+W[B[i]]>=S[i])Q.push(i);
else q[A[i]].push({(S[i]+1)/2,S[i],i}),q[B[i]].push({(S[i]+1)/2,S[i],i});
}
vector<ll>ans;
while(!Q.empty()){
ll i=Q.top();Q.pop();
if(Unite(A[i],B[i]))ans.push_back(i);
}
write(ans.size()),putchar('\n');
for(ll x:ans)write(x),putchar(' ');
putchar('\n');
cerr<<"\n"<<clock()*1.0/CLOCKS_PER_SEC*1000<<"ms\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 40420kb
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: 3ms
memory: 40704kb
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: 5ms
memory: 39184kb
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: 0
Accepted
time: 0ms
memory: 40324kb
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
output:
9 1 4 5 6 2 7 8 9 13
result:
ok 10 numbers
Test #5:
score: -100
Time Limit Exceeded
input:
100 200 49 21 70 93 66 51 36 59 56 62 10 4 46 73 22 48 89 17 72 60 29 64 19 56 32 54 55 0 77 86 34 35 56 17 55 2 98 80 73 71 64 37 61 22 89 96 99 28 0 35 56 45 74 81 30 3 66 96 28 16 29 43 60 61 60 95 83 5 73 1 28 47 73 44 8 4 91 34 100 23 4 93 44 87 72 5 13 88 100 52 56 61 100 80 14 30 59 97 51 43 ...