POJ题目网址:http://poj.org/problem?id=1308

HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272

并查集的运用,如果想要形成一棵树,那么我们应该只能有一个根,并查集联合次数为节点数-1。

//Asimple
//#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
#define INF 0xfffffff
#define mod 10007
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
#define abs(x) x<0?-x:x
using namespace std;
typedef long long ll;
const int maxn = ;
int fa[maxn];
//int re[maxn];
int n, m, T, x, y, num, len;
set<int> s;
bool f; void init() {
for(int i=; i<maxn; i++) fa[i] = i;
f = false;
len = ;
s.clear();
} int find(int x) {
return fa[x]==x?x:fa[x]=find(fa[x]);
} void make_set(int x, int y) {
if( x==y ) f = true;
x = find(x);
y = find(y);
if( x!=y ){
fa[x] = y;
len ++;
} else f = true;
} void solve() { } void input() {
int cas = ;
while( true ) {
init();
cin >> x >> y;
if( x == && y == ) {
// cout << "Case "<< cas++ <<" ";
// puts("is a tree.");
puts("Yes");
continue;
}
if( x==- && y==- ) break;
s.insert(x);
s.insert(y);
make_set(x, y);
while( cin >> x >> y ) {
if( x== && y == ) break;
s.insert(x);
s.insert(y);
make_set(x, y);
}
// cout << "Case "<< cas++ <<" ";
if( len+ == s.size() && !f ) puts("Yes");
else puts("No");
}
} int main() {
input();
return ;
}

POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫的更多相关文章

  1. HDU 1272小希的迷宫(裸并查集,要判断是否构成环,是否是连通图)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    ...

  2. hdu 1272 小希的迷宫(并查集+最小生成树+队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)     ...

  3. hdu 1272 小希的迷宫 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 第二条并查集,和畅通工程的解法类似.判断小希的迷宫不符合条件,即有回路.我的做法是,在合并两个集 ...

  4. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

     本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...

  5. HDU - 1272 小希的迷宫 【并查集】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 思路 只需要判断 这张图 无环 并且只有一个连通块 就可以了 要注意 如果 只输入 0 0 那给 ...

  6. HDU 1272 小希的迷宫 并查集

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  7. hdu 1272 小希的迷宫

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. HDU 1272 小希的迷宫(并查集)

    题解: 首先,init,find,union操作都是模版,多敲几遍,在记记,就没问题了. 然后,本题就是多了一个flag数组,记录数据是否出现过,最后记录最小,和最大,遍历一般他们的父亲,只有一个就是 ...

  9. HDU 1272 小希的迷宫 (水题)

    题意: 其实就是让你判断一个图是否为树,要求不能有孤立的点(没有这中情况),且只能有1个连通图,且边数+1=点数,且每个点都有边(不可能只有1个点出现). 思路: 有可能出现连续的4个0,也就是有测试 ...

随机推荐

  1. wechat-注意事项

  2. internal table operation

    1: the basic operation *&---------------------------------------------------------------------* ...

  3. Jmeter测试报告

    服务器: 2个CPU,每个CPU 1个核,4G内存  20G硬盘 客户端(Jmeter):2个CPU,每个2个核,4+8内存   500G硬盘 ---------------------------- ...

  4. (转)EOS中账户、钱包和密钥的关系

    EOS对于账户的设计与ETH有很大的不同,引入了Account账户, Wallet钱包, 钱包密码, Key公私钥, Permission权限等众多概念,刚入门的时候感觉一头雾水.本文希望通过对这些概 ...

  5. java编写的Http协议的多线程下载器

    断点下载器还在实现中...... //////////////////////////////////界面/////////////////////////////////////////// pac ...

  6. [Java in NetBeans] Lesson 03. More Variables / Type Casting

    这个课程的参考视频在youtube. 主要学到的知识点有: It is different from python, that "1" only present string &q ...

  7. Repeater 中TextBox 触发TextChanged事件

    两种方法 1.TextBox 绑定TextChanged 并设置AutoPostBack ="true" ,如果不设置AutoPostBack ="true"则 ...

  8. Cocos Creator 动作(动画)笔记

    动作cc.ActionInterval 和cc.ActionInstant; var action = cc.moveTo(2, 100, 100); // 创建一个移动动作node.runActio ...

  9. unity3d对象池的使用

    说对象池之前首先来看看单例类和单例脚本的区别.这里有介绍 http://blog.csdn.net/lzhq1982/article/details/12649281 使用对象池的好处是不用每次都创建 ...

  10. 图像控件 ImageControl

    图像控件 书:164 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns: ...