poj 1308 Is It A Tree?

来源:互联网 发布:苹果电脑web前端软件 编辑:程序博客网 时间:2024/06/06 03:10

http://poj.org/problem?id=1308

 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 directed edges between nodes satisfying the following properties. 

There is exactly one node, called the root, to which no directed edges point. 
Every node except the root has exactly one edge pointing to it. 
There is a unique sequence of directed edges from the root to each node. 
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not. 

In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.

Input

The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.

Output

For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).

Sample Input

6 8  5 3  5 2  6 45 6  0 08 1  7 3  6 2  8 9  7 57 4  7 8  7 6  0 03 8  6 8  6 45 3  5 6  5 2  0 0-1 -1

Sample Output

Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.

和小希的迷宫很类似的一道题


判断树有四点:

1:判断是否只有一个根;

2:判断节点的总数是否比边的总数大一;

3:判断是否有回路;

4:判断每个节点的入度是否<=1;


之前自己按着这四点要求写了代码,发现好麻烦,最后看了别人的博客,发现根本不用那么麻烦,只要判断节点的总数是否比边的总数大一也就判断出了这个集合是否只有一个根,每个节点的入度是不是<=1;


解题思路:

输入两个数,判断是否在一个集合中,如果不在一个集合中,就把它们放到一个集合中;如果在一个集合中就说明这个集合有回路,返回false;还有计算总节点数和总边数


初次接触树的问题,花的时间可不少呢,不过还好,总算弄明白了害羞害羞害羞


#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 100010#define INF 0xfffffff#define PI acos (-1.0)#define EPS 1e-8int f[N], r[N], flag, flag1, flag2, flag3, flag4, vis[N];int Find (int x);//查找根节点void Init ();//初始化void join (int a, int b);//判断两个节点之间是否有回路int main (){    int a, b, k = 1;    while (scanf ("%d%d", &a, &b), a != -1 && b != -1)    {        flag = 1, flag1 = 0, flag2 = 0;        if (!a && !b)        {            cout << "Case " << k++ << " is a tree." << endl;            continue;        }        Init ();        while (a || b)        {            if (!vis[a]) flag1++, vis[a] = 1;            if (!vis[b]) flag1++, vis[b] = 1;            flag2++;            join (a, b);            scanf ("%d%d", &a, &b);        }        if (flag && flag1 == flag2 + 1)//没有回路并且节点数=边数+1            cout << "Case " << k++ << " is a tree." << endl;        else            cout << "Case " << k++ << " is not a tree." << endl;    }}int Find (int x){    if (x != f[x]) f[x] = Find (f[x]);    return f[x];}void join (int a, int b){    int x = Find (a), y = Find (b);    if (x != y) f[x] = y;//根节点合并    else flag = 0;}void Init (){    memset (vis, 0, sizeof (vis));    for (int i=1; i<=N; i++)        f[i] = i;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果6s发烫严重怎么办 手机型号不适配全军出击怎么办? 苹果5版本过低怎么办 在泰国买到假货怎么办 在泰国旅游遇到危险该怎么办? 拾到小米手环怎么办 苹果充电打游戏触点怎么办 衣服上金属锈了怎么办 光之子门锁住了怎么办 挖到金矿石了该怎么办 邻居是精神病扔石头怎么办 汽车保养手册丢了怎么办 五号电池没电了怎么办 电脑打印时显示脱机怎么办 博新计划两年后怎么办 门帘粘扣老是掉怎么办 魔术粘贴带双面背胶不粘了怎么办 斜挎包包磨衣服怎么办 电脑ip被别人知道了怎么办 西乐葆不止痛了怎么办 家里鸽子多捣蛋的不行怎么办 联璧倒闭了理财钱怎么办 众筹平台雷了怎么办 App理财无法提现怎么办 喝咖啡喝的心慌怎么办 睾丸被用力捏破流血怎么办 足球大师球队声望过高怎么办 fm17买了好多球员怎么办 fm2018电脑适应了战术怎么办 家纺摆放跑色怎么办 买的t恤太小了怎么办? 汽车全包围脚垫翘边怎么办 针织衣服线跑了怎么办 老板跟老板娘吵架我应该怎么办 偷看老板娘洗澡被发现怎么办? 幽浮2人挂了怎么办 蜻蜓fm业务扣费怎么办 退出键退不到主页面怎么办 3d纯英文版怎么办 3d单位设置错了怎么办 翼龙贷款不还款怎么办