题目1481:Is It A Tree?

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.

There is exactly one node, called the root, to which no directed edges point. 
Every node except the root has exactly one edge pointing to it. 
There is a unique sequence of directed edges from the root to each node. 
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.


In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.

注意空树的情况,wa了好几次,不认真审题的后果。。。

(1)判定节点的入度,只能为0或者1;//排除上图的第三种情况

(2)入度为0的节点只有一个;

(3)既然是一棵树,必须是有向无换图,由拓扑排序思想解之。

注意到入度为0的节点只有一个,所以可保证联通分量的唯一性。

由此开始coding。。。。。。

// 题目3:Is It A Tree.cpp: 主项目文件。

//#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const int N=10003;
int degrees[N];
int used[N];
vector<int> ivec1[N];
queue<int> Q;
int n;

void init()
{
	n=0;
	memset(used,0,sizeof(used));
	for(int i=0;i<N;i++)
	{
		degrees[i]=0;
		ivec1[i].clear();
	}
}

bool isTree()
{
	while(!Q.empty())
		Q.pop();
	for(int i=0;i<N;i++)
	{
		if(used[i])
		{
			if(degrees[i]==0)
				Q.push(i);
			else if(degrees[i]>1)
				return false;//树的入度只允许为1
		}		
	}
	if(Q.size()>1)//树的根只有一个
		return false;
	int cnt=0;
	while(!Q.empty())
	{
		int temp=Q.front();
		Q.pop();
		cnt++;
		for(vector<int>::iterator ite=ivec1[temp].begin();ite!=ivec1[temp].end();ite++)
		{
			degrees[*ite]--;
			if(degrees[*ite]==0)
				Q.push(*ite);
		}
	}
	if(cnt==n)
		return true;
	else
		return false;
}

int main()
{
	int from,to;
	int caseCnt=1;
	while(~scanf("%d%d",&from,&to))
	{
		if(from==0&&to==0)
		{
			printf("Case %d is a tree.\n",caseCnt++);
			continue;
		}
		if(from<0&&to<0)
			break;
		init();
		degrees[to]++;
		ivec1[from].push_back(to);
		if(!used[from])
		{
			used[from]=true;
			n++;
		}
		if(!used[to])
		{
			used[to]=true;
			n++;
		}
		while(scanf("%d%d",&from,&to))
		{
			if(from==0&&to==0)
				break;
			degrees[to]++;
			ivec1[from].push_back(to);
			if(!used[from])
			{
				used[from]=true;
				n++;
			}
			if(!used[to])
			{
				used[to]=true;
				n++;
			}
		}
		bool flag=isTree();
		if(flag)
			printf("Case %d is a tree.\n",caseCnt++);
		else
			printf("Case %d is not a tree.\n",caseCnt++);
	}
	return 0;
}


A-4 Is It A Tree(c++题目,代码不要有注释) 分数 30 作者 陈越 单位 浙江大学 By definition, a tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root; and zero or more nonempty (sub)trees, each of whose roots are connected by a directed edge from r. In other words, a tree is a spacial case of a directed graph. Given a directed graph, your job is to tell whether or not it is a tree. Input Specification: Each input file contains one test case, which starts from a line containing a positive integer n (≤10 4 ), the number of nodes. Hence we assume that all the nodes are numbered from 1 to n. Then several lines follow, each describes a directed edge in the format source destination where both are the nodes' indices. It is guaranteed that there are no more than 10 4 edges, and that source is never the same as destination. The input ends with source being zero, and that line must not be processed. Note: duplicated edges are counted as ONE edge (as shown by the first sample). Output Specification: If the given graph is a tree, out put in a line yes root, where root is the index of the root. Or if not, output no k where k is the number of nodes with 0 indegree. Sample Input 1: 7 2 1 2 1 4 3 4 2 6 5 6 4 6 7 0 Sample Output 1: yes 6 Sample Input 2: 7 2 1 4 3 4 2 6 4 6 7 5 3 0 Sample Output 2: no 2 鸣谢柳汀洲补充数据! 代码长度限制 16 KB Java (javac) 时间限制 800 ms 内存限制 256 MB 其他编译器 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB
最新发布
08-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值