HDU 2545 树上的战争 (并查集)

#include <stdio.h>


int ff[100001],ss[100001];
// int sex[100001];
// int qq[100001];


void ii(int n )
{
<span style="white-space:pre">	</span>for(int i =1 ;i <= n;i++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>ff[i] = i;
<span style="white-space:pre">	</span>}
}


int dd(int x)
{
<span style="white-space:pre">	</span>int nn =0;
<span style="white-space:pre">	</span>while(x != ff[x])
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>x = ff[x];
        nn++;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return nn;
// <span style="white-space:pre">	</span>if(x != ff[x])                  //递归写会有内存限制,,memory limit
// <span style="white-space:pre">	</span>{
// <span style="white-space:pre">		</span>ff[x] = dd(ff[x]);
// <span style="white-space:pre">		</span>nn++;
// <span style="white-space:pre">	</span>}
// <span style="white-space:pre">	</span>return nn;
}


void cc(int a,int b)
{
// <span style="white-space:pre">	</span>int ta = dd(a);
// <span style="white-space:pre">	</span>int tb = dd(b);
     ff[b] = a;




}


int main()
{
    int t,i,k,j,h;
<span style="white-space:pre">	</span>int a,b;
    int N,M,X,Y;
<span style="white-space:pre">	</span>while(scanf("%d%d",&N,&M),N&&M)
<span style="white-space:pre">	</span>{
          ii(N);
        for(i = 1;i < N;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>scanf("%d%d",&a,&b);
<span style="white-space:pre">			</span>cc(a,b);


<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>while(M--)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>scanf("%d%d",&a,&b);
<span style="white-space:pre">			</span>X = dd(a);
<span style="white-space:pre">			</span>Y = dd(b);
<span style="white-space:pre">			</span>if(X > Y)
<span style="white-space:pre">				</span>printf("pfz");
<span style="white-space:pre">			</span>else
<span style="white-space:pre">				</span>printf("lxh");
<span style="white-space:pre">			</span>printf("\n");
<span style="white-space:pre">		</span>}


<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return 0;
}

分析:计算两个人的节点移动到根的步数,最后比较哪个走的多少,多的就输,少的就胜。

     要注意路径查找dd函数不要用递归写

树上战争

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 661    Accepted Submission(s): 360


Problem Description
给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜
 

Input
输入包含多组数据
每组第一行包含两个数N,M(N,M<=100000),N表示树的节点数,M表示询问数,N=M=0表示输入结束。节点的编号为1到N。
接下来N-1行,每行2个整数A,B(1<=A,B<=N),表示编号为A的节点是编号为B的节点的父亲
接下来M行,每行有2个数,表示lxh和pfz的初始位置的编号X,Y(1<=X,Y<=N,X!=Y),lxh总是先移动

 

Output
对于每次询问,输出一行,输出获胜者的名字
 

Sample Input
  
  
2 1 1 2 1 2 5 2 1 2 1 3 3 4 3 5 4 2 4 5 0 0
 

Sample Output
  
  
lxh pfz lxh 提示: 本题输入、输出都很多,请使用scanf和printf代替cin、cout。
 

Source

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值