# poj 3310 Caterpillar (树的直径+并查集判环+dfs判断连通性)

Posted by 111qqz on Wednesday, July 13, 2016

## TOC

poj3310 题目链接

``````/* ***********************************************
Author :111qqz
Created Time :2016年07月12日 星期二 20时27分28秒
File Name :code/poj/3310.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=305;
int n,m;
vector < int >edge[N];
int f[N];
bool vis[N];
bool die;
int d[N];
int to[N];
int pre[N];//记录最长的路径。。。
int lst,beg;
bool ok[N];
struct Edge
{
int u,v;
}e[N];
void init()
{
ms(to,-1);
ms(pre,-1);
ms(vis,false); //for dfs
ms(ok,false);
for ( int i = 1 ; i <= n ; i++) f[i] = i;
for ( int i = 1 ; i <= n ; i++) edge[i].clear();
}
int root ( int x)
{
if (x!=f[x]) f[x] = root (f[x]);
return f[x];
}
void merge( int x,int y)
{
int rx = root(x);
int ry = root(y);
if (rx==ry) return ;
f[rx] = ry;
}
void dfs( int u)
{
vis[u] = true;
int siz = edge[u].size();
for ( int i = 0 ; i < siz ; i++)
{
int v = edge[u][i];
if (vis[v]) continue;
dfs(v);
}
}
void bfs( int s)
{
ms(d,0);
ms(vis,false);
ms(pre,-1);
queue<int>q;
q.push(s);
vis[s] = true;
while (!q.empty())
{
int u = q.front();q.pop();
int siz = edge[u].size();
for ( int i = 0 ; i < siz ; i++)
{
int v = edge[u][i];
if (vis[v]) continue;
//  cout<<" u:"<<u<<" v:"<<v<<endl;
d[v] = d[u] + 1;
pre[v] = u;
vis[v] = true;
q.push(v);
}
}
}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif
int cas = 0 ;
while (scanf("%d",&n)!=EOF)
{
if (n==0) break;
scanf("%d",&m);
init();
die = false;
for ( int i =1 ; i <= m ; i++)
{
int u,v;
scanf("%d%d",&u,&v);
//	    cout<<"u:"<<u<<" v:"<<v<<endl;
e[i].u =u;
e[i].v= v;
to[u] = v;
to[v] = u;
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(1); //判断连通性
//	cout<<"dfs die?"<<endl;
for ( int i = 1 ; i <= n ; i++)
{
if (!vis[i])
{
die = true;
break;
}
}
if (die)
{
printf("Graph %d is not a caterpillar.\n",++cas);
continue;
}
for ( int i = 1 ; i <= m ; i++)
{
int u = e[i].u;
int v = e[i].v;
if (root(u)==root(v))   //判环
{
die = true;
continue;
}
merge(u,v);
}
//	cout<<"union set die?"<<endl;
if (die)
{
printf("Graph %d is not a caterpillar.\n",++cas);
continue;
}
bfs(1);
//	for ( int i = 1 ;i  <= n ; i++) cout<<" pre[i]:"<<pre[i]<<endl;
int mx = 0 ;
for ( int i = 1 ; i <= n ; i++)
{
if (d[i]>mx)
{
mx = d[i];
lst = i;
}
}
bfs(lst);
//	cout<<"bfs die?"<<endl;
mx = 0 ;
for ( int i = 1 ; i <= n ; i++)
{
if (d[i]>mx)
{
mx = d[i];
beg = i ;
}
}
int x = beg;
//	for ( int i = 1 ;i  <= n ; i++) cout<<" pre[i]:"<<pre[i]<<endl;
while(x!=-1)
{
//   cout<<"x:"<<x<<endl;
ok[x] = true;
x = pre[x];
}
//	cout<<"get pre die?"<<endl;
for ( int i = 1 ; i <= n ; i++)
{
if (ok[i]||ok[to[i]]) continue;
//	    cout<<"i:"<<i<<endl;
die = true;
break;
}
if (die)
{
printf("Graph %d is not a caterpillar.\n",++cas);
continue;
}
printf("Graph %d is a caterpillar.\n",++cas);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````

「真诚赞赏，手留余香」