HUD Is It A Tree?!!!!!)
这个是一个非常恶心的题,刚开始以为是POJ的题,结果发现不是,一直都是超时。上题吧
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.
5 6 0 0
7 4 7 8 7 6 0 0
5 3 5 6 5 2 0 0
Case 2 is a tree.
Case 3 is not a tree.
#include<stdio.h>
#include<string.h>
const int INF=;
int par[INF],ran[INF],sum;
bool t;
int find(int x)
{
if(par[x]==x)
return x;
return par[x]=find(par[x]);
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
par[x]=y;
else
t=true;
}
int main()
{
int x,y,m=;
while()
{
memset(par,,sizeof(par));
memset(ran,,sizeof(ran));
t=false;
while(scanf("%d%d",&x,&y)&&(x||y))
{
if(x<&&y<)
return ;
if(par[x]==)
par[x]=x;
if(par[y]==)
par[y]=y;
unite(x,y);
ran[y]++;
}
if(t)
printf("Case %d is not a tree.\n",m++);
else
{
int sum=,num=;
for(int i=; i<INF; i++)
{
if(par[i]==i)
{
sum++;
if(sum>)
{
num=;
break;
}
}
if(ran[i]>)
{
num=;
break;
}
}
if(num)
printf("Case %d is not a tree.\n",m++);
else
printf("Case %d is a tree.\n",m++);
}
}
return ;
}
HUD Is It A Tree?!!!!!)的更多相关文章
- debian、ubuntu:使用apt包管理器可能存在的问题! 让新手望而却步!
apt包管理器说好真好,说不好真不好. 最近在debian9.ubuntu18.04上安装oracle 10g 玩. 怎么都准备不好安装环境.原因就是i386构架体系的deb包总安装不正确! baid ...
- js 也来 - 【拉勾专场】抛弃简历!让代码说话!
前些日子谢亮兄弟丢了一个链接在群里,我当时看了下,觉得这种装逼题目没什么意思,因为每种语言都有不同的实现方法,你怎么能说你的方法一定比其他语言的好,所以要好的思路 + 好的语言特性运用才能让代码升华. ...
- 《Javascript DOM编程艺术》 读书笔记 —— 好书,通俗易懂!!!!! 相当的严谨!!!!
1.javascript弱类型语言,解释性语言. 2.javascript数据类型:字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空( ...
- 一天完成把PC网站改为自适应!原来这么简单!
http://www.webkaka.com/blog/archives/how-to-modify-a-web-page-to-be-responsive.html 一天完成把PC网站改为自适应!原 ...
- 【Mood-8】IT男!五更天!
男人从毕业到30岁之间所承受的叠加的压力,赡养父母.结婚生子.升职加薪.工作压力.生活质量,这些东西可以压得我们喘不过起来,也成为我们成长的动力, 我们累,但不说累,我们闷着头做事儿,不张扬,但每个都 ...
- Asp.Net读取服务器EXE文件的方法!(超简单实例)
Asp.Net读取服务器EXE文件的方法!(超简单实例) Process process = new Process(); process.StartInfo.FileName = "d:\ ...
- 摇滚吧HTML5!有声前端交互!(Hello, Jsonic!)
软件工程师们摆弄1和0编写他们的乌托邦,音乐人门把玩12平均律上的音符构筑他们的伊甸园.最近,我偶然看了<蓝色骨头>这部电影,片中的男主角是位黑客,同时又兼具音乐创作的才华.在现实生活中, ...
- genymotion 模拟器 真是牛叉了 速度超快啊!!! 不解释了!建议大家速度去体验一把吧!
已经有人写了blog了 我就不再赘述了,详情去这里看去吧!! android genymotion模拟器怎么使用以及和google提供的模拟器性能对比 http://blog.csdn.net/ ...
- 【百度地图API】交你如何用百度地图搜索自己的数据!不需数据库!
原文:[百度地图API]交你如何用百度地图搜索自己的数据!不需数据库! 摘要: 我有一定的房产数据,还有银行数据.我想在百度地图上标注出来,并且能搜索到我这些数据. 可是百度的数据库上并没有我的数据. ...
- Hello world! My first blog!
Hello world! My first blog!
随机推荐
- EXAM-2018-7-29
EXAM-2018-7-29 未完成 [ ] H [ ] A D 莫名TLE 不在循环里写strlen()就行了 F 相减特判 水题 J 模拟一下就可以发现规律,o(n) K 每个数加一减一不变,用m ...
- 导出Wireless组中的成员
get-adgroupmember -Identity wireless |export-csv -path C:\Group.csv -Encoding UTF8
- 71)PHP,使用cookie的语法问题
1) 为啥用数组的形式,就是这样好区分,你看都是跟student相关的东西, (2)
- python语法基础-函数-迭代器和生成器-长期维护
############### 迭代器 ############## """ 迭代器 这是一个新的知识点 我们学习过的可以迭代的对象有哪些? list str ...
- 3dmax2019卸载/安装失败/如何彻底卸载清除干净3dmax2019注册表和文件的方法
3dmax2019提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dmax2019失败提示3dmax2019安装未完成,某些产品无法安装,也有时候想重新安装3 ...
- perf4j @Profiled常用写法
以下内容大部分摘抄自网络上信息. 1.默认写法 @Profiled 日志语句形如: 2009-09-07 14:37:23,734 [main] INFO org.perf4j.TimingLogge ...
- js各继承方法的优缺点
在js中有很多种继承的方法,下面总结这些方法的优缺点. ####1.原型链继承 优点: 非常纯粹的继承关系,实例是子类的实例,也是父类的实例 父类新增原型方法/原型属性,子类都能访问到 简单,易于实现 ...
- 《内蒙古自治区第十二届大学生程序设计竞赛试题_D: 正品的概率》
问题 D: 正品的概率 内存限制:128 MB时间限制:1 S标准输入输出 题目类型:传统评测方式:文本比较上传者:外部导入 提交:36通过:7 返回比赛提交提交记录 题目描述 袋中有m枚正品硬币,n ...
- printf 输出浮点数
在测试printf函数输出结果时,写了如下代码: /** * printf:格式化输出函数 * printf函数不会按照格式控制而对数据类型进行转换,不管三七二十一, * 抓到二进制数据就按照格式控制 ...
- Nginx笔记总结二:Nginx编译参数
-prefix= 安装路径-with-http_ssl_module ...