【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)

39 篇文章 0 订阅
39 篇文章 0 订阅

题干:

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 nn2indicating 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.是一棵树(是否全部连通并且不存在环)。

            2.存在一条脊椎(主链),使得所有点要么在链上要么与链上的点距离为1(即支链不会再有支链)。 
    输入格式,多组输入数据,每一组,第一行个数n,第二行边数m,第三行共m对(2*m个),每一对的数字是点的编号,表示这两个点之间连一条边。 

解题报告:

     这题做法很多?最近在学树的直径所以用此法解题。其实这里的dfs就是一层的递归啦没这么高大上。。。判断支链有无支链而已。

AC代码:(代码有点长哦但是各函数分工还是比较明确的)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
int cnt;
int head[1000 + 5];
int f[1000 + 5];
bool vis[1000 + 5];
struct Edge {
	int pos,to;
	int w;//此题中就是零,,没意义
	int ne;
} e[1000 + 5];
struct Point {
	int pos;
	int s;
	Point(){}
	Point(int pos,int s):pos(pos),s(s){} 
};
//记录路径 
int rem[505];
void add(int u,int v,int w) {
	e[cnt].pos = u;
	e[cnt].to = v;
	e[cnt].w = w;
	e[cnt].ne = head[u];
	head[u] = cnt++; 
}
void init() {
	cnt = 0;
	memset(head ,-1, sizeof(head));
	memset(vis,0,sizeof(vis));
	for(int i = 1; i<=n; i++) f[i] = i;
}
int getf(int v) {
	return f[v] == v? v:f[v] = getf(f[v]);
}
void merge(int u,int v) {
	int t1 = getf(u);
	int t2 = getf(v);
	f[t2] = t1;
}
//判断连通无环 
bool judge() {
	if(m != n-1) return false;
	for(int i = 1; i<=n; i++) {
		if(getf(i) != getf(1) ) {
			return false;
		}
	}
	return true;
}
int bfs(int st) {
	queue<Point> q;
	q.push(Point(st,1));
	int retp = st;
	int maxx = 0;
	vis[st] = 1;
	while(q.size()) {
		Point cur = q.front();
		q.pop();
		for(int i = head[cur.pos]; i!=-1; i = e[i].ne) {
			Point now = Point(e[i].to, cur.s +1);
			if(vis[now.pos] == 1) continue;
			vis[now.pos] = 1;
			rem[now.pos] = cur.pos;
			q.push(now);
			if(now.s>maxx) {
				maxx = now.s;
				retp = now.pos;
			}
		}
	}
	return retp;
}
int dfs(int st) {
//	printf(":::::::::::::::::::::::::::\n");
	int cnt = 0;
	for(int i = head[st];i!=-1;i=e[i].ne) {
		if(vis[e[i].to] == 0) {
			vis[e[i].to] = 1; 
			cnt+=dfs(e[i].to)+1;
		}
	}
//	printf("cnt = %d\n",cnt);
	return cnt;
}
int main()
{
	int a,b,iCase = 0;
	while(scanf("%d",&n) ) {
		if(n == 0) break;
		scanf("%d",&m);
		init();
		for(int i = 1; i<=m; i++) {
			scanf("%d%d",&a,&b);
			add(a,b,0);
			add(b,a,0);//注意 需要加两条边! 
			merge(a,b);
		}
		if(!judge()) {
			printf("Graph %d is not a caterpillar.\n",++iCase);continue;
		}
		int u = bfs(1);//树的直径的一个端点 
		memset(vis,0,sizeof(vis));
		memset(rem,0,sizeof(rem));
		int v = bfs(u);
//		printf("%d  %d\n",u,v);
		//必须从v开始倒着找,不能u开始! 
//		printf("%d ",v);
		//将骨架上的点标记为走过。 
		memset(vis,0,sizeof(vis) );
		vis[v] = 1;
		int flag = 0;
//		printf(":::::::::::::::::::::::::::\n");
		for(int i = v; rem[i]!=0; i=rem[i]) vis[i] = 1;
		if(dfs(v)>1) {
			flag = 1;
			printf("Graph %d is not a caterpillar.\n",++iCase);continue;
		}
		for(int i = rem[v]; rem[i]!=0; i=rem[i]) {
//			vis[i] = 1;
			for(int j = head[i];j !=-1;j=e[j].ne) {
				if(vis[e[j].ne] == 1) continue;
				if(dfs(e[j].ne)>0) {
//				printf("%d ",rem[i]);
					flag = 1;
					printf("Graph %d is not a caterpillar.\n",++iCase);break;
				}
				
			}
			if(flag == 1) break;
//			printf("%d ",rem[i]);
		}
		if(flag == 0) {
			printf("Graph %d is a caterpillar.\n",++iCase);
		}
		
	}
	return 0 ;
}

总结:

    加边别忘是加两条边,无向图! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值