1077. Travelling Tours
Time limit: 1.0 second Memory limit: 64 MB
There 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)
T
1,
T
2, …,
TK
and return to
T
1. Your task is to help them make
Ttours such that:
- Each of these T tours has at least a road that does not belong to (T−1) other tours.
- T is the greatest.
Input
The 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
Hand city
T.
Output
You 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 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 |
Problem Author: Nguyen Xuan My (Converted by Dinh Quang Hiep and Tran Nam Trung)
Problem Source: From the third contest at Department of Mathematics and Informatics - Natural Sciences College - National University of HaNoi.
***************************************************************************************
Disjoint-set;
Each time a ring is obtained, labeled node until it finds all of the rings
***************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<vector> 8 using namespace std; 9 int head[1001],fa[1001]; 10 int n,m,i,j,k,cnt; 11 int link[1001][1001];//联系数组 12 int vis[1001];//Labeled node used 13 is Vector < int > ANS [ 180000 ;] // store results 14 void the make () 15 { 16 for ( int IT = 0 ; IT <= n-; IT ++ ) . 17 { 18 is FA [IT] = IT; . 19 Link [IT] [ 0 ] = 0 ; 20 is ANS [IT] .clear (); 21 is } 22 is } 23 is int Find ( int X) // disjoint-set 24 { 25 if(fa[x]!=x) 26 fa[x]=find(fa[x]); 27 return fa[x]; 28 } 29 void work(int x,int y) 30 { 31 memset(vis,0,sizeof(vis)); 32 memset(head,-1,sizeof(head));//前驱存储 33 head[x]=-1; 34 vis[x]=1; 35 queue<int>Q; 36 Q.push(x); 37 while(!Q.empty()) 38 { 39 int h=Q.front(); 40 Q.pop(); 41 if(h==y) 42 { 43 ans[cnt].push_back(y); 44 for(int it=head[y];it!=-1;it=head[it]) 45 ans[cnt].push_back(it); 46 cnt++; 47 return; 48 } 49 for( Int IT = . 1 ; IT <= Link [H] [ 0 ]; IT ++ ) 50 { 51 is int GS = Link [H] [IT]; // put into the queue node is not accessible 52 is IF (! VIS [GS ]) 53 is { 54 is Q.push (GS); 55 VIS [GS] = . 1 ; 56 is head [GS] = H; 57 is } 58 } 59 60 } 61 is 62 is 63 is } 64 int main() 65 { 66 cin>>n>>m; 67 int x,y; 68 cnt=0; 69 make(); 70 for(i=1;i<=m;i++) 71 { 72 cin>>x>>y; 73 int fs=find(x); 74 int ds=find(y); 75 if(fs==ds) 76 { 77 work(x,y); 78 } 79 else 80 { 81 link[x][++link[x][0]]=y; 82 link[y][++link[y][0]]=x; 83 fa[fs]=ds; 84 } 85 86 } 87 cout<<cnt<<endl; 88 for(i=0;i<cnt;i++) 89 { 90 int hs=ans[i].size(); 91 cout<<hs<<' '; 92 for(j=0;j<hs;j++) 93 cout<<ans[i][j]<<' '; 94 cout<<endl; 95 } 96 return 0; 97 98 }
Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3283676.html