Caterpillar
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.
第一步,所谓的毛毛虫必须是树,建立图,并查集查是否有环,有则continue;
第二步,从一点出发,bfs求其到其他点距离,若有达不到的点,continue;
第三步,求出从一点出发的最远的点,再次bfs求其到其他点距离;
第四步,求出最远的点,即树的直径的两个端点都已经得出,回溯法标记其路径当中的每一个点;
第五步,标记的每一个点距离为一的点,同样标记;
第六步,若每一个点都被标记过,则为毛毛虫。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <string>
using namespace std;
// for(i=1;i<=t;i++)
// scanf("%d%d%d",&v,&r,&c);
// printf("%d",maxx);
int mapp[110][110],dis[110],flag[110];
int tree[110];
int root(int jie)
{
if(tree[jie]==jie)
return jie;
else
return tree[jie]=root(tree[jie]);
}
int main()
{
int i,j,k,n,e,temp,key,ccc=0,maxx;
while(1)
{
ccc++;
scanf("%d",&n);
if(n==0) break;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
mapp[i][j]=0;
memset(dis,0,sizeof(dis));
memset(flag,0,sizeof(flag));
scanf("%d",&e);
key=0;
for(i=1;i<=n;i++)
tree[i]=i;
for(i=1;i<=e;i++)
{
scanf("%d%d",&j,&k);
mapp[j][k]=1;
mapp[k][j]=1;
j=root(j);
k=root(k);
if(j!=k)
{
tree[j]=k;
}
else
{
key=1;
}
}
if(key==1)
{
printf("Graph %d is not a caterpillar.\n",ccc);
continue;
}
queue<int>q;
flag[1]=1;
dis[1]=1;
q.push(1);
while(q.empty()!=true)
{
temp=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(flag[i]==0&&mapp[temp][i]==1)
{
dis[i]=dis[temp]+1;
q.push(i);
flag[i]=1;
}
}
}
maxx=0;
for(i=1;i<=n;i++)
{
if(dis[i]==0)
key=1;
if(dis[i]>maxx)
{
maxx=dis[i];
temp=i;
}
}
if(key==1)
{
printf("Graph %d is not a caterpillar.\n",ccc);
continue;
}
memset(dis,0,sizeof(dis));
memset(flag,0,sizeof(flag));
flag[temp]=1;
dis[temp]=1;
q.push(temp);
while(q.empty()!=true)
{
temp=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(flag[i]==0&&mapp[temp][i]==1)
{
dis[i]=dis[temp]+1;
q.push(i);
flag[i]=1;
}
}
}
maxx=0;
for(i=1;i<=n;i++)
{
if(dis[i]>maxx)
{
maxx=dis[i];
temp=i;
}
}
memset(flag,0,sizeof(flag));
flag[temp]=1;
q.push(temp);
while(q.empty()!=true)
{
temp=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(flag[i]==0&&mapp[temp][i]==1)
{
if(dis[i]==dis[temp]-1)
{
q.push(i);
flag[i]=1;
}
}
}
dis[temp]=0;
}
for(i=1;i<=n;i++)
{
if(dis[i]==0)
{
q.push(i);
}
}
while(q.empty()!=true)
{
temp=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(mapp[temp][i]==1)
{
dis[i]=0;
}
}
}
key=0;
for(i=1;i<=n;i++)
{
if(dis[i]!=0)
{
key=1;
}
}
if(key==1)
{
printf("Graph %d is not a caterpillar.\n",ccc);
}
else
{
printf("Graph %d is a caterpillar.\n",ccc);
}
}
return 0;
}