URAL 1077 travelling Tours (statistics on the number of non-circular graphs)

Source: Internet
Author: User

Travelling tourstime limit:1.0 Second
Memory limit:64 Mbthere is NCities numbered from 1 to N(1≤ N≤200) and MTwo-way roads connect them. There is at the most one road between and cities. In summer holiday, members of the DSAP Group want to make some traveling tours. Each tour is a route passes KDifferent cities ( K> 2) T1, T2,..., TKand return to T1. Your task is to help them make TTours such that:
    1. Each of the These T tours have at least a road, does not belong to (T? 1) and other tours.
    2. T is maximum.
Inputthe first line of input contains Nand MSeparated with white spaces. Then follow by MLines, each with a number Hand TWhich means there is a road connect city Hand city T. Outputyou must output an integer number T-the Maximum number of tours. If T> 0, then TLines followed, each describe a tour. The first number is K-the amount of different cities in the tour and then KNumbers which represent KCities in the tour. If there is 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)"Analysis" gives you an no-map, asking you how many rings exist in the graph. Use and check the set to do, each input an edge, if the two vertices are not in the same set, they are combined into a set, if already in a collection,as long as this edge is added, a ring will be formed, and then the BFS can be found on the line, using the pre array to record the path.
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#defineINF 0x3f3f3f3f#defineMet (b) memset (a,b,sizeof a)#definePB Push_backtypedefLong Longll;using namespacestd;Const intN =205;Const intM =24005;intn,m,k,l,tot=0;intParent[n],pre[n],vis[n];vector<int>vec[n],ans[n*N];intFind (intx) {    if(parent[x]!=x) parent[x]=Find (parent[x]); returnparent[x];}voidUnion (intXinty) {x=find (x); y=Find (y); if(x==y)return; Elseparent[y]=x;}voidBFsintSintt) {Met (Vis,0); Met (Pre,0); Queue<int>Q; Q.push (s); Vis[s]=1;  while(!Q.empty ()) {        intu=Q.front (); Q.pop (); if(u==t)return;  for(intI=0; I<vec[u].size (); i++){            intv=Vec[u][i]; if(!Vis[v]) {Pre[v]=u;vis[v]=1;            Q.push (v); }        }    }}intMain () {intu,v;  for(intI=0; i<n;i++) parent[i]=i; scanf ("%d%d",&n,&m);  while(m--) {scanf ("%d%d",&u,&v); intX=find (U);inty=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(intI=1; i<=tot;i++) {printf ("%d", Ans[i].size ());  for(intj=0; J<ans[i].size (); j + +) {printf ("%d", Ans[i][j]); }printf ("\ n"); }    return 0;}

URAL 1077 travelling Tours (statistics on the number of non-circular graphs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.