题意:

题目给你一组单向边,当遇到输入0 0就证明这是一组边,当遇到-1 -1就要停止程序。让你判断这是不是一棵树

题解:

题目很简单,但是程序要考虑的很多

1、因为是一颗树,所以肯定不能出现环,这个可以用并查集来判断

2、边数量+1==节点数量

3、每一个点的入度不能大于1(例如边a->b,这个b点的入度就要加1)

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<queue>
6 #include<map>
7 #include<vector>
8 #include<math.h>
9 #define mem(a,x) memset(a,x,sizeof(a))
10 using namespace std;
11 typedef long long ll;
12 const int maxn=120000+10;
13 const int mod=26;
14 const int INF=0x3f3f3f3f;
15 const int Times = 10;
16 const int N = 5500;
17 int v[maxn],vis[maxn],sum[maxn];
18 int flag=0;
19 int finds(int x)
20 {
21 if(x!=v[x])
22 {
23 int y=finds(v[x]);
24 return v[x]=y;
25 }
26 return x;
27 }
28 void join(int a,int b)
29 {
30 int fa=finds(a),fb=finds(b);
31 if(fa!=fb)
32 v[fa]=fb;
33 else
34 flag=1;
35 }
36 int main()
37 {
38 int a,b,p=1,edge;
39 while(scanf("%d%d",&a,&b))
40 {
41 flag=0;
42 edge=0;
43 for(int i=1; i<maxn; i++)
44 {
45 v[i]=i;
46 }
47 memset(vis,0,sizeof(vis));
48 memset(sum,0,sizeof(sum));
49 if(a<0&&b<0)
50 break;
51 if(a==0&&b==0)
52 {
53 printf("Case %d is a tree.\n",p);
54 p++;
55 continue;
56 }
57 vis[a]=vis[b]=1;
58 join(a,b);
59 sum[b]++;
60 edge++;
61 while(scanf("%d%d",&a,&b))
62 {
63 if(a==0&&b==0)
64 break;
65 join(a,b);
66 vis[a]=vis[b]=1;
67 sum[b]++;
68 edge++;
69 }
70 if(flag==1)
71 {
72 printf("Case %d is not a tree.\n",p++);
73 }
74 else
75 {
76 int flagg=0,node=0;
77 for(int i=1; i<maxn; i++)
78 {
79 if(vis[i]==1)
80 node++;
81 if(sum[i]>1)
82 flagg=1;
83 }
84 if(node==(edge+1)&&flagg==0)
85 {
86 printf("Case %d is a tree.\n",p++);
87 }
88 else
89 {
90 printf("Case %d is not a tree.\n",p++);
91 }
92 }
93 }
94 }

Is It A Tree? POJ - 1308的更多相关文章

  1. Is It A Tree? POJ - 1308(并查集判树)

    Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...

  2. POJ 1308&&HDU 1272 并查集判断图

      HDU 1272 I - 小希的迷宫 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  3. 并查集判树 poj 1308

    例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...

  4. 杭电 1272 POJ 1308 小希的迷宫

    这道题是我学了并查集过后做的第三个题,教我们的学姐说这是并查集的基础题,所以有必要牢牢掌握. 下面就我做这道题的经验,给大家一些建议吧!当然,我的建议不是最好的,还请各位大神指出我的错误来,我也好改正 ...

  5. Apple Tree POJ - 2486

    Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这 ...

  6. E - Apple Tree POJ - 2486

    E - Apple Tree POJ - 2486 Wshxzt is a lovely girl. She likes apple very much. One day HX takes her t ...

  7. POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫

    POJ题目网址:http://poj.org/problem?id=1308 HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272 并查集的运用 ...

  8. HDU 1325,POJ 1308 Is It A Tree

    HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...

  9. hdu 1325 && 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 ...

随机推荐

  1. 十三:SQL注入之MYSQL注入

    MYSQL注入中首先要明确当前注入点权限,高权限注入时有更多的攻击手法,有的能直接进行getshell操作,其中也会遇到很多的阻碍,相关防御手法也要明确,所谓知己知彼,百战不殆.作为安全开发工作者,攻 ...

  2. 【Java】Java注释 - 单行、块、文档注释

    简单记录,Java 核心技术卷I 基础知识(原书第10 版) 注释 我们在编写程序时,经常需要添加一些注释,用来描述某段代码的作用,提高Java源程序代码的可读性,使得Java程序条理清晰. 写代码的 ...

  3. 【Linux】linux的所有文件分类解析

    今天看书的时候,无意间看到/dev/文件夹,以前没注意,今天去看了下发现,很多文件的开头文件属性都是一些不怎么见到的 常见的是   -     这个是代表文件,可以vim编辑的 d     这个是代表 ...

  4. DG主备切换遇到not allwod或者RESOLVABLE GAP解决办法

    今天做switchover,环境是11.2.0.3+OEL5.7,开始时主备库状态都是正常的,符合直接切换条件: 主库: SQL> select open_mode,database_role, ...

  5. 敏感信息泄露 - Pikachu

    概述: 由于后台人员的疏忽或者不当的设计,导致不应该被前端用户看到的数据被轻易的访问到. 比如:---通过访问url下的目录,可以直接列出目录下的文件列表;---输入错误的url参数后报错信息里面包含 ...

  6. ctfshow—pwn10

    格式化字符串漏洞 具体什么是格式化字符串请大家参考如下文章 https://wiki.x10sec.org/pwn/fmtstr/fmtstr_intro/ printf函数格式化输出符号及详细说明 ...

  7. 【译】Async/Await(三)——Aysnc/Await模式

    原文标题:Async/Await 原文链接:https://os.phil-opp.com/async-await/#multitasking 公众号: Rust 碎碎念 翻译 by: Praying ...

  8. Entity与Entity之间的相互转化

    一.两个实体类的属性名称对应之间的转化 1.两个实体类 public class Entity1 { private Integer id; private String name; private ...

  9. django url别名和反向解析 命名空间

    url别名和反向解析 我们平时写的url名字都是死的,如果项目过大,需要项目中某个文件名改动一下,那么改动起来就不是一般的麻烦了,所以我们就在定义的时候给url起一个别名,以后不管哪个文件中运用都是用 ...

  10. YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. data = yaml.load(file_data)

    YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsa ...