【poj解题】1308
判断一个图是否是一个树,树满足一下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的更多相关文章
- POJ解题经验交流
感谢范意凯.陈申奥.庞可.杭业晟.王飞飏.周俊豪.沈逸轩等同学的收集整理. 题号:1003 Hangover求1/2+1/3+...1/n的和,问需多少项的和能超过给定的值 类似于Zerojudg ...
- 【poj解题】1028
stack的应用 #include<iostream> #include<stack> #include<stdio.h> #include<stdlib.h ...
- 【poj解题】3664
简单,两次排序 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 500 ...
- 【poj解题】3663
排序, 遍历,需要裁减 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX ...
- [poj解题]1017
Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41014 Accepted: 13776 Descrip ...
- 洛谷 P1731 [NOI1999]生日蛋糕 && POJ 1190 生日蛋糕
题目传送门(洛谷) OR 题目传送门(POJ) 解题思路: 一道搜索题,暴力思路比较容易想出来,但是这道题不剪枝肯定会TLE.所以这道题难点在于如何剪枝. 1.如果当前状态答案已经比我们以前某个状态 ...
- POJ 1308 Is It A Tree? 解题报告
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32052 Accepted: 10876 D ...
- POJ 1308
http://poj.org/problem?id=1308 题意:判断这是不是一棵树. 思路:这个题还是有部分坑点的.首先空树也是一棵树,还有森林不是树. 关于森林的判断我是利用并查集把每一个点压缩 ...
- POJ 1308&&HDU 1272 并查集判断图
HDU 1272 I - 小希的迷宫 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- CSS概要
CSS概要 laiqun@msn.cn Contents 1. css的引入 2. css的选择器及效果图 3. css 盒模型 4. css 浮动 4.1. 浮动的作用: 4.2. 浮动的影响: 5 ...
- PHP的抽象类、接口类的区别和选择【转载】
本文转自:http://blog.csdn.net/fanteathy/article/details/7309966 区别: 1.对接口的使用是通过关键字implements.对抽象类的使用是通过关 ...
- C#判断文字是否为汉字
/// <summary> /// 检测一个字符串是不是以汉字开始 /// </summary> /// <param name="str">要 ...
- JNI调用问题(部分机型崩溃)
1.今日测试发现在部分手机上游戏会崩溃,通过logcat日志发现是jni调用问题(我猜测) 错误日志中有如下语句: trying to work around app JNI bugs, but di ...
- Hibernate 系列教程1-枚举单例类
你还在为不知道怎样正确使用Hibernate而纠结吗 你还在为不知道怎样配置映射文件而郁闷吗 枚举单例(Enum Singleton) 是实现单例模式的一种方式而已,不过写法简单,创建枚举默认也是线程 ...
- 织梦DEDECMS更新6月7日补丁后出现版权链接的删除办法
说最近网站底部多出了一个Power by DedeCms的超链接版权信息,但是该网友最近只是升级了0607更新补丁,其它的源代码并没有改动,而以前的文章页面并没有,看来是这次更新dedecms后才被加 ...
- php 版本比较
判断当前运行的 PHP 版本是否高于或等于你提供的版本号. function is_php($version) { static $_is_php; $version = (string) $vers ...
- Swift 学习笔记(五)
126. 协议(Protocols) 协议语法(Protocol Syntax) 属性要求(Property Requirements) 方法要求(Method Requirements) Mutat ...
- Issue 5158: Modal dialog present (UnexpectedAlertOpen) issue in IE (Similar issue like 3360)
https://code.google.com/p/selenium/issues/detail?id=5158 Reported by mailtopa...@gmail.com, Feb 13 ...
- hdu_5179_beautiful number(数位DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 题意:给你一个范围,问你漂亮的数有多少个,漂亮的数的定义为 数位高的比数位低的大,并且 数位高的 ...