HDU ACM 1325 / POJ 1308 Is It A Tree?
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?的更多相关文章
- 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 ...
 - HDU 1325,POJ 1308  Is It A Tree
		
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
 - 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 并查集的运用 ...
 - HDU ACM 1051/ POJ 1065 Wooden Sticks
		
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
 - POJ 1308 Is It A Tree? (并查集)
		
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24237 Accepted: 8311 De ...
 - 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 ...
 - POJ 1308 Is It A Tree?
		
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18778 Accepted: 6395 De ...
 - POJ 1308 Is It A Tree?--题解报告
		
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31092 Accepted: 10549 D ...
 - POJ 1308 Is It A Tree? 解题报告
		
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32052 Accepted: 10876 D ...
 
随机推荐
- Android HTTPS(4)直接用SSLSocket,黑名单,客户端证书
			
Warnings About Using SSLSocket Directly So far, the examples have focused on HTTPS using HttpsURLCon ...
 - Mtk Android 打包解包*.img
			
打包/解包 boot.img, system.img, userdata.img, or recovery.img [DESCRIPTION] MTK codebase编译出来的image必须使用MT ...
 - Linux同步机制(二) - 条件变量,信号量,文件锁,栅栏
			
1 条件变量 条件变量是一种同步机制,允许线程挂起,直到共享数据上的某些条件得到满足. 1.1 相关函数 #include <pthread.h> pthread_cond_t cond ...
 - Qt之QHostInfo
			
简述 QHostInfo 类为主机名查找提供了静态函数. QHostInfo 利用操作系统提供的查询机制来查询与特定主机名相关联的主机的 IP 地址,或者与一个IP地址相关联的主机名.这个类提供了两个 ...
 - UVa 1608 (分治 中途相遇) Non-boring sequences
			
预处理一下每个元素左边和右边最近的相邻元素. 对于一个区间[l, r]和区间内某一个元素,这个元素在这个区间唯一当且仅当左右两边最近的相邻元素不在这个区间内.这样就可以O(1)完成查询. 首先查找整个 ...
 - 51nod1161 Partial Sums
			
开始想的是O(n2logk)的算法但是显然会tle.看了解题报告然后就打表找起规律来.嘛是组合数嘛.时间复杂度是O(nlogn+n2)的 #include<cstdio> #include ...
 - 安卓WebView中接口隐患与手机挂马利用(远程命令执行)
			
安卓应用存在安全漏洞,浏览网站打开链接即可中招.目前有白帽子提交漏洞表明目前安卓平台上的应用普遍存在一个安全漏洞,用户打开一个链接就可导致远程安装恶意应用甚至完全控制用户手机,目前微信,手机QQ,QV ...
 - ASP.NET 4的Demo实践:URL路由改进支持
			
从.NET框架3.5 SP1开始,微软推出了ASP.NET路由支持,从而实现了特定资源的URL与其对应的Web服务器上的物理文件之间的彻底解耦.借助于ASP.NET路由支持,开发人员可以定义一组路由规 ...
 - python练习程序(c100经典例3)
			
题目: 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? for i in range(1,100000): a=i+100; b=a+168; sa=int ...
 - 如何定义 Java 中的方法
			
所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 一般情况下,定义一个方法的语法是: 其中: 1. 访问修饰符:方法允许被访问的权限范围, 可以是 public.protected.p ...