标签:connect name 顶点 limit group define represent span bsp
input | output |
---|---|
5 7 1 2 1 3 1 4 2 4 2 3 3 4 5 4 |
3 3 1 2 4 3 1 4 3 4 1 2 3 4 |
#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) #define pb push_back typedef long long ll; using namespace std; const int N = 205; const int M = 24005; int n,m,k,l,tot=0; int parent[N],pre[N],vis[N]; vector<int>vec[N],ans[N*N]; int Find(int x){ if(parent[x]!=x)parent[x]=Find(parent[x]); return parent[x]; } void Union(int x,int y){ x=Find(x);y=Find(y); if(x==y)return; else parent[y]=x; } void bfs(int s,int t){ met(vis,0);met(pre,0); queue<int>q; q.push(s);vis[s]=1; while(!q.empty()){ int u=q.front();q.pop(); if(u==t)return; for(int i=0;i<vec[u].size();i++){ int v=vec[u][i]; if(!vis[v]){ pre[v]=u;vis[v]=1; q.push(v); } } } } int main() { int u,v; for(int i=0;i<N;i++)parent[i]=i; scanf("%d%d",&n,&m); while(m--){ scanf("%d%d",&u,&v); int x=Find(u);int y=Find(v); if(x==y){ bfs(u,v); ans[++tot].push_back(v); while(pre[v]){ ans[tot].pb(pre[v]); v=pre[v]; } }else{ vec[u].pb(v);vec[v].pb(u); Union(u,v); } } printf("%d\n",tot); for(int i=1;i<=tot;i++){ printf("%d",ans[i].size()); for(int j=0;j<ans[i].size();j++){ printf(" %d",ans[i][j]); }printf("\n"); } return 0; }
URAL 1077 Travelling Tours(统计无向图中环的数目)
标签:connect name 顶点 limit group define represent span bsp
原文地址:http://www.cnblogs.com/jianrenfang/p/5997525.html