这题我都不知道要怎么搞了.感觉在乱报错误,如果我把所有结果都输出-1报的错误是超时...如果不加入检测报的错误是运行时错误,如果加入的是范围检测就报分割错误...
那么多错误= =,在我自己电脑上测试一点问题都没有...郁闷
#include<iostream>
#include<queue>
using namespace std;
int father[100001];
int fine(int x)//压缩,只用压缩时间320
{
int r=x,j;
while(father[r]>0)
r=father[r];//r是根
while(r!=x)
{
j=father[x];
father[x]=r;
x=j;
}
return r;
}
void weightmerge(int x,int y)//只用折叠时间为280//折叠压缩一起用260
{
int temp;
int ix,iy;
ix=fine(x);
iy=fine(y);
temp=father[ix]+father[iy];
if(ix!=iy)
if(father[ix]>father[iy])//ix的结点少,以结点多的为父节点
{
father[iy]=temp;
father[ix]=iy;//iy为父指针
}
else
{
father[ix]=temp;
father[iy]=ix;//ix为父指针
}
}
int main()
{
int num;
while(cin>>num)
{
for(int i=1;i<=num;i++)
father[i]=-1;
int k=1;
priority_queue<int> **p;
p=new priority_queue<int> *[num+1];
priority_queue<int> *temp;
while(num--)
{
temp=new priority_queue<int>;
int tnum;
cin>>tnum;
temp->push(tnum);
p[k++]=temp;
}
int order;
cin>>order;
while(order--)
{
int a,b;
cin>>a>>b;
if(fine(a)!=fine(b))
{
int pnum;
if(!p[a]->empty()&&!p[b]->empty())//合法性检测 !p[a]->empty()&&!p[b]->empty()或者a<k&&b<k
{
weightmerge(a,b);
if(p[a]->size()<p[b]->size())//如果是范围检测的话这里就会分割错误,如果是空检测的话就超时= =,如果不检测就运行时错误...
{
temp=p[b];
p[b]=p[a];
p[a]=temp;
}
pnum=p[a]->top();
p[a]->pop();
pnum/=2;
p[a]->push(pnum);
pnum=p[b]->top();
p[b]->pop();
pnum/=2;
p[a]->push(pnum);
while(!p[b]->empty())
{
p[a]->push(p[b]->top());
p[b]->pop();
}
p[b]=p[a];
cout<<p[a]->top()<<endl;
}
else
cout<<-1<<endl;
}
else
cout<<-1<<endl;
}
delete []p;
}
return 0;
}