標籤:connect name 頂點 limit group define represent span bsp
Travelling ToursTime limit: 1.0 second
Memory limit: 64 MBThere are N cities numbered from 1 to N (1 ≤ N ≤ 200)and M two-way roads connect them. There are at most one road between two cities. In summer holiday, members of DSAP Group want to make some traveling tours. Each tour is a route passes K different cities (K > 2) T1, T2, …, TKand return to T1. Your task is to help them make T tours such that:
- Each of these T tours has at least a road that does not belong to (T?1) other tours.
- T is maximum.
InputThe first line of input contains N and M separated with white spaces. Then follow by M lines, each has two number H and T which means there is a road connect city H and city T.OutputYou must output an integer number T — the maximum number of tours. If T > 0, then T lines followed, each describe a tour. The first number of each line is K — the amount of different cities in the tour, then K numbers which represent K cities in the tour.If there are more than one solution, you can output any of them.Sample
| input |
output |
5 71 21 31 42 42 33 45 4 |
33 1 2 43 1 4 34 1 2 3 4 |
Problem Author: Nguyen Xuan My (Converted by Dinh Quang Hiep and Tran Nam Trung)【分析】給你一張無向圖,問你圖中最多存在多少個環。用並查集來做,每次輸入一條邊,如果兩個頂點不在同一集合中,就把他倆合為一個集合中,如果已經在一個集合中了,說明只要加上這條邊,就會形成一個環,然後就BFS找就行了,用pre數組記錄路徑。
#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_backtypedef 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(統計無向圖中環的數目)