Caterpillar POJ - 3310(拓扑排序+求树的直径)

An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a path in the graph where every node is either on this path or a neighbor of a node on the path. This path is called the spine of the caterpillar and the spine may not be unique. You are simply going to check graphs to see if they are caterpillars.

For example, the left graph below is not a caterpillar, but the right graph is. One possible spine is
shown by dots.
在这里插入图片描述

Input
There will be multiple test cases. Each test case starts with a line containing n indicating the number of nodes, numbered 1 through n (a value of n = 0 indicates end-of-input). The next line will contain an integer e indicating the number of edges. Starting on the following line will be e pairs n1 n2 indicating an undirected edge between nodes n1 and n1. This information may span multiple lines. You may assume that n ≤ 100 and e ≤ 300. Do not assume that the graphs in the test cases are connected or acyclic.

Output
For each test case generate one line of output. This line should either be

Graph g is a caterpillar.
or
Graph g is not a caterpillar.
as appropriate, where g is the number of the graph, starting at 1.

Sample Input
22
21
1 2 2 3 2 4 2 5 2 6 6 7 6 10 10 8 9 10 10 12 11 12 12 13 12 17
18 17 15 17 15 14 16 15 17 20 20 21 20 22 20 19
16
15
1 2 2 3 5 2 4 2 2 6 6 7 6 8 6 9 9 10 10 12 10 11 10 14 10 13 13 16 13 15
0
Sample Output
Graph 1 is not a caterpillar.
Graph 2 is a caterpillar.

题意:所有的入度不为1的点(与之有边的点不止一个的点)必须在最长路上,并且不存在环和孤点,问给出的图是否满足以上条件。

思路:
无向图是特殊的有向图,将所有点入度-1后求拓扑序列,判断是否存在环和孤点,若不存在,继续判断,dfs求树的直径,看是否等于所有入度不为1的点集。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; 
const int N =505;
int e[N],h[N],ne[N],idx;
int d[N],q[N],vis[N];
int n,m,ans;
void init()
{
	memset(h,-1,sizeof h);
	idx=0;
}
void add(int a,int b)
{
	e[idx]=b;ne[idx]=h[a],h[a]=idx++;
}
bool toposort()  //拓扑排序判断是否存在环和孤点
{
	int hh=0,tt=-1;
	for(int i=1;i<=n;i++)
	{
		if(!d[i])
			q[++tt]=i;
	}
	while(hh<=tt)
	{
		int x=q[hh++];
		for(int i=h[x];i!=-1;i=ne[i])
		{
			int j=e[i];d[j]--;
			if(!d[j])
				q[++tt]=j;
		}
	} 
	return tt==n-1;
}
int dfs(int x,int sum)
{
	int v,flag=0;
	ans=max(ans,sum);
	for(int i=h[x];i!=-1;i=ne[i])
	{
		int j=e[i];
		if(!vis[j])
		{
			vis[j]=1;
			v=dfs(j,sum+1);flag=1;
		}
	}
	return flag?v:x;
}
int main()
{
	int cas=1;
	while(~scanf("%d",&n) && n)
	{
		cin>>m;
		init();
		memset(d,0,sizeof d);
		for(int i=0;i<m;i++)
		{
			int a,b;
			cin>>a>>b;
			d[a]++;d[b]++;
			add(a,b);add(b,a);
		}
		int sum=0;
		for(int i=1;i<=n;i++)
		{
			d[i]--;
			if(d[i])
				sum++;
		}
		memset(vis,0,sizeof vis);
		ans=0;
		vis[1]=1;
		int v=dfs(1,0);
		memset(vis,0,sizeof vis);
		ans=0;
		vis[v]=1;
		dfs(v,0);
		ans--;   //1的原因是最长路的两个端点都是入度为1的点,不算在内
		if(toposort() && ans==sum)
			printf("Graph %d is a caterpillar.\n",cas++);
		else
			printf("Graph %d is not a caterpillar.\n",cas++);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值