判断一个图是否是一个树,树满足一下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地址bash[转载]

    ipaddr=`/sbin/ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d : -f2 | awk '{print $1}'`

  2. table导出到excel的两种方法

    1. 用table2excel 的js文件,这种方法没有IE兼容性 需要在文件中导入 <script type="text/javascript" src="< ...

  3. RGB888转RGB666

    内存中的数据排列高位在左,低位在右 RGB888->RGB666 高 -------低 B[3]         B[2]          B[1]         B[0]         ...

  4. Java中的一些术语的解释

    一  API(Application Programming Interface,应用程序编程接口) 简单来说,就是其他人开发出来一块程序,你想用,他会告诉你调用哪个函数,给这个函数传什么参数,然后又 ...

  5. 解决oracle数据库连接不上的问题

    今天打开部署好的java开发的网站系统,反应好慢,第一反应就是后台有问题. 查看tomcat一堆的报错信息,重启还是存在. 使用plSql连接数据库看看,登录提示如下:ORA-12514:TNS:监听 ...

  6. oracle-查询执行速度慢的sql

    Oracle 查询每天执行慢的SQL 2014-12-11 18:00:04 分类: Oracle 链接:http://blog.itpub.net/28602568/viewspace-136484 ...

  7. java的两种异常runtimeException和checkedException

    java异常处理机制主要依赖于try,catch,finally,throw,throws五个关键字.   try 关键字后紧跟一个花括号括起来的代码块,简称try块.同理:下面的也被称为相应的块. ...

  8. Struts2值栈详解

    1. 关于值栈: 1). helloWorld 时, ${productName} 读取 productName 值, 实际上该属性并不在 request 等域对象中, 而是从值栈中获取的.  2). ...

  9. PullToRefreshScrollView的上拉加载、下拉刷新

    eclipse中的项目: //注意:此刷新功能是使用的第三方的PullToRefreshScrollView,因此需要导入第三方library作为依赖 步骤:导入第三方library,依赖:点击你的应 ...

  10. CodeForces 525D Arthur and Walls

    广搜.看了官方题解才会的..... 定义2*2的小矩阵,有三个是点,一个是星,这样的小矩阵被称为元素块. 首先把所有元素块压入队列,每次取出对头,检查是否还是元素块,如果是 那么将那个*改为点,否则跳 ...