标签:puts make i++ nal determine ems else bsp string
input | output |
---|---|
3 2 20 1 2 10 2 3 5 |
NO |
3 3 1000 1 2 1 2 3 1 1 3 1 |
YES |
Problem Source: 2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk,
【分析】先判断是否有环,如果有则YES,没有的话找最大直径,如果大于等于s,则YES,其他情况则NO。注意有图不连通情况。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <queue> #include <vector> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) typedef long long ll; using namespace std; const int N = 10005; const int M = 24005; int n,m,cnt=0; int tot=0,s,t,son,sum; int head[N],dis[N],vis[N],pre[N],vis1[N]; int w[N][N]; int in[N],out[N]; int bfs(int x) { met(vis,0); met(dis,0); sum=0; queue<int>Q; Q.push(x); vis[x]=1;vis1[x]=1; while(!Q.empty()) { int t=Q.front(); Q.pop();//printf("t=%d\n",t); bool has=false; for(int i=1; i<=n; i++) { if(!vis[i]&&w[t][i]!=0) { Q.push(i); dis[i]=dis[t]+w[t][i]; vis[i]=vis1[i]=1; has=true; } } if(!has) { if(dis[t]>sum) { sum=dis[t]; //printf("%d %d\n",sum,dis[t]); son=t; } } } return sum; } int main() { int u,v,l,sum=0; scanf("%d%d%d",&n,&m,&s); while(m--) { scanf("%d%d%d",&u,&v,&l); w[u][v]=w[v][u]=l; in[u]++; in[v]++; } queue<int>q; for(int i=1; i<=n; i++) { if(in[i]<=1)q.push(i); } while(!q.empty()) { int t=q.front(); q.pop(); vis[t]=1; for(int i=1; i<=n; i++) { if(!vis[i]&&w[t][i]!=0) { in[i]--; if(in[i]==1)q.push(i); } } } bool flag=false; for(int i=1; i<=n; i++) { if(!vis[i])flag=true; } if(flag)printf("YES\n"); else { int ans=-1; memset(vis1,0,sizeof vis1); for(int i=1; i<=n; i++) if(!vis1[i]) { bfs(i); ans=max(ans,bfs(son)); } if(ans>=s)puts("YES"); else puts("NO"); } return 0; }
URAL 1227 Rally Championship(树的直径)(无向图判环)
标签:puts make i++ nal determine ems else bsp string
原文地址:http://www.cnblogs.com/jianrenfang/p/5986261.html