POJ 3310 Caterpillar
来源:互联网 发布:mysql集群监控 编辑:程序博客网 时间:2024/05/28 09:32
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 isshown 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.
题解:
读完题目我们知道,题目要求就是找一条路径,满足对于任意路径外的点都可以在路径上找到一个点到它的距离为1。因为路径为1,所以那条路径肯定是树的直径。
又因为n<=100,所以这道题可以直接
还要注意题目要求不能有环,所以一开始还要判环,可以用dfs,也可以用并查集,我用的是并查集
#include<cstdio>#include<cmath>#include<ctime>#include<iomanip>#include<iostream>#include<cstring>#include<string>#include<cctype>#include<algorithm>#define N 105#define M 305#define INF 100000000using namespace std;int n,m,x,y,s,t,num,index,max_dis,tail;int p[105],fa[105],f[105][105];bool flag=false,bz=false,in[105];inline int Readint(){ int i=0,f=1;char ch; for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar()); if(ch=='-') f=-1,ch=getchar(); for(;ch>='0'&&ch<='9';ch=getchar()) i=(i<<1)+(i<<3)+ch-'0'; return i*f;}inline int find(int x){ if(x==fa[x]) return x; fa[x]=find(fa[x]); return fa[x];}inline bool judge(){ int cnt=0; for(int i=1;i<=n;i++){ if(fa[find(i)]==i)cnt++; } return cnt==1;}inline void pre(){ num=max_dis=tail=0; bz=flag=false; memset(in,0,sizeof(in)); memset(p,0,sizeof(p)); for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) f[i][j]=INF;}int main(){// freopen("lx.in","r",stdin); while(scanf("%d",&n) && n!=0){ pre(); m=Readint(); for(int i=1;i<=m;i++){ x=Readint(),y=Readint(); f[x][y]=f[y][x]=1; if(find(x)!=find(y)) fa[find(x)]=find(y);//判环 else flag=true; } if(m!=n-1){ cout<<"Graph "<<++index<<" is not a caterpillar."<<endl; continue; } if(flag || !judge()) { cout<<"Graph "<<++index<<" is not a caterpillar."<<endl; continue; } for(int i=1;i<=n;i++) f[i][i]=0; for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) f[i][j]=min(f[i][j],f[i][k]+f[k][j]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(f[i][j]>max_dis && f[i][j]!=INF){ max_dis=f[i][j]; s=i,t=j; }//找树的直径 p[++tail]=s; p[++tail]=t; in[s]=in[t]=true; for(int i=1;i<=n;i++) if((f[s][i]+f[i][t])==f[s][t] && i!=s && i!=t) p[++tail]=i,in[i]=true;//把树的直径上的点都存在p[]里 for(int i=1;i<=n;i++){ for(int j=1;j<=tail;j++){ if(f[i][p[j]]==1 && i!=s && i!=t && !in[i]){ bz=true; num++; } } if(bz==true) continue; } if(num==n-tail) cout<<"Graph "<<++index<<" is a caterpillar."<<endl; else cout<<"Graph "<<++index<<" is not a caterpillar."<<endl; } return 0;}
最后还要注意:
在判环和判是否为一棵树时,一定要等这组数据读完再推出,否则下次会一直读这组数据剩下的数,我就是这样WA了一上午QAQ
- POJ 3310 Caterpillar
- Poj 3310 Caterpillar (并查集判环+度)
- poj 3310 Caterpillar 图的遍历
- POJ 3310 Caterpillar 并查集+dfs
- [POJ 3310][树的直径]Caterpillar
- 3310 Caterpillar
- POJ 3310 Caterpillar (BFS + DFS + 并查集)
- Caterpillar Problem
- Sicily 1403. Caterpillar
- A. Gabriel and Caterpillar(cf)
- codeforce-A. Gabriel and Caterpillar(模拟)
- codeforces--652A Gabriel and Caterpillar (模拟)
- codeforces 652 A. Gabriel and Caterpillar
- codeforces652AGabriel and Caterpillar(模拟题)
- Code Forces 652A Gabriel and Caterpillar
- Codeforces 652A Gabriel and Caterpillar【水题】
- 【CodeForces】[652A]Gabriel and Caterpillar
- 善于遗忘_转自caterpillar.onlyfun.net
- Queue与Topic区别
- IIC中的应答和非应答
- 企业应用SaaS行业的B端产品经理,如果提升自己的产品设计能力?
- Java并发之synchronized关键字
- 2017亿欧创新者年会-科技一带一路峰会召开通知!
- POJ 3310 Caterpillar
- javaScript变量及数据类型
- Java并发之线程
- C语言之指针(1)
- Git版本控制系统之基本使用
- 重磅报告 | 70页PPT、10大关键词解读中国互联网创新飞跃的五年
- Spring IOC -生命周期,aware接口
- [Linux] MySQL Cluster 集群安装
- 不得不知的心理学:释放连接的力量