URAL 1077 Travelling Tours(統計無向圖中環的數目)

來源:互聯網
上載者:User

標籤: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:
  1. Each of these T tours has at least a road that does not belong to (T?1) other tours.
  2. 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(統計無向圖中環的數目)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.