题目:

http://ac.jobdu.com/problem.php?pid=1481

http://acm.nyist.net/JudgeOnline/problem.php?pid=129

http://poj.org/problem?id=1308

http://acm.hdu.edu.cn/showproblem.php?pid=1272

题目意思就是判断一些给定的支点构成的树是不是一颗合法的树,

判断是不是一颗合法的树如下:

        1、该树只有一个根节点

        2、不存在环

对于上述两种情况采用如下进行判断:

        1、定义一个标识符,当这个点出现,并且父节点和自己相同的节点个数大于1,表示有多个根节点,反之不是

        2、在寻找父节点时,这两点指向同一个父节点,则出现了回路

nyoj 和poj 直接使用下代码就ok ,hdu上面相应的改改(代码最后一个)一开始一直RE 后来看看了题目 是10^5,(纠结了)

/*hdu 1272*/
#include<stdio.h>
#include <string.h>
#define N 100005 int f[N];
int a[N];
int flag = 1;
int max = 0; void init()
{
memset(a,0,sizeof(a)); memset(f,0,sizeof(f));
max = 0;
flag = 1;
int i;
for(i = 0; i <= N; i++)
f[i] = i;
}
int find_f(int x)
{
if(x == f[x]) return x;
else
return f[x] = find_f(f[x]);
} void add(int x,int y)
{
int fx = find_f(x);
int fy = find_f(y);
if(fx != fy)
f[fy] = fx;
else flag = 0;
} int main()
{
int x,y;
int fx,fy;
init();
while(1)
{
scanf("%d%d",&x,&y);
if(x == -1 && y == -1) break;
if(x ==0 && y == 0)
{
int t = 0;
if(flag)
{
int i;
for(i = 0; i <= max; i++)
if(a[i] == 1)
{
if(i == f[i]) t++;
}
}
if(t > 1)
flag = 0;
if(flag)
printf("Yes\n");
else
printf("No\n");
init();
}
else
{
if(flag)
{
max = max > (x > y ? x : y) ? max: (x > y ? x : y) ;
add(x,y);
a[x] = 1;
a[y] = 1;
}
}
}
return 0;
}
/***
判断是否有环,当他们的父相同则会出现环,然后是判断是否还有孤立的点(树存在)
*/
#include<stdio.h>
#include <string.h>
#define N 10005 int f[N];
int a[N];
int in[N];
int flag = 1;
int max = 0; void init()
{
memset(a,0,sizeof(a));
memset(in,0,sizeof(in));
memset(f,0,sizeof(f));
max = 0;
flag = 1;
int i;
for(i = 0; i <= N; i++)
f[i] = i;
}
int find_f(int x)
{
if(x == f[x]) return x;
else
return find_f(f[x]);
}
int main()
{
int x,y;
int fx,fy;
init();
int tt = 0;
while(1)
{
scanf("%d%d",&x,&y);
if(x == -1 && y == -1)
break;
if(x == 0 && y == 0)
{
int t = 0,i;
if(flag)
{
for(i = 0; i <= max; i++)
if(a[i] == 1)
{
if(i == f[i]) t++;
}
}
if(t > 1)
flag = 0;
tt++;
if(flag)
printf("Case %d is a tree.\n",tt);
else
printf("Case %d is not a tree.\n",tt); init();
}
else
{
a[x] = 1;
a[y] = 1;
in[y]++;
max = max > (x > y ? x : y) ? max : (x > y ? x : y);
if(in[y] > 1)
{
flag = 0;
}
if(flag == 1)
{
fx = find_f(x);
fy = find_f(y);
//printf("%d %d\n",fx,fy);
if(fx != fy) f[fy] = fx;
else
flag = 0;
} }
}
return 0;
}

