Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6961    Accepted Submission(s): 1619

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

这题如果用并查集做的话,最后判断的时候还是要看一个结点的入度是否为大于1(纯并查集的情况下),搜了几份代码,有些是去判断是否有环的,这些都不如直接用树的充分条件去判断:一个节点入度为0,其余节点入读为1,V个点只有V-1条边,题目坑你的地方大概有两点:没节点也是树,结束时两个数是负数而不是两个-1

 #include <cstdio>
#include <cstring>
#define SIZE 2013
using namespace std;
int father[SIZE];
bool exit[SIZE];
int find(int f)
{
return father[f] = f == father[f] ? f : find(father[f]);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int n, m, T = , max = , nv = , ne = ;
bool flag = true;
memset(father, , sizeof(father));
memset(exit, false, sizeof(exit));
while(scanf("%d%d", &n, &m) != EOF)
{
if(n < && m < ) break;
else if(n+m == )
{
if(ne == && nv == )
{
printf("Case %d is a tree.\n", ++T);
continue;
}
if(ne == nv-)
{
int cnt = , zero = ;
for(int i = ; i <= max; ++i)
if(exit[i])
{
if(!father[i]) zero++;
else if(father[i] == ) cnt++;
}
if(cnt == ne && zero == )
printf("Case %d is a tree.\n", ++T);
else
printf("Case %d is not a tree.\n", ++T);
}
else printf("Case %d is not a tree.\n", ++T); flag = true;
memset(exit, false, sizeof(exit));
memset(father, , sizeof(father));
ne = nv = ;
max = ;
}
else
{
max = max > n ? (max > m ? max : m) : (n > m ? n : m);
if(!exit[n])
{
exit[n] = true;
nv++;
}
if(!exit[m])
{
exit[m] = true;
nv++;
}
ne++;
father[m]++;
}
}
return ;
}

HDU ACM 1325 / POJ 1308 Is It A Tree?的更多相关文章

  1. 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 ...

  2. HDU 1325,POJ 1308 Is It A Tree

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

  3. 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 并查集的运用 ...

  4. HDU ACM 1051/ POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. POJ 1308 Is It A Tree? (并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24237   Accepted: 8311 De ...

  6. POJ 1308 Is It A Tree? (并查集)

    Is It A Tree? 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/M Description A tree is a w ...

  7. POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18778   Accepted: 6395 De ...

  8. POJ 1308 Is It A Tree?--题解报告

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31092   Accepted: 10549 D ...

  9. POJ 1308 Is It A Tree? 解题报告

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32052   Accepted: 10876 D ...

随机推荐

  1. Struts标签、Ognl表达式、el表达式、jstl标签库这四者之间的关系和各自作用

    我之前虽然会用,但是一直分不清彼此之间有什么区别,所以查找资料,将它们进行整合区分,加深了解, 一 介绍 1.Struts2的作用   Struts2标签库提供了主题.模板支持,极大地简化了视图页面的 ...

  2. How to: Read Object Data from an XML File

    This example reads object data that was previously written to an XML file using the XmlSerializer cl ...

  3. [CF660C]Hard Process(尺取法)

    题目链接:http://codeforces.com/problemset/problem/660/C 尺取法,每次遇到0的时候补一个1,直到补完或者越界为止.之后每次从左向右回收一个0点.记录路径用 ...

  4. Post的请求案例

    1.简单的post请求案例 $.post(rootPath+"/jasframework/loginLog/getStatisticsInfoByUserId.do",functi ...

  5. 一个基于PDO的数据库操作类(新) 一个PDO事务实例

    <?php /* * 作者:胡睿 * 日期:2011/03/19 * 电邮:hooray0905@foxmail.com * * 20110319 * 常用数据库操作,如:增删改查,获取单条记录 ...

  6. wx处理鼠标事件

    #include "MainFrame.h" BEGIN_EVENT_TABLE(MyFrame,wxFrame) EVT_LEFT_DOWN(MyFrame::OnMouseLe ...

  7. Struts2 的 helloworld

    配置步骤: 1.在你的strut2目录下找到例子项目,把它的 lib 下的jar拷贝到你的项目.例如我的:struts-2.3.24\apps\struts2-blank 2.struts-2.3.2 ...

  8. android4.0以上部分手机绘图时会出现重影

    canvas外层的div需要设定属性style="overflow:visible;-webkit-transform: translateZ(0);

  9. Math.random();函数 随机数

    random函数参数 无参数 random函数返回值 返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) random函数示例 document.write(Math.random()); ...

  10. js函数内嵌函数的整体跳出 .

    stop=false; $.ajax({success:function(){ 这里面不能用return false跳出整个<script></script>,只能跳出该处的f ...