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. What is Entity Framework?

    1.什么是EntityFramework? http://www.entityframeworktutorial.net/what-is-entityframework.aspx Writing an ...

  2. 巧用ifstream判断文件是否存在

    最近在写手写数字的识别软件,训练样例数量巨大而且数字个数不唯一,有可能在中途粘出一部分做测试样例.因此写下面的脚本来获取文件名,之后丢到训练函数里. #include <algorithm> ...

  3. 使用JAVA直观感受快速排序与冒泡排序的性能差异

    初学算法,肯定会编写排序算法 其中两个最为有名的就是冒泡排序和快速排序 理论上冒泡排序的时间复杂度为O(N^2),快速排序的时间复杂度为O(NlogN) 下面本门使用JAVA,分别编写三段排序程序 对 ...

  4. 浅谈 Scala 中下划线的用途

    Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...

  5. Core Text

    Core Text 本文所涉及的代码你可以在这里下载到 https://github.com/kejinlu/CTTest,包含两个项目,一个Mac的NSTextView的测试项目,一个iOS的Cor ...

  6. Android custom View AirConditionerView hacking

    package com.example.arc.view; import android.content.Context; import android.graphics.Canvas; import ...

  7. android studio 加载第三方类库

    以引入Xutil包为例 1. 将Xutil包导入到module的libs目录下 2. File->project structure 还有一种方法是在libs目录下右键点击Add as libr ...

  8. jQuery.validate API

  9. HDU 4766 Network

    题意   就是找到一个 位置 使得路由器可以覆盖所有英雄    可以不覆盖主人自己, 找到距离 主人房子最近的位置,距离为多少 没想到  其实是道水题啊!!  分三个情况讨论 第一个情况    如果 ...

  10. 【转】安装Django

    原文网址:http://www.crifan.com/record_install_django/ 1.参考Quick install guide,最终找到下载的地址: http://bitnami. ...