和上一道小希的迷宫差点儿相同,可是在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 &amp;&amp;poj1308 基础并查集的更多相关文章

  1. hdu 1829 基础并查集,查同性恋

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  3. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  4. HDU4496 D-City【基础并查集】

    Problem Description Luxer is a really bad guy. He destroys everything he met.  One day Luxer went to ...

  5. AOJ 2170 Marked Ancestor (基础并查集)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...

  6. poj2236 基础并查集

    题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O ...

  7. 基础并查集poj2236

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  8. poj1308(并查集)

    题目链接:http://poj.org/problem;jsessionid=436A34AE4BE856FB2DF9B264DCA9AA4E?id=1308 题意:给定一些边让你判断是否构成数. 思 ...

  9. 并查集——poj1308(并查集延伸)

    题目链接:Is It A Tree? 题意:给你一系列形如u v的点对(u v代表一条由u指向v的有向边),请问由给你的点构成的图是不是一棵树? 树的特征:①每个节点(除了根结点)只有一个入度:②只有 ...

随机推荐

  1. git创建分支与合并分支

    git branch myfeture 创建分支 git checkout myfeture git add --all git commit -m git push origin myfeture ...

  2. hdu 3529 Bomberman - Just Search! 重复覆盖

    题目链接 依然是重复覆盖的模板.... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) #d ...

  3. 记录.net 中的常见术语

    --Entity Framework和NHibernate --EF和NH都是一种ORM技术.就是对象关系模型映射. --NHibernate和Entity Framework 4.0优劣势争论 -- ...

  4. discuz_style_default.xml 修改

    <?xml version="1.0" encoding="ISO-8859-1"?> <root> <item id=" ...

  5. COB(Chip On Board) 工艺技术

    COX(Chip On X) •X 基板:  PCB (Printed circuit board)  FPC (Flexible Printed Circuit)  Glass •导线焊接 球形焊接 ...

  6. HUB主要芯片方案

    HUB主要品牌:慧荣.擎泰.联盛  安国.创惟 创惟GL850G简介:拥有低耗电.温度低及接脚数减少等产品特性.它支援4个下游连接埠,采用48 pin LQFP封装,可完全支援USB 2.0/1.1规 ...

  7. aliyun 镜像

    [epel]name=Extra Packages for Enterprise Linux 6 - $basearchbaseurl=http://mirrors.aliyun.com/epel/6 ...

  8. 如何使用ssh-keygen生成key

    ssh-keygen - 生成.管理和转换认证密钥 通常使用:[b]ssh-keygen -i -f 公密匙名>> authorized_keys[/b] 语法详细介绍 [code]ssh ...

  9. strdup函数的使用方法

    函数名: strdup 功  能: 将串复制到新建的位置处 用  法: char *strdup(char *str): 这个函数在linux的man手冊里解释为: The strdup() func ...

  10. Android UI设计

    Android UI设计--PopupWindow显示位置设置 摘要: 当点击某个按钮并弹出PopupWindow时,PopupWindow左下角默认与按钮对齐,但是如果PopupWindow是下图的 ...