比较恶心

1: 0 0 空树是一棵树
2: 1 1 0 0 不是树
3: 1 2 1 2 0 0 不是树...
4: 1 2 2 3 4 5 不是树 森林不算是树
5: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 错
6: 1 2 2 1 0 0 也是错误的
#include<stdio.h>
#include<string.h>
int pa[],a[],b[],n,in[],map[],hash[];
void init()
{
for(int i=;i<=n;i++)
{
pa[i]=i;
}
}
int find(int x)
{
if(x!=pa[x])
pa[x]=find(pa[x]);
return pa[x];
}
int main()
{
int i,j,flag,ff=;
while(scanf("%d%d",&a[],&b[])!=EOF)
{
j=;
memset(hash,,sizeof(hash));
memset(map,,sizeof(map));
memset(in,,sizeof(in));
if(!hash[a[]])
map[j++]=a[];
if(!hash[b[]])
map[j++]=b[];
if(a[]==&&b[]==)
{
printf("Case %d is a tree.\n",++ff);
continue;
}
n=;
flag=;
if(a[]<&&b[]<)
break;
in[a[]]++,in[b[]]++;
if(a[] > n) n = a[];
if(b[] > n) n = b[];
i=;
while()
{
i++;
scanf("%d %d",&a[i],&b[i]);
if(a[i]==&&b[i]==)
break;
if(!hash[a[i]])
{
hash[a[i]]=;
map[j++]=a[i];
}
if(!hash[b[i]])
{
map[j++]=b[i];
hash[b[i]]=;
}
if(a[i] > n) n = a[i];
if(b[i] > n) n = b[i];
}
int num=j;
//for(j=0;j<num;j++)
// printf("%d ",map[j]);
//printf("\n");
int len=i;
init();
for(i=;i<len;i++)
{
int x,y;
x=find(a[i]);
y=find(b[i]);
if(x!=y)
{
pa[x]=y;
}
else
flag=;
}
int ans=;
for(i=;i<num;i++)
{
if(pa[map[i]]==map[i])
{
ans++;
}
}
if(ans>)
flag=;
if(flag)
printf("Case %d is a tree.\n",++ff);
else
printf("Case %d is not a tree.\n",++ff);
}
}

poj1308 并查集的更多相关文章

  1. poj1308(并查集)

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

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

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

  3. POJ1308:Is It A Tree?(并查集)

    Is It A Tree? 题目链接:http://poj.org/problem?id=1308 Description: A tree is a well-known data structure ...

  4. POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28838   Accepted: 9843 -& ...

  5. POJ-1308 Is It A Tree?(并查集判断是否是树)

    http://poj.org/problem?id=1308 Description A tree is a well-known data structure that is either empt ...

  6. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  7. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  8. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  9. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

随机推荐

  1. Android 动态加载 (一) 态加载机制 案例一

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

  2. 通过imeMode禁用键盘只能输入数字

    var obj = document.getElementById('y'); var arr = [48,49,50,51,52,53,54,55,56,57];//数字对应的键码 obj.onke ...

  3. [转]redis 五种数据类型的使用场景

    FROM : http://blog.csdn.net/gaogaoshan/article/details/41039581#t5 String 1.String 常用命令: 除了get.set.i ...

  4. Use CLR Profiler

    Use CLR Profiler 第一次翻译对我而言比较长的E文,有很多不足之处,请见谅.(个人的习惯GC又做了名词又做了名词) 原文:http://msdn.microsoft.com/en-us/ ...

  5. 每日一SQL-善用DATEADD和DATEDIFF

    转自:http://www.dotblogs.com.tw/lastsecret/archive/2010/10/04/18097.aspx 上個星期去Tech-Day聽了幾場有趣的課,其中一堂是楊志 ...

  6. Android 动画之AlphaAnimation应用详解

    窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation.AlphaAnimation(0.01f, 1.0f); 从0.01f到1.0f渐变.学过 ...

  7. Windows10+Ubuntu双系统安装[多图]

    最近因为毕设重新回归Ubuntu,手头有一台装了Win10的ThinkPad X240s,最终成功完成了Windows 10 教育版和Ubuntu Kylin 15.10 的双系统配置,下文(多图慎入 ...

  8. [CareerCup] 12.3 Test Move Method in a Chess Game 测试象棋游戏中的移动方法

    12.3 We have the following method used in a chess game: boolean canMoveTo( int x, int y). This metho ...

  9. LeetCode:Clone Graph

    题目如下:实现克隆图的算法  题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...

  10. 动画:UIKitAnimation 简单动画学习 iOS (一) 渐变 、 移动 、翻页、来回翻转 ——转载

    转载请说明(谢谢) http://blog.csdn.net/a21064346/article/details/7851695 点击打开链接 以下 一个系列的 动画效果 在 UIView.h文件中可 ...