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!
随机推荐
- addEventListener和onclick的区别
onclick只出现一次alert:我是click2[很正常第一次click事件会被第二次所覆盖],但是addEventListener却可以先后运行,不会被覆盖[正如:它允许给一个事件注册多个监听器 ...
- Miller-Rabin素数检测算法
遇到了一个题: Description: Goldbach's conjecture is one of the oldest and best-known unsolved problems in ...
- Rearrangement
In a two dimensional array of integers of size 2×n2 \times n2×n, is it possible to rearrange integer ...
- maven中指定build一个project中几个特定的子modules
问题由来: 一个项目可能会有多个子module,在特定情况下可能只需要build其中几个module. 例如我的项目的目录结构如下 myproject |------------module_one ...
- django框架进阶-form组件-长期维护
################## form组件做了什么事情? ####################### 之前web开发的模式,以注册为例 1,要有一个注册页面,然后有一个f ...
- 方差分析||MSA/MSE|
应用统计学-方差分析 数值型数据使用线性回归来研究因素对因变量的影响.类别型数据使用方差分析来研究因素对因变量的影响.方差分析是使用方差比MSA/MSE来检验均值是否全相等,即相等是H0假设,而不全相 ...
- Python-SSH批量登陆并执行命令
Python-SSH批量登陆并执行命令 #!/usr/bin/env python #-*- coding:utf-8 -*- import paramiko from time import cti ...
- FPGA开平方的实现
3种方法: 1.JPL近似的实现方法 `timescale 1ns / 1ps )( clk, syn_rst, dataa, datab, ampout); input clk; :] dataa; ...
- [LC] 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode Day 6
LeetCode0006 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ,指定行数为 3 时,排 ...