poj 3310 Caterpillar(树的直径)

Caterpillar


Time Limit: 2000MS		Memory Limit: 65536K
Total Submissions: 2897		Accepted: 1319

Description

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.

Source

East Central North America 2006

题意

一个图无向图叫做毛虫(caterpillar )需要满足的条件是:

  • 连通;
  • 无环;
  • 除在路径上的点外,其他点都与路径上的点相邻两点相邻(图中有一条路径,其中每个节点都在该路径上或该路径上节点的邻居)

要判断一个图是否是毛虫。

思路

  • 连通性好判断,dfs一遍看所有点是否被遍历到即可。
  • 无环性的判断分成两部分:
  1. 输入边时判断是否有点数<=2的环;
  2. 对于点数>2的环dfs(or bfs)判断(根据遍历层序,再次遍历到访问过的点时计算中间经过了多少个点)即可。
  • 对于最后一个条件:
    树的一条直径,同时要记录哪些点在直径上。然后判断直径上的点和直接与直径上的点相连的点数个数是否等于总点数即可。

代码

#include <iostream>
#include <vector>
#include <set>
#define PII pair<int,int>
using namespace std;
const int N = 305;
vector<PII>vec[N];
bool vis[N];
bool link[N][N];
int cnt[N];
int st[N];
int n,m,total;
int maxx,pos1,pos2;
bool connect_flag,circle_flag;

// 连通性,点数大于2的环
void dfs1(int u,int fa,int deep)
{
    int iSize = vec[u].size();
    for(int i = 0; i < iSize; i++)
    {
        int to = vec[u][i].first;
        //判断是否存在点数大于2的环
        if(!circle_flag && vis[to])
        {
            circle_flag = deep+1-cnt[to] > 2;
        }
        if(!vis[to])
        {
            vis[to] = true;
            cnt[to] = cnt[u] + 1;
            dfs1(to,u,deep+1);
        }
    }
    return;
}
/*
7 7
1 3 1 2 1 6 6 7 2 4 2 5 4 5
*/
//直径的一个端点
void dfs2(int u,int fa,int dis)
{
    if(dis > maxx)
    {
        pos1 = u;
        maxx = dis;
    }
    int iSize = vec[u].size();
    for(int i = 0; i < iSize; i++)
    {
        int to = vec[u][i].first;
        int val = vec[u][i].second;
        if(to != fa)
        {
            dfs2(to,u,dis+val);
        }
    }
    return;
}
//直径的另一个端点并记录路径
void dfs3(int u,int fa,int dis,int idx)
{
    if(dis > maxx)
    {
        pos1 = u;
        //记录直径所经过的点
        st[idx] = u;
        total = idx;
        maxx = dis;
    }
    int iSize = vec[u].size();
    for(int i = 0; i < iSize; i++)
    {
        int to = vec[u][i].first;
        int val = vec[u][i].second;
        if(to != fa)
        {
            dfs3(to,u,dis+val,idx+1);
        }
    }
    return;
}
//初始化
void init()
{
    connect_flag = true;
    circle_flag = false;
    for(int i = 1; i <= n; i++)
    {
        cnt[i] = 0;
        vis[i] = false;
        vec[i].clear();
        for(int j = 1; j <= n; j++)
        {
            link[i][j] = false;
        }
    }
}
int main()
{
    int id = 0;
    while(cin>>n)
    {
        if(!n) break;
        cin>>m;
        id++;
        init();
        while(m--)
        {
            int x,y;
            cin>>x>>y;
            if(circle_flag) continue;
            //自环与2元环
            if(x==y || link[x][y] || link[y][x])
            {
                circle_flag = true;
            }
            link[x][y] = link[y][x] = true;
            vec[x].push_back(make_pair(y,1));
            vec[y].push_back(make_pair(x,1));
        }
        cnt[1] = 1;
        vis[1] = true;
        dfs1(1,1,1);
        if(circle_flag)
        {
            cout<<"Graph "<<id<<" is not a caterpillar."<<endl;
            continue;
        }
        for(int i = 1; i <= n; i++)
        {
            if(!vis[i])
            {
                connect_flag = false;
                break;
            }
        }
        if(!connect_flag)
        {
            cout<<"Graph "<<id<<" is not a caterpillar."<<endl;
            continue;
        }
        maxx = 0;
        dfs2(1,0,0);
        pos2 = pos1;
        maxx = -1;
        dfs3(pos1,0,0,1);
        //记录直径及与直径上点相邻的点的总个数
        set<int>s;
        for(int i = 1; i <= total; i++)
        {
            s.insert(st[i]);
            int iSize = vec[st[i]].size();
            for(int j = 0; j < iSize; j++)
            {
                int to = vec[st[i]][j].first;
                s.insert(to);
            }
        }
        if(int(s.size())==n)
            cout<<"Graph "<<id<<" is a caterpillar."<<endl;
        else
            cout<<"Graph "<<id<<" is not a caterpillar."<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leo Bliss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值