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 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.
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 H and 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 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#include #include #include #include #include #include