Codeforces 131D. Subway 寻找环-树的最短路径
来源:互联网 发布:sd卡删除数据怎么恢复 编辑:程序博客网 时间:2024/06/07 02:16
图只有一个环,找到每个点距离环的最短距离。
#include <iostream>#include <cstring>using namespace std;bool a[4111][4111];int n;bool v[4111]={0};int p[4111]={0};int cnt=0;int ans[4111]={0};bool dfs_find(int i,int lt){ v[i]=true; for (int j=1;j<=n;j++) { if (a[i][j]>0&<!=j&&i!=j) { if (v[j]) { p[cnt++]=j; return true; } else { p[cnt++]=j; if (dfs_find(j,i)) { return true; } cnt--; } } } return false;}void dfs(int i){ for (int j=1;j<=n;j++) { if (i!=j&&a[i][j]>0&&ans[j]==-1) { ans[j]=ans[i]+1; dfs(j); } }}int main(){ memset(a,0,sizeof(a)); memset(ans,-1,sizeof(ans)); cin>>n; for (int i=1;i<=n;i++) { int x,y; cin>>x>>y; a[x][y]=a[y][x]=1; } p[cnt++]=1; dfs_find(1,0); ans[p[cnt-1]]=0; for (int i=cnt-2;i>=0;i--) { if (p[i]==p[cnt-1]) break; ans[p[i]]=0; } for (int i=1;i<=n;i++) { if (ans[i]==0) { dfs(i); } } for (int i=1;i<=n;i++) { cout<<ans[i]<<" "; } cout<<endl; return 0;}
- Codeforces 131D. Subway 寻找环-树的最短路径
- CodeForces 131D Subway [tarjan+最短路]
- POJ2502 Subway(最短路径)
- Codeforces 131D - Subway
- 寻找最短路径
- 最短路径D
- 寻找最短路径BFS
- CODEFORCES 450D Jzzhu and Cities <最短路径>
- codeforces 821D(状压构图+最短路径)
- Codeforces 131D Subway(找图上唯一环)
- 寻找有向图的最短路径的最大值
- 用栈寻找迷宫的最短路径
- vtkDijkstraGraphGeodesicPath在曲面上寻找最短路径的应用
- 迪杰斯特拉算法(最短路径寻找的方法)
- 图的基本操作(遍历,寻找最短路径,最小生成树)
- Codeforces Beta Round #95 (Div. 2) D. Subway(环最短路)
- 寻找电路布线最短路径(Queue)
- 算法-图论-Dijstra寻找最短路径
- The content of the adapter has changed but ListView did not receive a notification 异常解析
- c语言中.c和.h文件的困惑
- MAT Memory Analyzer Tool 插件装配(图解)
- Microsoft.XMLHttp的用法
- 10317 - Equating Equations
- Codeforces 131D. Subway 寻找环-树的最短路径
- mCAS之VIP
- 一个CAsyncSocket例子
- ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock
- Devexpress XtraReport 设置页面大小单位
- PHP 知识汇总
- AbandonedObjectPool 的相關屬性
- 计算机网络
- S5PV310 Android 电容屏驱动调试