判断一个图是否是一个树,树满足一下2个条件即可:
1. 边树比node数少1
2. 所有node的入度非0即1

节点数是0的时候,空树,合法树~

代码如下

#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define MAX 100 int matrix[MAX][MAX];
int node[MAX] = {0};
int N = 0;
int M = 0; int check() {
int i, j;
int sum;
if(N == 0) {
return 1;
} if(N != M + 1) {
return 0;
} /* 每个节点的入度非0则1 */
for(i = 0; i < MAX; i++) {
sum = 0;
for(j = 0; j < MAX; j++) {
sum += matrix[j][i];
}
if(sum == 0 || sum == 1) {
;
}
else {
return 0;
}
}
/* 判断图中无环 */ return 1;
} void print() {
int i, j;
for(i = 0; i < 10; i++) {
for(j = 0; j < 10; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
} int main() {
int u,v;
int flag = 1;
int count = 0;
int i;
while(flag) {
/* init */
N = 0;
M = 0;
memset(node, 0, sizeof(node));
memset(matrix, 0, sizeof(matrix)); while(1) {
scanf("%d%d", &u, &v);
if(u == -1 && v == -1) {
flag = 0;
break;
}
if(u == 0 && v == 0) {
;
}
else {
u --;
v --;
M ++;
if(0 == node[u]) {
node[u] = 1;
N ++;
}
if(0 == node[v]) {
node[v] = 1;
N ++;
}
matrix[u][v] = 1;
continue;
}
count ++;
if(check()) {
printf("Case %d is a tree.\n", count);
}
else {
printf("Case %d is not a tree.\n", count);
}
break;
}
}
return 0;
}

  

【poj解题】1308的更多相关文章

  1. POJ解题经验交流

    感谢范意凯.陈申奥.庞可.杭业晟.王飞飏.周俊豪.沈逸轩等同学的收集整理.   题号:1003 Hangover求1/2+1/3+...1/n的和,问需多少项的和能超过给定的值 类似于Zerojudg ...

  2. 【poj解题】1028

    stack的应用 #include<iostream> #include<stack> #include<stdio.h> #include<stdlib.h ...

  3. 【poj解题】3664

    简单,两次排序 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 500 ...

  4. 【poj解题】3663

    排序, 遍历,需要裁减 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX ...

  5. [poj解题]1017

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41014   Accepted: 13776 Descrip ...

  6. 洛谷 P1731 [NOI1999]生日蛋糕 && POJ 1190 生日蛋糕

    题目传送门(洛谷)  OR 题目传送门(POJ) 解题思路: 一道搜索题,暴力思路比较容易想出来,但是这道题不剪枝肯定会TLE.所以这道题难点在于如何剪枝. 1.如果当前状态答案已经比我们以前某个状态 ...

  7. POJ 1308 Is It A Tree? 解题报告

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32052   Accepted: 10876 D ...

  8. POJ 1308

    http://poj.org/problem?id=1308 题意:判断这是不是一棵树. 思路:这个题还是有部分坑点的.首先空树也是一棵树,还有森林不是树. 关于森林的判断我是利用并查集把每一个点压缩 ...

  9. POJ 1308&&HDU 1272 并查集判断图

      HDU 1272 I - 小希的迷宫 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. 循环ip段 转载 出处不明

    public struct IP         {             public byte A;             public byte B;             public  ...

  2. Oracle 游标及存储过程实例

    /*********实例一*********/ create or replace procedure users_procedure is cursor users_cursor is select ...

  3. 使用anyremote进行远程鼠标控制

    源代码安装 http://anyremote.sourceforge.net/pre.html 安装  -xtest apt-get install libxtst-dev 安装 glib sudo ...

  4. ListBox之类控件的Item项显示对象的两个属性

    wpf项目中,ListBox绑定对象集合,ListBoxItem要显示对象的两个属性,例如:显示员工的工号和姓名. 之前我的做法是在Employee员工类中添加一个"NumAndName&q ...

  5. Hibernate 系列教程13-继承-鉴别器与内连接相结合

    Employee public class Employee { private Long id; private String name; HourlyEmployee public class H ...

  6. CodeForces 567B Berland National Library hdu-5477 A Sweet Journey

    这类题一个操作增加多少,一个操作减少多少,求最少刚开始为多少,在中途不会出现负值,模拟一遍,用一个数记下最大的即可 #include<cstdio> #include<cstring ...

  7. java基础增强

    Eclipse使用: java Compile配置的是java编译环境 java Build path配置的是java运行环境 运行环境的版本必须高于编译环境的版本.否则报错 工程上 右键--prop ...

  8. (一)Python之猜数游戏

    猜数游戏由简如深的编码学习过程: 3次机会: print('------------------我爱鱼C工作室------------------')count=0while count < 3 ...

  9. Android中ViewPager如何设置不能通过屏幕左右滑动来切换页面

    //很多时候,我想禁止用户通过屏幕的左右滑动来切换界面!如何实现! //创建一个类继承viewpager,实现 onTouchEvent   和   onInterceptTouchEvent方法,都 ...

  10. ubuntu 解压 windows 生成的 zip 文件乱码问题

    在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 有两种方式解决问题:(建议采用方法 ...