zoukankan      html  css  js  c++  java
  • 树判断——pku1308

    空树也是树,森林不是树……还有注意ca++,大囧……
    View Code
    #include<stdio.h>

    int f[109];
    int jin[109];

    int find(int pos)
    {
    if(f[pos]==-1)return pos;
    return f[pos]=find(f[pos]);
    }

    int un(int a,int b)
    {
    int fa=find(a),fb=find(b);
    if(fa==fb)return 0;
    f[fa]
    =fb;return 1;
    }

    int main()
    {
    int i,j,ca=0;
    while(1)
    {
    ca
    ++;
    for(i=1;i<=100;i++)
    {
    f[i]
    =-1;
    jin[i]
    =0;
    }
    int t=0,add=0;
    while(scanf("%d%d",&i,&j),i||j)
    {
    t
    ++;
    if((i==-1)&&(j==-1))
    {
    return 0;
    }

    if(un(i,j))
    {
    add
    ++;
    }
    jin[j]
    ++;
    }

    if(t==0&&i==0&&j==0)
    {
    printf(
    "Case %d is a tree.\n",ca);
    continue;
    }

    for(i=1;i<=100;i++)
    {
    if(jin[i]>=2)
    break;
    }

    int a=0,set,temp;
    for(j=1;j<=100;j++)
    {
    temp
    =find(j);
    if(a==0&&f[j]!=-1)
    {
    set=temp;
    a
    =1;
    }

    if(a==1&&f[j]!=-1)
    {
    if(set!=temp)
    break;
    }
    }
    if(add==t&&i==101&&j==101)
    printf(
    "Case %d is a tree.\n",ca);
    else
    printf(
    "Case %d is not a tree.\n",ca);
    }
    }
  • 相关阅读:
    bom案例2-弹出层
    bom案例1-div拖拽
    bom-scroll
    bom-client
    bom-offset
    9. 阻塞队列
    8. 读写锁
    7. CountDownLatch、CyclicBarrier、Semaphore
    6. Callable
    5. 集合不安全
  • 原文地址:https://www.cnblogs.com/huhuuu/p/1961773.html
Copyright © 2011-2022 走看看