Sicily 1403. Caterpillar

1403. Caterpillar

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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 n 1 n 2 indicating an undirected edge between nodes n 1 and n 2. 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.

// Problem#: 1403
// Submission#: 3241342
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
#include <set>
using namespace std;

const int MAX_N = 105;

int N, E;
bool G[MAX_N][MAX_N];
bool isCyclic;
bool isConnected;
int visit[MAX_N];
int father[MAX_N];

void dfsVisit(int node) {
    visit[node] = 1;
    for (int i = 1; i <= N; i++) {
        if (i != node && G[node][i]) {
            if (visit[i] == 1 && i != father[node]) {
                isCyclic = true;
                return;
            } else if (visit[i] == 0) {
                father[i] = node;
                dfsVisit(i);
            }
        }
    }
    visit[node] = 2;
}

void dfsJudgeIsCyclic() {
    for (int i = 1; i <= N; visit[i++] = 0);
    for (int i = 1; i <= N; father[i++] = -1);
    for (int i = 1; i <= N; i++) {
        if (visit[i] == 0) {
            dfsVisit(i);
        }
    }
}

void dfsJudgeIsConnected() {
    for (int i = 1; i <= N; visit[i++] = 0);
    queue<int> q;
    q.push(1);
    visit[1] = 1;
    int counter = 0;
    while (!q.empty()) {
        int node = q.front();
        q.pop();
        counter++;
        for (int i = 1; i <= N; i++) {
            if (G[node][i] && !visit[i]) {
                q.push(i);
                visit[i] = 1;
            }
        }
    }
    if (counter == N) isConnected = true;
}

int main() {

    std::ios::sync_with_stdio(false);

    int counter = 0;

    while (1) {
        cin >> N;
        if (N == 0) break;
        counter++;
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= N; j++) {
                G[i][j] = false;
            }
        }
        cin >> E;
        for (int i = 0; i < E; i++) {
            int n1, n2;
            cin >> n1 >> n2;
            G[n1][n2] = G[n2][n1] = true;
        }
        isConnected = isCyclic = false;
        dfsJudgeIsCyclic();
        dfsJudgeIsConnected();
        if (!isConnected || isCyclic) {
            cout << "Graph " << counter << " is not a caterpillar." << endl;
            continue;
        }

        int aloneNode[MAX_N];
        for (int i = 1; i <= N; aloneNode[i++] = 0);
        for (int i = 1; i <= N; i++) {
            int c = 0;
            int cNode;
            for (int j = 1; j <= N; j++) {
                if (G[i][j]) {
                    c++;
                    cNode = j;
                }
            }
            if (c == 1) aloneNode[i] = cNode;
        }
        for (int i = 1; i <= N; i++) {
            if (aloneNode[i] != 0) {
                G[i][aloneNode[i]] = G[aloneNode[i]][i] = false;
            }
        }
        bool isCaterpillar = true;
        for (int i = 1; i <= N; i++) {
            if (aloneNode[i] == 0) {
                int c = 0;
                for (int j = 1; j <= N; j++) {
                    if (G[i][j]) c++;
                }
                if (c >= 3) {
                    isCaterpillar = false;
                    break;
                }
            }
        }
        if (isCaterpillar) cout << "Graph " << counter << " is a caterpillar." << endl;
        else cout << "Graph " << counter << " is not a caterpillar." << endl;
    }

    //getchar();
    //getchar();
    
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值