• HDU 1325 Is It A Tree?


    Portal: http://acm.hdu.edu.cn/showproblem.php?pid=1325

    1272的原版?

    依然并查集+map,ID分配正是好用

    话说为什么hdu上rank和_是保留字?

    与1272相比,由无向树变成了有向树,所以在1272的基础上维护结构时去掉左右合并rank优化,再保证每个节点只有一个父节点,如果不行直接切出去进行下个循环。

     1 #include<iostream>
     2 #include<map>
     3 using namespace std;
     4 #define FOR(i,j,k) for(int i=j;i<=k;i++)
     5 #define FORD(i,j,k) for(int i=j;i>=k;i--)
     6 #define LL long long
     7 #define maxn 20010
     8 int father[maxn];
     9 int x,y,k,num;
    10 bool flag;
    11 map<int,int> ID;
    12 int setfind(int xx)
    13 {
    14     int fa=father[xx];
    15     if(fa==xx) return fa;
    16     else return father[xx]=setfind(fa);
    17 }
    18 bool setunion(int xx,int yy)
    19 {
    20     int X=setfind(xx);
    21     int Y=setfind(yy);
    22     if(X!=xx) return true;
    23     if(X==Y) return true;
    24     father[X]=Y;
    25     return false;
    26 }
    27 bool issame(int xx,int yy)
    28 {
    29     return setfind(xx)==setfind(yy);
    30 }
    31 bool setcheck()
    32 {
    33     FOR(i,1,k-1)
    34     if(!issame(i,i+1)) return true;
    35     return false;
    36 }
    37 int main()
    38 {
    39 while(true)
    40 {
    41     num++;
    42     k=0;
    43     flag=true;
    44     for(cin>>x>>y;x>0&&y>0;cin>>x>>y)
    45     {
    46         if(!ID.count(x)) {k++; ID[x]=k; father[k]=k;}
    47         if(!ID.count(y)) {k++; ID[y]=k; father[k]=k;}
    48         if(setunion(ID[y],ID[x])) {for(cin>>x>>y;x>0&&y>0;cin>>x>>y);flag=false;break;}
    49     }
    50     if(x<0) return 0; 
    51     cout<<"Case "<<num<<" is";
    52     if (setcheck()) flag=false;
    53     if(!flag) cout<<" not";
    54     cout<<" a tree."<<endl;
    55     ID.clear();
    56 }
    57 return 0;
    58 }
    神奇的代码

    值得注意的是,本题输入的条件是“Node numbers will always be greater than zero. ” 即【输入为负数就结束程序】

    什么鬼表述啊!!!谁看的懂啊!!!语文没学好吗? 

  • 相关阅读:
    Python生成器表达式
    Python列表解析
    Python迭代器(Iterator)
    Python set 集合
    python eval 函数妙用
    Python字典 (dict)
    Python序列之元组 (tuple)
    Python序列之列表 (list)
    递归和反射
    常用标准库
  • 原文地址:https://www.cnblogs.com/mukoiaoi/p/5635225.html
Copyright © 2020-2023  润新知