Caterpillar (图论 & 思维)

#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.

题目链接
题目大体意思就是给你图,然后这个图要求要是一棵树,并且这棵树有一条主干,不在主干上的点必须以悬挂点的形式挂在主干上,不能挂在在主干的分支上,也就是在分支上不能出现小的分支。这个当时看了以后,先不说看不懂题意,问了队友题意之后还是一脸懵逼,这要怎么做?后来队友给我发了个题解的代码,一下子就被折服了,太强了。
题解的主要思想是:根据题目要求,我们的树不能出现多重分支。也就是说你如果把在这棵树挂在主干上的悬挂点全部都去掉的话,那么就只剩了一条主干,不可能存在其他分支。并且这个主干的两边一定原来连了许多悬挂边,后来成为了主干的两侧,也变成了悬挂边,这样我们记录一下有这样变化的点或者边,如果大于2就说明除了两侧,还有其他分支,是不符合题意的。用两个数组做记录,剩下的就是判断是不是一棵树了,用并查集就可以了。

AC代码:

#include <cstdio>
#include <cstring>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 300 + 10;

struct node
{
    int idx,step;
};

int n, m, f[maxn], recin[maxn] = {0}, recdeg[maxn] = {0};
vector<int> mp[maxn];

int Find(int x)
{
    return x == f[x] ? x : f[x] = Find(f[x]);
}

int cal()
{
    int i,j;
    //把悬挂点都去掉
    for(int i = 1; i <= n; i++)
        if(recin[i] == 1)
            for(vector<int>::iterator it = mp[i].begin(); it != mp[i].end(); it++)
                recin[*it]--;

    //因为一棵树并且不能有二重分支,那么除了主干一定都是悬挂边,所以经过上面把悬挂边都去掉,应该剩一条主干
    //只有主干的两侧变成了悬挂点,所以如果还有其他的点上的悬挂边数量超过了一,去掉悬挂边以后剩了一个悬挂点,
    //而且又不是主干的两侧,一定是不符合题目条件的
    int cnt = 0;
    for(int i = 1; i <= n; i++)
        if(recdeg[i] > 1 && recin[i] == 1)
            cnt++;
    if(cnt > 2) return false;
    else    return true;
}
int main()
{
    int t = 1;
    while(~scanf("%d", &n), n)
    {
        memset(recin, 0, sizeof(recin));
        memset(recdeg, 0, sizeof(recdeg));
        int flag = 1;
        for(int i=1;i<=n;i++)   mp[i].clear();
        for(int i = 0; i <= n; i++) f[i] = i;;
        scanf("%d", &m);
        for(int i = 0; i < m; i++)
        {
            int t1, t2;
            scanf("%d%d", &t1, &t2);
            mp[t1].push_back(t2);   mp[t2].push_back(t1);
            recin[t1]++;    recin[t2]++;
            recdeg[t1]++;   recdeg[t2]++;
            int x = Find(t1), y = Find(t2);
            if(x != y)    f[y] = x;
            else if(flag)
            {
                printf("Graph %d is not a caterpillar.\n", t++);
                flag = 0;
            }
        }
        if(flag)
        {
            int num = 0;
            for(int i = 1; i <= n; i++)
                if(f[i] == i)  num++;
            if(num != 1)
            {
                printf("Graph %d is not a caterpillar.\n", t++);
                flag = 0;
            }
            else if(!cal())
            {
                    printf("Graph %d is not a caterpillar.\n",t++);
                    flag = 0;
            }
            if(flag)    printf("Graph %d is a caterpillar.\n",t++);
        }
    }
    return 0;
}

这也算是个图论中的思维题吧,个人感觉,因为我想不出来,很笨……
希望对大家有帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值