洛谷P1456 Monkey King 【左偏树】

洛谷P1456 Monkey King

题目描述

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.

一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.

输入输出格式

输入格式:

 

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 strength value of the strongest monkey among all of its friends after the duel.

 

输入样例#1: 
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
输出样例#1:
8
5
5
-1
10

说明

题目可能有多组数据

题解:

今儿个新学了个算法左偏树。

此题为左偏树模板题,将一个猴子兄弟团看做一棵左偏树。

打完架后 w 值变为一半,先要把节点从中删去,然后当做新的左偏树再合并进来。

这里还需要用到并查集,用来记录两个节点是否在同一棵左偏树中。

上代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=100005;
 4 int n,fa[N];
 5 struct node{
 6     int w,l,r,d;
 7     void cle()
 8     {
 9         l=r=d=0;
10     }
11 }a[N];
12 inline int read()
13 {
14     int x=0,f=1; char ch=getchar();
15     while (!isdigit(ch)) f=(ch=='-')?-f:f,ch=getchar();
16      while (isdigit(ch)) x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
17     return x*f;
18 }
19 inline int getf(int x)
20 {
21     return fa[x]==x?x:fa[x]=getf(fa[x]);
22 }
23 int merge(int x,int y)
24 {
25     if (!x) return y; if (!y) return x;
26     if (a[x].w<a[y].w) swap(a[x],a[y]);
27     a[x].r=merge(a[x].r,y);
28     int l,r; l=a[x].l,r=a[x].r,fa[r]=x;
29     if (a[l].d<a[r].d) swap(a[x].l,a[x].r);
30     a[x].d=(a[x].r)?a[a[x].r].d+1:0;
31     return x;
32 }
33 int del(int x)
34 {
35     int l,r; l=a[x].l,r=a[x].r;
36     a[x].cle(),fa[l]=l,fa[r]=r;  
37     return merge(l,r);
38 }
39 int main()
40 {
41     while (scanf("%d",&n)!=EOF)
42     {
43         for (int i=1; i<=n; i++)
44           a[i].cle(),a[i].w=read(),fa[i]=i;
45         for (int q=read(); q; q--)
46         {
47             int x=read(),y=read();
48             x=getf(x),y=getf(y);
49             if (x==y) {
50                 cout<<-1<<endl; continue;
51             }
52             a[x].w>>=1,a[y].w>>=1;
53             int l,r; l=del(x),r=del(y);
54             l=merge(l,x),r=merge(r,y),l=merge(l,r);
55             cout<<a[l].w<<endl;
56         }
57     }
58     return 0;
59 }
View Code

加油加油加油!!! fighting fighting fighting !!!

原文地址:https://www.cnblogs.com/Frank-King/p/9236086.html