最后跟新贴一个javaAC全部的代码

 import java.util.Scanner;

 //判断是否是树

 public class Main {
private static My_Tree[] tree = new My_Tree[10007] ;//定义对象数组保存输入的值
private static boolean Main_flag = false;
private static int t = 1; public static void init(){
for(int i = 0; i < 10007; i++)
tree[i] =new My_Tree(i,i,0,false);
Main.Main_flag = false;
}
public static int find_f(int x){
if(tree[x].getN() == tree[x].getF())
return tree[x].getF();
else
return find_f(tree[x].getF());
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n,m;
Main.init();
while(true){
n = cin.nextInt();
m = cin.nextInt();
// System.out.println(Main.Main_flag);
if(n == -1 && m == -1)
break;
if(n == 0 && m == 0){
int t_tmp = 0;
if(!Main.Main_flag){
for(int i = 0; i < 10007; i++)
if(tree[i].isFlag()){
// tree[i].print();
if(tree[i].getF() == tree[i].getN())
t_tmp++;
}
}
if(t_tmp > 1)
Main.Main_flag = true;
if(!Main.Main_flag)
System.out.println("Case "+(Main.t++)+" is a tree.");
else
System.out.println("Case "+(Main.t++)+" is not a tree.");
Main.init();
}
else{
int fx,fy; tree[n].setFlag(true);
tree[m].setFlag(true);
tree[m].setIn(tree[m].getIn()+1);
if(tree[m].getIn() > 1)
Main.Main_flag = true; if(!Main.Main_flag){
fx = Main.find_f(n);
fy = Main.find_f(m);
if(fx != fy)
tree[m].setF(fx);
else
Main.Main_flag = true;
}
}
}
} } class My_Tree{
private int n,f,in;
private boolean flag;
public My_Tree(int n, int f, int in, boolean flag) {
super();
this.n = n;
this.f = f;
this.in = in;
this.flag = flag;
}
public My_Tree() {
super();
} public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public int getF() {
return f;
}
public void setF(int f) {
this.f = f;
}
public int getIn() {
return in;
}
public void setIn(int in) {
this.in = in;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void print(){
System.out.println("--->"+n+f+in+flag);
}
}
/**************************************************************
Problem: 1481
User: yangyin1217
Language: Java
Result: Accepted
Time:790 ms
Memory:105768 kb
****************************************************************/

树的判断(poj nyoj hduoj)的更多相关文章

  1. PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)

    嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...

  2. 算法手记 之 数据结构(线段树详解)(POJ 3468)

    依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...

  3. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  4. (树)判断二叉树是否为BST

    题目:判断一颗二叉树是否为BST. 思路:其实这个问题可以有多个解决方法. 方法一:递归解决.根据BST的特性.左边的小于根节点的值,右边的大于根节点的值.并且对于每一棵子树都是如此.所以我们可以直接 ...

  5. poj2513字典树+欧拉图判断+并查集断连通

    题意:俩头带有颜色的木棒,要求按颜色同的首尾相连,可能否? 思路:棒子本身是一条边,以俩端为顶点(同颜色共点),即求是否有无向图欧拉路(每条棒子只有一根, 边只能用一次,用一次边即选一次棒子). 先判 ...

  6. 关于树的重心--POJ 1655

    树的重心的定义: 在一棵树中,找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 通俗来说就是以这个点为根节点,找到他最大的衣蛾子树,然后 ...

  7. 线段树单点更新poj 2828

    n个人 他要插入的位置 和权值(这东西就最后输出来的) 倒的插就一定是他自己的位子 一个线段树维护一下就可以了 nlog(n) #include<stdio.h> #include< ...

  8. 线段树(区间操作) POJ 3325 Help with Intervals

    题目传送门 题意:四种集合的操作,对应区间的01,问最后存在集合存在的区间. 分析:U T [l, r]填充1; I T [0, l), (r, N]填充0; D T [l, r]填充0; C T[0 ...

  9. 树的直径 poj 2631

    树的直径:从随意一点出发,BFS找到最远的距离,然后在从该点出发BFS找到最远的距离 #include <iostream> #include <algorithm> #inc ...

随机推荐

  1. Fedora、CentOS install TTF/otf fonts

    Step 1:切换至字体下载目录: [Richard@localhost Downloads]$ ll | grep otf -rw-rw-r--. Richard Richard 7月 RBNo2L ...

  2. CentOS添加中科大、163 yum源

    首先备份CentOS-Base.repo [root@richard yum.repos.d]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos ...

  3. Java程序执行Linux命令

    Java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个 ...

  4. Oracle学习之Oracle 树操作(select…start with…connect by…prior)

    转自:http://www.cnblogs.com/linjiqin/archive/2013/06/24/3152674.html oracle树查询的最重要的就是select…start with ...

  5. javascript中的for……in循环

    <script type="text/javascript">    var theBeatles=new Array("John","P ...

  6. nodejs 下载网页及相关资源文件

    功能其实很见简单,通过 phantomjs.exe 采集 url 加载的资源,通过子进程的方式,启动nodejs 加载所有的资源,对于css的资源,匹配css内容,下载里面的url资源 当然功能还是很 ...

  7. php的模板引擎

    设计一个交互式的网站,我们需要关注两个主要的问题:分别是图形用户界面和业务逻辑.例如,一个标准的web开发小组由两三个美工和三个程序员组成,则设计流程是:美工设计者制作了项目的网站的界面模板,然后把它 ...

  8. 有哪些适合学生参与的 C++,网络编程方面的开源项目?

    有哪些适合学生参与的 C++,网络编程方面的开源项目?   Tinyhttpd是一个超轻量型Http Server,使用C语言开发,全部代码只有502行(包括注释),附带一个简单的Client,可以通 ...

  9. poj2070简单题

    #include <stdio.h> #include <stdlib.h> int main() { float sped; int wei,sth; while(scanf ...

  10. [LeetCode][Python]18: 4Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...