树的判断(poj nyoj hduoj)
题目:
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)的更多相关文章
- 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 ...
- 算法手记 之 数据结构(线段树详解)(POJ 3468)
依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- (树)判断二叉树是否为BST
题目:判断一颗二叉树是否为BST. 思路:其实这个问题可以有多个解决方法. 方法一:递归解决.根据BST的特性.左边的小于根节点的值,右边的大于根节点的值.并且对于每一棵子树都是如此.所以我们可以直接 ...
- poj2513字典树+欧拉图判断+并查集断连通
题意:俩头带有颜色的木棒,要求按颜色同的首尾相连,可能否? 思路:棒子本身是一条边,以俩端为顶点(同颜色共点),即求是否有无向图欧拉路(每条棒子只有一根, 边只能用一次,用一次边即选一次棒子). 先判 ...
- 关于树的重心--POJ 1655
树的重心的定义: 在一棵树中,找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 通俗来说就是以这个点为根节点,找到他最大的衣蛾子树,然后 ...
- 线段树单点更新poj 2828
n个人 他要插入的位置 和权值(这东西就最后输出来的) 倒的插就一定是他自己的位子 一个线段树维护一下就可以了 nlog(n) #include<stdio.h> #include< ...
- 线段树(区间操作) 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 ...
- 树的直径 poj 2631
树的直径:从随意一点出发,BFS找到最远的距离,然后在从该点出发BFS找到最远的距离 #include <iostream> #include <algorithm> #inc ...
随机推荐
- SQL Server执行计划那些事儿(1)——哈希、合并、嵌套联接的选择
接下来的文章是记录自己曾经的盲点,同时也透漏了自己的发展历程(可能发展也算不上,只能说是瞎混).当然,一些盲点也在工作和探究过程中慢慢有些眉目,现在也愿意发扬博客园的奉献精神,拿出来和大家分享一下. ...
- Struts2配置问题java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
方法一:右键点击项目--->build path-->configure build path-->左侧菜单栏就会看到Deployment Assembly-->右侧点击add ...
- c++多线程同步使用的对象
线程的同步 Critical section(临界区)用来实现“排他性占有”.适用范围是单一进程的各线程之间.它是: · 一个局部性对象,不是一个核心对象. · 快速而 ...
- QF——UI之几种常用的隐藏键盘的方法
怎么在填写完UITextField之后,点击空白处,隐藏软键盘. 下面两个方法都可以隐藏键盘 [tf resignFirstResponder]; 停止textfield的第一响应者 [self.vi ...
- SVN版本控制服务器安装与配置
版本管理在我们日常学习中一般接触不到,因为我们都是一个人在学习与开发一些练习的项目.但是实际中,一般项目都是协同开发的,这样就需要一个版本管理工具,常见的有SVN/CVS/GitHut等...通过它们 ...
- javascript 常用函数
//获取元素的样式值. function getStyle(elem,name){ if(elem.style[name]){ return elem.style[name]; }else if(el ...
- 比callback更简洁的链式执行promise
promise自己理解的也不够深刻,具体知识点不在这里细说了 直接上个例子,清晰明了,自己去悟吧 <script type="text/javascript"> //模 ...
- android sdk 更新问题——截止2014年6月10日有效
因为墙的原因,很多人的sdk都更新不了,下面记录了我刚刚实现更新的方法: 进到Android SDK Manager,菜单Tools->Options..,这时弹出一个框,在这个框的下面Othe ...
- Codis集群的搭建
Codis集群的搭建与使用 一.简介 Codis是一个分布式的Redis解决方案,对于上层的应用来说,连接Codis Proxy和连接原生的Redis Server没有明显的区别(不支持的命令列表 ...
- oracle 查询表的大小,表空间的使用情况,默认表空间
oracle 查询表的大小,表空间的使用情况,默认表空间 oracle 查询表的大小,表空间的使用情况,默认表空间 --查看某张表占用磁盘空间大小 ( 表名大写 ) Select Segment_Na ...