判断一个图是否是一个树,树满足一下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. unable to connect to :5555

    有可能批处理文件用的adb和eclipse的adb不兼容.把你的批处理文件用的adb换成eclipse的adb就可以了: 运行结果:

  2. Android onTouchEvent方法

    onTouchEvent方法简介 前面已经介绍了手机键盘事件的处理方法,接下来将介绍手机屏幕事件的处理方法onTouchEvent.该方法在View类中的定义,并且所有的View子类全部重写了该方法, ...

  3. profile和bash

    24down votefavorite 12 I am reading about basic shell scripting from Linux Command Line and Shell Sc ...

  4. MySQL 异常错误码使用 及 对照表

    DECLARE CONTINUE HANDLER FOR SQLWARNING, SQLEXCEPTION, NOT FOUND BEGIN GET DIAGNOSTICS CONDITION cod ...

  5. php redis 消息队列

    redis是什么东西就不多说了,网上文章一搜一大堆. 首先来说一下我要实现的功能: 类似一个消息中转站吧,如果有人要发送消息,先将消息发到我这里来,然后我这边进行转发,为的就是有一个统一的管理和修改时 ...

  6. WPF(x:Null 使用)

    <Window x:Class="TestOfNull.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  7. Hibernate 系列教程17-查询缓存

    在二级缓存配置成功的基础上进行查询缓存配置 Product public class Product { private Long id; private String name; Product.h ...

  8. IIS访问PHP文件时,弹出用户名和密码提示框的解决方法

    找了一圈,以下的方法解决了IIS访问PHP弹用户名和密码提示框问题. 解决方法:给PHP安装目录everyone读取权限 这样不知道会不会出现安全问题,请大家谨慎.

  9. Android自定义XML属性

    <?xml version="1.0" encoding="utf-8"?> <resources> <declare-style ...

  10. 在win7/8/10鼠标右键添加“管理员取得所有权”

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\Shell\TakeAuthority]"icon"=" ...