判断一个图是否是一个树,树满足一下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. python3.5 + django1.9.1+mysql

    python3 对mysql 的驱动不再是mysqldb 具体步骤 : 1 安装依赖 pip install PyMySQL 2 修改配置 __init__.py import pymysql pym ...

  2. Linux 任务控制(bg job fg nohup &)

    一. 简介     Linux/Unix 区别于微软平台最大的优点就是真正的多用户,多任务.因此在任务管理上也有别具特色的管理思想.我们知道,在 Windows 上面,我们要么让一个程序作为服务在后台 ...

  3. ip地址的网络配置

    记录一下linux下的网络配置 3.执行命令(通过ifconfig查一下网卡): vi /etc/sysconfig/network-scripts/ifcfg-eth2 注:按字母a,代表插入. 编 ...

  4. junit 单元测试 - 参数化测试

    junit4.x版本需要引入如下jar包: hamcrest-core-1.3.jar junit-4.12-beta-3.jar 新建一个计算器类,如下: package com.pt; publi ...

  5. http & json

    TCP(传输层协议) (1) 面向连接  (2) 可靠的  (3) 基于字节流的   连接建立阶段: 客户端 ------->SYN                 服务端(服务器被动打开) 客 ...

  6. Anton and Making Potions

    Anton and Making Potions time limit per test 4 seconds memory limit per test 256 megabytes input sta ...

  7. Hibernate 系列教程11-继承-Single Table策略

    Single Table策略 通过 discriminator鉴别器来区分是父类还是子类 Employee public class Employee { private Long id; priva ...

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

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

  9. ffmpeg合并多个视频

    实例1: 把4个视频(1.f4v, 2.f4v, 3.f4v, 4.f4v)合并成一个文件(out.mp4) 基本无损,而且速度飞快! #-vcodec copy -acodec copy   == ...

  10. HDU - 2502 Subway

    题目链接:http://poj.org/problem?id=2502 分析: 告诉一些地铁线路,从起点到终点,中途可以步行,可以坐地铁,找一条最短的路 主要是把图建立好,然后直接dijkstra或者 ...