题意:

题目给你一组单向边,当遇到输入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. python中re模块的使用(正则表达式)

    一.什么是正则表达式? 正则表达式,又称规则表达式,通常被用来检索.替换那些符合某个模式(规则)的文本. 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合, ...

  2. python函数2-函数参数

    rgb法则:

  3. /usr/bin/ld: cannot find -lc

    yum install glibc-static [root@test chkrootkit-0.50]# make sensecc -static -o strings-static strings ...

  4. 【Linux】tcpdump

    tcpdump介绍 tcpdump 是一个运行在命令行下的抓包工具.它允许用户拦截和显示发送或收到过网络连接到该计算机的TCP/IP和其他数据包.tcpdump 适用于 大多数的类Unix系统操作系统 ...

  5. .NET 云原生架构师训练营(模块二 基础巩固 Scrum 团队)--学习笔记

    2.7.3 Scrum 团队 理想的环境 团队章程 如何组建 Scrum 团队 产品待办事项列表 用户故事 敏捷开发流程 理想的环境 5-9人 100% 跨职能 在一起 自组织 自组织 目标 授权 沟 ...

  6. CTFHub - Web(四)

    最近又是每天忙到裂开,,,淦 xss: 反射型: 1.第一个输入框与下面Hello后的内容相同,猜测可以通过该输入,改变页面内容. 测试语句: <script>alert(1)</s ...

  7. 这难道不是.NET5 的bug? 在线求锤?

    hello,最近在对一个使用.NET5项目的认证授权系统进行重构,对.NET 5的授权中间件的源码有些看法. 也希望同学们能帮我理解. 一个朴素的需求 这是一个api项目,默认所有的api都需要授权, ...

  8. C# 请求被中止: 未能创建 SSL/TLS 安全通道。 设置SecurityProtocol无效

    今天为了获取一张图片,用了一段代码: ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateV ...

  9. Java集合List-差集、并集、交集

    Java集合List的差集.并集.交集 转载于:https://www.cnblogs.com/qlqwjy/p/9812919.html 一.List的差集 @Test public void te ...

  10. scrapy框架基于管道的持久化存储

    scrapy框架的使用 基于管道的持久化存储的编码流程 在爬虫文件中数据解析 将解析到的数据封装到一个叫做Item类型的对象 将item类型的对象提交给管道 管道负责调用process_item的方法 ...