POJ1308 Is It A Tree? (easy but...)

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.

题目大意:给定一些点和边的关系,然后判断是否是一棵树(注意:森林是不可以的!!!)

思路:读入边的信息,然后将两个点合并,将后面点的fa给前面的点, 如两点已经在同一集合就不是树,最后判断是否是森林就可以了。。。

code:

#include<iostream>

#include<cstdio>

#include<cstring>

using namespace std;

int fa[100001]={0};

int visit[100001]={false},visitt[100001]={false};

int rool(int x)

{

if (fa[x]!=x) fa[x]=rool(fa[x]);

return fa[x];

}

int main()

{

int a,b,r1,r2,ci=0,i,t,maxn;

bool f=false;

while (cin>>a>>b)

{

++ci;

f=false;

if (a==-1&&b==-1) break;

for (i=0;i<=100000;++i)

fa[i]=i;

memset(visit,false,sizeof(visit));

memset(visitt,false,sizeof(visitt));

t=maxn=0;

while (a!=0||b!=0)

{

if (a>maxn) maxn=a;

if (b>maxn) maxn=b;

visit[a]=visit[b]=true;

r1=rool(a);

r2=rool(b);

if (r1==r2)

f=true;

fa[r2]=r1;

cin>>a>>b;

}

if (!f)

{

for (i=1;i<=maxn;++i)

if (visit[i])

{

r1=rool(i);

if (!visitt[r1])

{

visitt[r1]=true;

++t;

}

}

if (t>1) f=true;

}

cout<<"Case "<<ci<<" is ";

if (f) cout<<"not ";

cout<<"a tree."<<endl;

}

}

时间: 2024-08-29 08:05:52

POJ1308 Is It A Tree? (easy but...)的相关文章

hdu5593--ZYB&#39;s Tree(树形dp)

问题描述 ZYB有一颗N个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采用如下方式进行读入输出: 读入:读入两个数A,B,令fai??为节点i的父亲,fa?1??=0;fa?i??=(A∗i+B)%(i−1)+1,i∈[2,N] . 输出:输出时只需输出N个点的答案的xor和即可. 输入描述 第一行一个整数TT表示数据组数. 接下来每组数据: 一行四个正整数N,K,A,

HDU 1325 Is It A Tree? (POJ 1308)

并查集问题... 这题以前做过-- 以前做过-- 做过-- 过-- 不过重做时候被吭得异常之爽-- 在判断 vis[i]的时候.我记得标准C++是非0 即为真. 而我用C++ 提交的时候 if(vis[i]) 去直接给我WA了. 用G++ 就AC了...然后改成if(vis[i]==1) 交C++ 就AC了. 特瞄的我每次初始化都把 vis[i] 都赋值为 0 了..都能出这种错? 求路过大神明示我的错误. 题意是判断是否是一棵树. 不能存在森林,用并查集合并,每个点的入度不能超过1. 比如 1

HDU 4925 Apple Tree(模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种是种苹果树操作,第二种是施肥操作,种苹果树操作可以使得该块地 长出一个苹果,施肥操作可以使得与这块土地相邻的土地的苹果产量变为原来的两倍,问可以得到的最多的苹果数量是多少? 例如一个4*4的土地,用1表示在该土地上做第一种操作,0表示在该土地上做第二种操作,可以得到最多苹果的操作如下: 0 1 0

【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】

[226-Invert Binary Tree(反转二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 题目大意 将一棵二叉树进行翻转. 解题思路 对每一个结点,将它的左右子树进行交换,再对它的左右子结点进行同样的操作. 代码实现 树结点类 pub

codeforces 161D - Distance in Tree(树形dp)

题目大意: 求出树上距离为k的点对有多少个. 思路分析: dp[i][j] 表示 i 的子树中和 i 的距离为 j 的点数有多少个.注意dp[i] [0] 永远是1的. 然后在处理完一颗子树后,就把自身的dp 更新. 更新之前更新答案. 如果这颗子树到 i 有 x 个距离为j的.那么答案就要加上 dp[i] [ k-j-1] * x; #include <iostream> #include <cstdio> #include <cstring> #include &l

hdu 4670 Cube number on a tree(点分治)

Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1628    Accepted Submission(s): 382 Problem Description The country Tom living in is famous for traveling. Every year, man

【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not:

POJ 2367:Genealogical tree(拓扑排序)

Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They ga

hdu4925Apple Tree(找规律)

题目:hdu4925Apple Tree(找规律) 题目大意:给出N* M 的矩阵,然后每个格子要不种苹果,要不施肥:在(X,Y)施肥后,它上下左右的苹果树的产量会翻倍.种了苹果数,产量为1.求这样的N * M个矩阵能得到的最大的苹果数. 解题思路: 对于2 * 2的矩阵: X代表施肥 , 有数字代表种树  X 2  这样是最好的.各自上的数字代表这个格子被施肥几次.那么2 * 2的最多的苹果数 2 ^2 + 2^2 = 8; 2 X 3 * 3 : X  3 X 3  X   3 X  3