• hdu 2545 树上战争(并查集)


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

    树上战争

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


    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。
     
     
    题目大意:先占据节点的获胜。一提到节点,很容易想到用并查集来构图。但是一开始用并查集,在用最普通的来分别比较是否是节点,超时了0.0
    所以,想了一个很简单的办法,就是来比较两点到达节点的时间分别是多少。来判断谁能获胜,哇哈哈~
     
    详见代码。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 int father[100010],n,m;
     6 
     7 int find(int a)
     8 {
     9     int flag=0;
    10     while(a!=father[a])
    11     {
    12         a=father[a];
    13         flag++;
    14     }
    15     return flag;
    16 }
    17 
    18 int main ()
    19 {
    20     while (~scanf("%d%d",&n,&m))
    21     {
    22         for (int i=1; i<=n; i++)
    23             father[i]=i;
    24         if (n==0&&m==0)
    25             break;
    26         for (int i=1; i<=n-1; i++)
    27         {
    28             int a,b;
    29             scanf("%d%d",&a,&b);
    30             father[b]=a;
    31         }
    32         for (int i=1; i<=m; i++)
    33         {
    34             int startl,startp;
    35             scanf("%d%d",&startl,&startp);
    36             int t1=find(startl);
    37             int t2=find(startp);
    38             if (t1>t2)
    39             printf ("pfz
    ");
    40             else
    41             printf ("lxh
    ");
    42         }
    43     }
    44     return 0;
    45 }
     
  • 相关阅读:
    [Algorithm] DP Min Number of Jumps
    [React] Error boundary
    [Recoil] Optimising API Calls
    [Recoil] Intermediate Selectors
    [Typescript] Tips: Create autocomplete helper which allows for arbitrary values
    一文搞懂springboot启动原理
    [springboot]springboot启动流程[通俗易懂]
    springboot启动原理总结_Springboot启动流程
    springboot启动流程概述_简述app启动的主要流程
    SpringBoot启动原理
  • 原文地址:https://www.cnblogs.com/qq-star/p/4035486.html
Copyright © 2020-2023  润新知