poj 3895.Cycles of Lanes
来源:互联网 发布:数据挖掘在金融欺诈 编辑:程序博客网 时间:2024/05/16 18:20
Cycles of Lanes
#include <vector>#include <iostream>#include <cstdio>using namespace std;#define N 5000vector<int> v[N];int vis[N];int step[N];int ans;void dfs(int x,int t){ vis[x]=1; step[x]=t; int i; for(i=0;i<v[x].size();i++) { int k=v[x][i]; if(!vis[k]) dfs(k,t+1); else { int temp=step[x]-step[k]+1; if(temp>ans)ans=temp; } }}int main(){ int ca; scanf("%d",&ca); int n,m; while(ca--) { scanf("%d%d",&n,&m); int i,j,k; for(i=0;i<=n;i++) { vis[i]=0; v[i].clear(); } while(m--) { int a,b; scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); } ans=0; for(i=1;i<=n;i++) { if(!vis[i]) { dfs(i,1); } } if(ans>=3) printf("%d\n",ans); else printf("0\n"); } return 0;}
#include <iostream>#include <string>#include <cstdio>using namespace std; int len; #define MAX 4450 int head[MAX]; int step[MAX]; struct node { int v; int next; } E[MAX *MAX]; int num; void init() { num = 0; len = 0;//note memset(head, -1, sizeof(head)); memset(step, -1, sizeof(step)); } void add(int s, int t) { E[num].v = t; E[num].next = head[s]; head[s] = num++; } void dfs(int cur, int pre) { int i; for(i = head[cur]; i != -1; i = E[i].next) { int v = E[i].v; if(v == pre) continue; if(step[v] == -1) { step[v] = step[cur] + 1; dfs(v, cur); } else { if(step[cur] + 1 - step[v] > len) len = step[cur] + 1 - step[v]; } } } int main() { int t; scanf("%d", &t); while(t--) { int n, m; scanf("%d%d", &n, &m); init(); int i; int st, ed; for(i = 1; i <= m; i++) { scanf("%d%d", &st, &ed); add(st, ed); add(ed, st); } step[1] = 0; dfs(1, 1); printf("%d\n", len); } }
- poj 3895.Cycles of Lanes
- POJ 3895 Cycles of Lanes
- POJ 3895 Cycles of Lanes
- POJ 3895 Cycles of Lanes
- POJ 3895 Cycles of Lanes
- poj-3895-Cycles of Lanes 简单DFS
- POJ 3895 Cycles of Lanes(DFS)
- POJ 3895 Cycles of Lanes (dfs)
- POJ 3895 Cycles of Lanes(DFS)
- poj3895 Cycles of Lanes
- poj3895 Cycles of Lanes
- poj 3895 Cycles of Lanes(DFS找最大环)
- POJ 3895 Cycles of Lanes 已被翻译
- POJ 3895 Cycles of Lanes(dfs+模拟)
- POJ3895 Cycles of Lanes,DFS
- poj 3895 Cycles of Lanes 修改tarjan算法求图中最大环
- poj3895Cycles of Lanes(dfs)
- poj 3377 Ferry Lanes
- 成为Java高手需要注意的25个学习目标
- jaxb冲突
- iOS6 UITableView的背景颜色的修改
- mac php环境的配置
- POJ 1183
- poj 3895.Cycles of Lanes
- eclipse中安装Flex插件步骤
- MAC android NDK r8c 安装
- 免费的Sprite精灵图片,可以随便用。SpriteLib
- linux脚本----函数的使用
- 网页编码就是那点事
- Syntax error on tokens, delete these tokens 的原因和解决办法
- Lucene 基础理论
- List的使用示例