POJ 3310 Caterpillar (BFS + DFS + 并查集)

来源:互联网 发布:网络尖兵 编辑:程序博客网 时间:2024/05/23 19:20

Caterpillar
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 2289 Accepted: 1074

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 containingn indicating the number of nodes, numbered 1 through n (a value ofn = 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 nodesn1 and n1. This information may span multiple lines. You may assume thatn ≤ 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

22211 2 2 3 2 4 2 5 2 6 6 7 6 10 10 8 9 10 10 12 11 12 12 13 12 1718 17 15 17 15 14 16 15 17 20 20 21 20 22 20 1916151 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 150

Sample Output

Graph 1 is not a caterpillar.Graph 2 is a caterpillar.

Source

East Central North America 2006

这道题目真是恶心到自己了!
也怪自己想不到好的方法!
题意:
给你一个无向图,判断这个无向图是否存在一条主干路,使得所有的点不是在干路上,就是在干路上的枝叶上!

思路:
随便记录一下吧,毕竟写的太麻烦了! = =!
因为主干路肯定是最长的,所以用bfs 寻找最长路,一个dfs1 来判断这个干路合不合适,即枝叶的上的点会不会太远!
一个dfs2  来判断是否有环的存在,  一个并查集来判断是否有孤立的点。
其实很多内容都可以合并的,时间太紧促了  脑子短路了= = !
其实数据量很小的,随便写就可以了!
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<cstdlib>#include<vector>#include<iostream>#include<set>#include<map>#include<stack>#include<string>#include<queue>using namespace std;const double eps = 1e-10;const int inf = 0x3f3f3f3f;const double pi = acos(-1.0);typedef long long ll;typedef unsigned ULL;struct Point{    double x,y;    Point(double x = 0,double y = 0):x(x),y(y){}    bool operator < (const Point& rhs) const {        return x < rhs.x || (fabs(x- rhs.x) < eps && y < rhs.y);    }    bool operator == (const Point & rhs) const {        return fabs(x - rhs.x) < eps && fabs(y - rhs.y) < eps;    }};typedef Point Vector;Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y + B.y); }Vector operator - (Vector A,Vector B) { return Vector(A.x-B.x,A.y - B.y); }Vector operator * (Vector A,double p) { return Vector(A.x*p,A.y*p); }Vector operator / (Vector A,double p) { return Vector(A.x/p,A.y/p); }double Cross(Vector A,Vector B) { return A.x*B.y - B.x*A.y; }Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){    Vector u = P-Q;    double t = Cross(w, u) / Cross(v, w);    return P + v*t;}int la[107];vector<int>g[107];int G[107][107];int root[107];int visroot[107];int cnt= 0;int vis[107];queue<int>q;bool bfs(int s){    memset(visroot,0,sizeof visroot);    memset(vis,0,sizeof vis);    vis[s] = 1;    cnt = 0;    while(!q.empty())q.pop();    q.push(s);    la[s] = -1;    int laem;    while(!q.empty()){        int u = q.front();        q.pop();        int len = g[u].size();        for (int i = 0; i < len; ++i){            int x = g[u][i];            if (vis[x] == 1)continue;            laem = x;            vis[x] = 1;            q.push(x);            la[x] = u;        }    }    root[cnt++] = laem;    visroot[laem] = 1;    memset(vis,0,sizeof vis);    while(la[laem] != -1){        laem = la[laem];        root[cnt++] = laem;        visroot[laem] = 1;    }}int dfs2(int cur,int dis){    if (dis > 1)return 0;    int len = g[cur].size();    for (int i = 0; i < len ;++i){        int x = g[cur][i];        if (vis[x])continue;        vis[x] = 1;        if (visroot[x])continue;        if (dfs2(x,dis+1) == 0)return 0;;    }    return 1;}bool dfs(int fa,int cur){    int len = g[cur].size();    for (int i = 0; i < len ;++i){        int x = g[cur][i];        if (vis[x]){            continue;        }        vis[x] = 1;        if (!dfs(cur,x))return false;        else return true;    }    if (len == 1)return true;    return false;}int fa[107];int find(int x){    return fa[x] == x ? x : fa[x] = find(fa[x]);}int main(){    int n,kase = 0;    while(scanf("%d",&n) == 1 && n){        int m;        for (int i = 1; i<= n; ++i)fa[i] = i;        memset(G,0,sizeof G);//        memset(vis,0,sizeof vis);//        memset(visroot,0,sizeof visroot);        for (int i = 0; i < 107; ++i)g[i].clear();        scanf("%d",&m);//        cnt = 0;        for (int i = 0; i < m; ++i){            int u,v;            scanf("%d%d",&u,&v);            int xx = find(u);            int yy = find(v);            if (xx != yy){                fa[xx] = yy;            }            G[u][v] = G[v][u] = 1;            g[u].push_back(v);            g[v].push_back(u);        }        // ===============================        bool okk = true;        for (int i = 1; i <= n; ++i){            if (find(i) != find(1)){                okk  = 0 ;                break;            }        }        if (!okk){            printf("Graph %d is not a caterpillar.\n",++kase);            continue;        }         memset(vis,0,sizeof vis);        if (!dfs(-1,1)){            printf("Graph %d is not a caterpillar.\n",++kase);            continue;        }        //===============================//        if (g[1].size() == 0){//            printf("Graph %d is not a caterpillar.\n",++kase);//            continue;//        }        bool ok = false;        for (int i = 1; i<= n; ++i){            bfs(i);            int ok2 =1;            for (int i = cnt-1; i >= 0; --i){                memset(vis,0,sizeof vis);                if (dfs2(root[i],0) == 0){                    ok2 = 0;;                    break;                }            }            if (ok2){                ok = true;                break;            }        }        if (!ok){            printf("Graph %d is not a caterpillar.\n",++kase);            continue;        }        else printf("Graph %d is a caterpillar.\n",++kase);    }    return 0;}


0 0