题目描述
输入
There are several test cases, and each case consists of two parts.
First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).
Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.
输出
For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.
样例输入
样例输出
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstdlib> 6 #include<ctime> 7 using namespace std; 8 const int N=100005; 9 int n;int fa[N]; 10 struct node{ 11 int x,dis; 12 node *l,*r; 13 int ldis(){return l?l->dis:0;} 14 int rdis(){return r?r->dis:0;} 15 }a[N]; 16 int gi() 17 { 18 int str=0;char ch=getchar(); 19 while(ch>'9' || ch<'0')ch=getchar(); 20 while(ch>='0' && ch<='9')str=str*10+ch-'0',ch=getchar(); 21 return str; 22 } 23 int find(int x){ 24 return x==fa[x]?x:fa[x]=find(fa[x]); 25 } 26 node *root[N],*pos=a; 27 void newnode(node *&p,int key) 28 { 29 p=pos++; 30 p->l=p->r=NULL; 31 p->dis=0;p->x=key; 32 } 33 node *merge(node *p,node *q) 34 { 35 if(!p || !q)return p?p:q; 36 if(p->x<q->x)swap(p,q); 37 p->r=merge(p->r,q); 38 if(p->ldis()<p->rdis())swap(p->l,p->r); 39 p->dis=p->rdis()+1; 40 return p; 41 } 42 void Delet(int t) 43 { 44 node *R=root[t]->r; 45 node *L=root[t]->l; 46 node *rt=root[t]; 47 rt->dis=0;rt->l=rt->r=NULL;rt->x>>=1; 48 root[t]=merge(R,L); 49 root[t]=merge(root[t],rt); 50 } 51 void work() 52 { 53 int x; 54 for(int i=1;i<=n;i++) 55 { 56 fa[i]=i;x=gi(); 57 newnode(root[i],x); 58 } 59 int m=gi(),y; 60 while(m--) 61 { 62 x=gi();y=gi(); 63 if(find(x)==find(y)){ 64 printf("-1\n"); 65 continue; 66 } 67 else{ 68 int xx=find(x),yy=find(y); 69 Delet(xx);Delet(yy); 70 fa[yy]=xx; 71 root[xx]=merge(root[xx],root[yy]); 72 printf("%d\n",root[xx]->x); 73 } 74 } 75 return ; 76 } 77 int main() 78 { 79 while(~scanf("%d",&n)){ 80 work(); 81 pos=a; 82 } 83 return 0; 84 }