Codeforces Beta Round #95 (Div. 2) D. Subway(环最短路)
来源:互联网 发布:迪蒙火花机编程实例 编辑:程序博客网 时间:2024/06/07 12:23
题目地址:http://codeforces.com/problemset/problem/131/D
思路:寻找环。首先将度数为1的节点入队,将与度数为1的节点的度数减一。将度数小于2的节点入队并标记,最后没有被标记的节点就是组成环的节点。
#include<cstdio>#include<queue>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#define debuusing namespace std;const int maxn=3e3+50;int n;queue<int> q;vector<int> g[maxn];int d[maxn],v[maxn];int dist[maxn],V[maxn];void mindist(int s){ memset(V,0,sizeof(V)); while(!q.empty()) q.pop(); q.push(s),V[s]=1,dist[s]=0; while(!q.empty()) { int now=q.front(); q.pop(),V[now]=0; for(int i=0; i<g[now].size(); i++) { int nt=g[now][i]; if(dist[nt]>dist[now]+1) { dist[nt]=dist[now]+1; if(!V[nt]); { V[nt]=1; q.push(nt); } } } }}void solve(){ for(int i=1; i<=n; i++) if(d[i]==1) { q.push(i); v[i]=1; } while(!q.empty()) { int now=q.front(); q.pop(); for(int i=0; i<g[now].size(); i++) { int nt=g[now][i]; d[nt]--; if(d[nt]<2&&!v[nt]) { v[nt]=1; q.push(nt); } } } //cout<<"flag"<<endl; memset(dist,0x3f3f3f3f,sizeof(dist)); for(int i=1; i<=n; i++) if(!v[i]) mindist(i);}int main(){#ifdef debug freopen("in.in","r",stdin);#endif // debug scanf("%d",&n); for(int i=0; i<n; i++) { int x,y; scanf("%d%d",&x,&y); d[x]++,d[y]++; g[x].push_back(y); g[y].push_back(x); } solve(); for(int i=1; i<n; i++) printf("%d ",dist[i]); printf("%d\n",dist[n]); return 0;}
0 0
- Codeforces Beta Round #95 (Div. 2) D. Subway(环最短路)
- Codeforces Beta Round #95 (Div. 2) -- D. Subway (DFS)
- Codeforces Beta Round #95 (Div. 2) D.Subway
- Codeforces Beta Round #95 (Div. 2) Subway (树上找环)
- Codeforces Round #302 (Div. 2) D. Destroying Roads(最短路)
- Codeforces Round #302 (Div. 2)D. Destroying Roads-最短路
- Codeforces Round #302 (Div. 2) D bfs最短路
- CodeForces 131D Subway [tarjan+最短路]
- Codeforces Round #257 (Div. 2)D(最短路Dijkstra+堆优化)
- Codeforces Round #420 (Div. 2) D. Okabe and City (最短路)
- Codeforces Round #420 (Div. 2) D. Okabe and City(最短路)
- Codeforces Beta Round #77 (Div. 1 Only) C. Volleyball (最短路)
- CodeForces 601A The Two Routes(dijkstra最短路)——Codeforces Beta Round #333 (Div.1 Div. 2)
- Codeforces Round #257 div.2 D or 450D Jzzhu and Cities【最短路】
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
- Codeforces Round #257 (Div. 2) D Jzzhu and Cities 最短路
- Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路
- Codeforces Round #103 (Div. 2)---D. Missile Silos(最短路+枚举边)
- Ubuntu下Python3和Python2的共存
- HDU 5762 Teacher Bo
- TCP之长连接与短连接
- MVC中配置MinProfiler
- hibernate中使用ehcache缓存框架
- Codeforces Beta Round #95 (Div. 2) D. Subway(环最短路)
- 瞎JB扯淡(1)
- Tomcat内存溢出
- setMobileDataEnabled方法不能再被调用 Android L and later
- StackOverflow程序员推荐:每个程序员都应读的30本书
- DS1302+LCD1602=万年历
- 一、搭建移动应用服务器
- Linux Samba 服务器配置
- HDU 3368 Reversi (简单dfs)