HDU1325 Is It A Tree? 并查集

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

这题与HDU1272 小希的迷宫 (并查集) 非常像,不过细细看,还是有一点区别的。就是这题的路径是单向的,每次只能由起点指向终点,在连接之前终点必须是根节点。

注意的问题:

1、不能成环,即每次输入的两个数的根节点不能相同;

2、最终根节点数目为一

3、注意当只输入“0 0” 时要输出”Case %d is a tree.“

4、路径是单向的,即每次只能由起点指向终点,在连接之前终点必须是根节点。

<span style="font-size:18px;"><span style="font-size:14px;">#include<stdio.h>
#define num 101     //我试过20都能通过
int fa[num];
int find(int u)
{
    return fa[u]==u?u:fa[u]=find(fa[u]);
}
int main()
{
    int u,v,x,y,i,k=0;
    bool bo;
    scanf("%d%d",&u,&v);
      while (u>=0)
      {
          if (u==0) printf("Case %d is a tree.\n", ++k);
          else
          {
              bo=true;
              for (i=1;i<=num;i++) fa[i]=i;
              while (u)
              {
                  if (fa[v]!=v) bo=false;           //与HDU1272 最大的差别在于多了这个判断
                  else
                  {
                     x=find(u);y=find(v);
                     if (x==y) bo=false;
                     else fa[y]=x;
                  }
                  scanf("%d%d",&u,&v);
              }
            if (bo)
              for (i=1;i<=num;i++)
                if (find(i)!=i&&find(i)!=x) bo=false;
              if (bo) printf("Case %d is a tree.\n", ++k);
                else printf("Case %d is not a tree.\n",++k);
          }
        scanf("%d%d",&u,&v);
      }
   return 0;
}</span>
</span>

同类题:http://blog.csdn.net/yzj577/article/category/2432227

HDU1325 Is It A Tree? 并查集

时间: 2024-10-12 21:44:31

HDU1325 Is It A Tree? 并查集的相关文章

HDU 1325 Is It A Tree? 并查集

判断是否为树 森林不是树 空树也是树 成环不是树 数据: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0 1 2 2 3 4 5 0 0 2 5 0 0 ans: no no yes #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype

HDU 5606 tree 并查集

tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans?i??=size[findset(i)],size表示每个并查集根的size Ans_i=size[findset(i)],sizeAns?i??=size[findset(i)],size表示每个并查集根的sizesize. #include<cstdio> #include<cstring> #include<algorithm>

HDU 1325 POJ 1308 Is It A Tree? (并查集)

这道题就是裸并查集,关键在于对不是树几种的判断 1. 空树是树 2. 森林不是树 3. 无环 或者从入度来看:1,无环:2,除了根,所有的入度为1,根入度为0:3,这个结构只有一个根,不然是森林了. 这道题本来暑假做的POJ 1308 但是HDU没有过.在于空树没有考虑. 用并查集判断有多少个森林注意编号是随机的,不是次序.... /* input: 0 0 1 1 0 0 1 2 1 2 0 0 1 2 2 3 4 5 0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

hdu5606 tree (并查集)

tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 823    Accepted Submission(s): 394 Problem Description There is a tree(the tree is a connected graph which contains n points and n−1 edges),t

swust oj 856--Huge Tree(并查集)

题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 There are N trees in a forest. At first, each tree contains only one node as its root. And each node is marked with a number. You're asked to do the following two

[POJ 1308]Is It A Tree?(并查集判断图是否为一棵有根树)

Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the root, t

CF109 C. Lucky Tree 并查集

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya encountered a tre

Is It A Tree?(并查集)(dfs也可以解决)

Is It A Tree? Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by direct

树上统计treecnt(dsu on tree 并查集 正难则反)

题目链接 \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\(L\sim R\)号点两两连通,最少需要选择的边的数量. 求\[\sum_{l=1}^n\sum_{r=l}^nTree[l,r]\] \(Solution\) 枚举每条边,计算它的贡献. 那么我们要判断有多少连续区间的点跨过这条边,并不好算,反过来去求在这条边的两侧分别有多少个连续区间. 那么显然有\(O(n^2)\)的做法,即对每条边DFS它的两侧,枚