POJ 3310 Caterpillar (BFS + DFS + 并查集)
来源:互联网 发布:网络尖兵 编辑:程序博客网 时间:2024/05/23 19:20
Caterpillar
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
这道题目真是恶心到自己了!
也怪自己想不到好的方法!
题意:
给你一个无向图,判断这个无向图是否存在一条主干路,使得所有的点不是在干路上,就是在干路上的枝叶上!
思路:
随便记录一下吧,毕竟写的太麻烦了! = =!
因为主干路肯定是最长的,所以用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;}
- POJ 3310 Caterpillar (BFS + DFS + 并查集)
- POJ 3310 Caterpillar 并查集+dfs
- Poj 3310 Caterpillar (并查集判环+度)
- hdu 1181 (搜索BFS,深搜DFS,并查集)
- POJ 3310 Caterpillar
- ZOJ3811 - Untrusted Patrol(并查集 或者 bfs dfs)
- DFS和BFS和并查集的 存档
- 【HDU 1198】Farm Irrigation(dfs+并查集+bfs)
- 几道周赛题目(bfs,dfs,kmp,快速幂运算,并查集)
- 图论(一):DFS,BFS,邻接链表,并查集
- 数据结构实验之图论八:欧拉回路(并查集+DFS+BFS)
- GYM 101173 H.Hangar Hurdles(并查集+bfs+dfs)
- ACM基础算法复习(STL + DFS + BFS + 并查集 + 快速幂 + 欧几里得算法)
- 并查集+DFS
- POJ 3310(并查集)
- POJ 3834 Graph Game 博弈 dfs+并查集剪枝
- POJ 1986 LCA 离线算法 dfs+并查集
- poj 1986LCA离线dfs+并查集
- next_day(d1[,c1])
- C++11智能指针简介
- MYSQL_C语言接口
- MP4文件sample读取流程
- NEW_TIME(dt1,c1,c2)
- POJ 3310 Caterpillar (BFS + DFS + 并查集)
- localtimestamp
- 【动态规划】Codeforces 698A & 699C Vacations
- Unity3D-加载XML配置表文件
- last_day(d1)
- INTERVAL c1 set1
- POJ 1990 MooFest
- Linux常用软件安装
- java三大框架之spring事务传播行为