hdu 2545 树上战争(并查集)

来源:互联网 发布:excel2007办公软件 编辑:程序博客网 时间:2024/05/22 05:31

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545

题       意:比较两个点与根结点的距离的大小

思       路:先将输入的点用并查集的方式处理好,再找两个点到根结点的距离,谁离的越近谁就赢,距离相等则先走的获胜。

代码如下:

#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <queue>#include <algorithm>typedef long long LL;int father[100002];int findx( int x ){    int r = x, l = 0;    while ( r != father[r] )        {r = father[r];l++;}    return l;}int main(){    int n, m;    while( scanf ( "%d %d", &n, &m ) != EOF )    {        if( n == 0 && m == 0 ) break;        for( int i = 0 ; i <= n; i ++ )            father[i]=i;        for( int i = 1; i < n; i ++ )        {            int x,y;            scanf( "%d %d", &x, &y );            father[y] = x;        }        for( int i = 0; i < m; i ++ )        {            int x,y;            scanf ( "%d %d", &x, &y );            int fx = findx(x);            int fy = findx(y);            if( fx <= fy )            {                printf("lxh\n");            }            else printf("pfz\n");        }    }    return 0;}


 

0 0