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. Support Library(5)在eclipse中导入SupportXXXDemos

    Support4Demos只用一个v4.,Support7Demos只要v13.jar, SupportAppNavigation只要一个v4.jar. Support7Demos 需要资源全部v7系 ...

  2. BZOJ 1507 Editor(块状链表)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...

  3. Entity Framework Architecture

    http://www.entityframeworktutorial.net/EntityFramework-Architecture.aspx The following figure shows ...

  4. R语言算术运算和逻辑运算

    Arithmetic Operators Operator Description + addition - subtraction * multiplication / division ^ or ...

  5. [HZNUOJ1524]排队买票(DP)

    题目链接:http://acm.hznu.edu.cn/JudgeOnline/problem.php?id=1524 简单分析后可以知道每一个手持两元的小朋友前面,售票员手里至少有一个一元. 假设d ...

  6. nginx.conf 配置文件详解

    简单的实现nginx在前端做反向代理服务器的例子,处理js.png等静态文件,jsp等动态请求转发到其它服务器tomcat: user www www; worker_processes ; erro ...

  7. leetcode:Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  8. gulp使用外部配置文件

    这很有好处,因为它使得任务很干净,并且 config.json 可以被其他的任务运行器(例如grunt)重复利用. config.json { "desktop" : { &quo ...

  9. Hadoop集群(第6期)_WordCount运行详解

    1.MapReduce理论简介 1.1 MapReduce编程模型 MapReduce采用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完成,然 ...

  10. c#调用系统资源大集合-2

    public static void 打开格式化对话框() { Process.Start("rundll32.exe"," shell32.dll,SHFormatDr ...