UVA 539 The Settlers of Catan(图的搜索)
来源:互联网 发布:linux 搭建vps 编辑:程序博客网 时间:2024/06/05 11:36
题意:有n个点m条边,求该图的最长一条链。
思路:很简单的图的搜索,以每个点为起点都搜一遍过,找出最长的。
//0 KB 18 ms#include#include#define M 30struct Edge{ int u,v,nxt;}edge[M*2];int head[M],vis[M*2];int n,m,ep,ans;void addedge(int cu,int cv){ edge[ep].v = cv; edge[ep].nxt = head[cu]; head[cu] = ep++; edge[ep].v = cu; edge[ep].nxt = head[cv]; head[cv] = ep++;}void DFS(int u,int dep){ for (int i = head[u];i != -1;i = edge[i].nxt) { if (!vis[i]) { ans = ans > (dep+1)? ans:(dep+1); vis[i] = vis[i^1] = 1; //是无向图,反向边也得标志 DFS(edge[i].v,dep+1); vis[i] = vis[i^1] = 0; } }}int main (){ int i,j,u,v; while (~scanf ("%d %d",&n,&m)) { if (n == 0&&m == 0) break; memset (head,-1,sizeof(head)); ep = 0; for (i = 0;i < m;i ++) { scanf("%d %d",&u,&v); addedge(u,v); //addedge(v,u); } ans = 0; for(i = 0;i < n;i ++) { memset (vis,0,sizeof(vis)); DFS(i,0); } printf ("%d\n",ans); } return 0;}
- UVA 539 The Settlers of Catan(图的搜索)
- uva 539 - The Settlers of Catan(搜索)
- UVa 539 The Settlers of Catan
- Uva 539 The Settlers of Catan
- uva 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan
- UVA 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan
- uva 539 The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVA 539 The Settlers of Catan
- uva 539 The Settlers of Catan
- UVA 539 The Settlers of Catan
- uva 539 The Settlers of Catan
- UVa 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- 单尼美,快到碗里来
- 接口作为参数
- tcp短连接TIME_WAIT问题解决方法大全(3)——tcp_tw_recycle
- 由12306.cn谈谈网站性能技术
- python的命名空间
- UVA 539 The Settlers of Catan(图的搜索)
- JQuery animate ul列表项相互飘动
- 基于DTC的分布式事务管理模型[下篇]
- 黑马程序员----反射
- 庞果英雄会---寻找直方图中面积最大的矩形---编程挑战
- git入门与实践
- maven核心,pom.xml详解
- rild 术语
- linux重启命令