HDU1325 &&poj1308 基础并查集
和上一道小希的迷宫差点儿相同,可是在HDU上提交一直WA,POJ过了
HDU的数据太强了吧,强的我感觉数据有问题
题意:输入若干对点,推断是否是一颗树,转化过来也就是是否存在环
点数-边数=1,还要推断每一个点的入度都<=1
POJ AC代码
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int INF = 1e8;
using namespace std;
int father[1010];
bool vis[1010];
int rudu[1010];
int findx(int r)
{
int i = r,j;
while(father[r]!=r)
{
r=father[r];
} while(father[i]!=r)
{
j = father[i];
father[i] = r;
i = j;
}
return r;
} bool Merge(int x,int y)
{
int fx,fy;
fx=findx(x);
fy=findx(y);
if(fx!=fy)
{
father[fx]=fy;
return 1;
}
else
return 0;
}
void init()
{
for(int i=0;i<1010;i++)
{
father[i]=i;
vis[i] = 0 ;
rudu[i] = 0;
}
}
int main()
{
int a,b,c = 0;
while(scanf("%d%d",&a,&b)!=EOF)
{
c++; if(a<0&&b<0)
break;
int flag=1,t=0;
if(a==0 && b==0)
{
printf("Case %d ",c);
puts("is a tree.");
continue;
}
init();
int num = 0;
while(1)
{
if(a==0&&b==0) break;
if(flag==1)
{
if(!vis[a]) {num++; }
if(!vis[b]) {num++; rudu[b]++;}
if(rudu[b]>1)
flag = 0;
vis[a]=1; vis[b]=1;
if(Merge(a,b)==1)
{
t++;
}
else
flag = 0;
}
scanf("%d%d",&a,&b);
}
printf("Case %d ",c);
if(num-t==1 &&flag == 1)
puts("is a tree.");
else
puts("is not a tree.");
}
return 0;
}
HDU1325 &&poj1308 基础并查集的更多相关文章
- hdu 1829 基础并查集,查同性恋
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- HDU4496 D-City【基础并查集】
Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...
- AOJ 2170 Marked Ancestor (基础并查集)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...
- poj2236 基础并查集
题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O ...
- 基础并查集poj2236
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- poj1308(并查集)
题目链接:http://poj.org/problem;jsessionid=436A34AE4BE856FB2DF9B264DCA9AA4E?id=1308 题意:给定一些边让你判断是否构成数. 思 ...
- 并查集——poj1308(并查集延伸)
题目链接:Is It A Tree? 题意:给你一系列形如u v的点对(u v代表一条由u指向v的有向边),请问由给你的点构成的图是不是一棵树? 树的特征:①每个节点(除了根结点)只有一个入度:②只有 ...
随机推荐
- linux杂记(四)热键[Tab],[ctrl]-c,[ctrl]-d,在线求助man page/info page
[Tab]按键 他具有[命令补全](接在一串指令的第一个字后面)与[档案补齐](接在第一串指令的第二字以后时)的功能.如 [KANO@kelvin ~]$ ca[tab][tab] cabextrac ...
- 经常会用到的js函数
//获取样式function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ re ...
- 安卓2D游戏开发设置
http://www.cnblogs.com/xiaominghimi/archive/2010/12/23/1921402.html
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- oracle查询字符集语句
(1)查看字符集(三条都是等价的) 复制代码 代码如下: select * from v$nls_parameters where parameter='NLS_CHARACTERSET'sel ...
- 走进Groovy (一)
一直很喜欢脚本语言,但是一直在不大的公司工作,用得一直是“高大上”的JAVA语言,在真正的项目中,没什么机会用到脚本语言.这两年,又断断续续的用了2年的Ruby,再回头继续用JAVA,说实话,真感觉J ...
- QT 一些非常常用的操作
一 如果在窗体关闭前自行判断是否可关闭二 如何用打开和保存文件对话框 三 如何使用警告.信息等对话框 四 在Windows下Qt里为什么没有终端输出五 想在源代码中直接使用中文 ...
- Consuming Hidden WCF RIA Services
原文 http://codeseekah.com/2013/07/05/consuming-hidden-wcf-ria-services/ A Silverlight application mad ...
- cocos2dx CCTextFieldTTF
CCTextFieldTTF是一个提供文本输入的控件. 先上个简单的例子 CCSize size = __winSize; CCTextFieldTTF* textEdit = CCTextField ...
- oralce dg conf
http://wenku.baidu.com/view/ea9fa16cdd36a32d73758168.html http://ylw6006.blog.51cto.com/470441/84181 ...