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

题干:

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 ;
}

总结:

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441938.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

*【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环)

题干&#xff1a; Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specia…

使用java开发应用程序_使用Java中的插件支持开发应用程序

我一直在研究如何开发可以加载插件的应用程序.到目前为止,我已经看到这可以通过定义一个接口来实现,并让插件实现它.但是,我当前的问题是如何在Jars中打包时加载插件.有没有“最好的”方法呢&#xff1f;我正在考虑的当前逻辑是让每个插件和他们的Jar内部寻找实现接口的类.但我…

【POJ - 1995】Raising Modulo Numbers(裸的快速幂)

题干&#xff1a; People are different. Some secretly read magazines full of interesting girls pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that t…

软件设计师下午题java_2018上半年软件设计师下午真题(三)

● 阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】生成器( Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图6-1所示为其类图。【Java代码】import java.util.*&#xff1b;class Product {priv…

【ZOJ - 2724】【HDU - 1509】Windows Message Queue(优先队列)

题干&#xff1a; Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhil…

java细粒度锁_Java细粒度锁实现的3种方式

最近在工作上碰见了一些高并发的场景需要加锁来保证业务逻辑的正确性&#xff0c;并且要求加锁后性能不能受到太大的影响。初步的想法是通过数据的时间戳&#xff0c;id等关键字来加锁&#xff0c;从而保证不同类型数据处理的并发性。而java自身api提供的锁粒度太大&#xff0c…

【POJ - 1062】【nyoj - 510】昂贵的聘礼 (Dijkstra最短路+思维)

题干&#xff1a; 年轻的探险家来到了一个印第安部落里。在那里他和酋长的女儿相爱了&#xff0c;于是便向酋长去求亲。酋长要他用10000个金币作为聘礼才答应把女儿嫁给他。探险家拿不出这么多金币&#xff0c;便请求酋长降低要求。酋长说&#xff1a;"嗯&#xff0c;如果…

php e notice,PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明

举例说明&#xff1a;在Windows环境下&#xff1a;原本在php4.3.0中运行正常的程序&#xff0c;在4.3.1中为何多处报错&#xff0c;大体提示为&#xff1a;Notice:Undefined varialbe:变量名称. 例如有如下的代码&#xff1a; 复制代码 代码如下:if (!$tmp_i) { $tmp_i10; }在4…

【POJ - 3169】 Layout(差分约束+spfa)(当板子记?)

题干&#xff1a; Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < N < 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are nu…

php中pregmatch,php中preg_match的isU代表什么意思

isU是大小写分的意思&#xff0c;这里s还有则不包括换行符而U是反转了匹配数量的值使其不是默认的重复,大概就是这样了个体我们看文章。正则后面的/(.*)/isU &#xff0c;“isU”参数代表什么意思&#xff1f;这是正则中的修正符.i是同时查找大小写字母,s是圆点(.)匹配所有字符…

【POJ - 1511】 Invitation Cards(Dijkstra + 反向建图 多源到单源最短路的处理)

题干&#xff1a; In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all…

【HDU - 3499】 Flight (单源最短路+优惠问题)

题干&#xff1a; Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him re…

php能不能用MyBatis,Mybatis与Ibatis的区别

Mybatis与Ibatis的区别:1、Mybatis实现了接口绑定&#xff0c;使用更加方便在ibatis2.x中我们需要在DAO的实现类中指定具体对应哪个xml映射文件&#xff0c;而Mybatis实现了DAO接口与xml映射文件的绑定&#xff0c;自动为我们生成接口的具体实现&#xff0c;使用起来变得更加省…

【51Nod - 1001 】 数组中和等于K的数对 (排序+ 尺取)

题干&#xff1a; 给出一个整数K和一个无序数组A&#xff0c;A的元素为N个互不相同的整数&#xff0c;找出数组A中所有和等于K的数对。例如K 8&#xff0c;数组A&#xff1a;{-1,6,5,3,4,2,9,0,8}&#xff0c;所有和等于8的数对包括(-1,9)&#xff0c;(0,8)&#xff0c;(2,6)…

linux php oauth安装,Linux php 扩展安装 mongo ,redis ,soap,imap,pdo_mysql,oauth

安装mongodb 参看文章&#xff1a;2.安装redisyum install gitgit clone git://github.com/owlient/phprediscd phpredis/usr/local/php/bin/phpize./configure --with-php-config/usr/local/php/bin/php-configmake && make install如果上述出现报错&#xff0c;可以尝…

*【CF#510C】Fox And Names (拓扑排序)

题干&#xff1a; Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she…

java质,JAVA分解质因子 - osc_r1gtal48的个人空间 - OSCHINA - 中文开源技术交流社区

/*题目分解质因数(5分)题目内容&#xff1a;每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式&#xff0c;这几个素数就都叫做这个合数的质因数。比如&#xff0c;6可以被分解为2x3&#xff0c;而24可以被分解为2x2x2x3。现在&#xff0c;你的程序要读入一个[2,100…

【HDU - 5605】 geometry(水,数学题,推公式)

题干&#xff1a; There is a point PP at coordinate (x,y)(x,y). A line goes through the point, and intersects with the postive part of X,YX,Yaxes at point A,BA,B. Please calculate the minimum possible value of |PA|∗|PB||PA|∗|PB|. Input the first line…

matlab如何画函数的外包络曲线,怎样在MATLAB中划出一个函数的包络线?

沧海一幻觉下面是一系列关于MATLAB的包络线的程序&#xff1a;%这是定义了一个函数&#xff1a;function [up,down] envelope(x,y,interpMethod)%ENVELOPE gets the data of upper and down envelope of the known input (x,y).%% Input parameters:% x the abscissa of the g…

【51nod - 1087】 1 10 100 1000(找规律推公式,水,map)

题干&#xff1a; 1,10,100,1000...组成序列1101001000...&#xff0c;求这个序列的第N位是0还是1。 Input 第1行&#xff1a;一个数T&#xff0c;表示后面用作输入测试的数的数量。&#xff08;1 < T < 10000) 第2 - T 1行&#xff1a;每行1个数N。&#xff08;1 &…