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) T1, T2, …, TK
and 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.

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 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)
【分析】给你一张无向图,问你图中最多存在多少个环。用并查集来做,每次输入一条边,如果两个顶点不在同一集合中,就把他俩合为一个集合中,如果已经在一个集合中了,说明只要加上这条边,就会形成一个环,然后就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_back
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int n,m,k,l,tot=;
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,);met(pre,);
queue<int>q;
q.push(s);vis[s]=;
while(!q.empty()){
int u=q.front();q.pop();
if(u==t)return;
for(int i=;i<vec[u].size();i++){
int v=vec[u][i];
if(!vis[v]){
pre[v]=u;vis[v]=;
q.push(v);
}
}
}
}
int main() {
int u,v;
for(int i=;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=;i<=tot;i++){
printf("%d",ans[i].size());
for(int j=;j<ans[i].size();j++){
printf(" %d",ans[i][j]);
}printf("\n");
}
return ;
}

URAL 1077 Travelling Tours(统计无向图中环的数目)的更多相关文章

  1. 统计无向图中三角形的个数,复杂度m*sqrt(m).

    统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #inclu ...

  2. asp.net——统计输入的字符数目

    asp.net——统计输入的字符数目 题目: 在页面中有一个TextBox输入框,一个显示文字用的Label,一个提交按钮Button.在TextBox中输入一段英文字母,点击按钮提交后统计其中字母‘ ...

  3. Codeforces 11D A Simple Task 统计简单无向图中环的个数(非原创)

    太难了,学不会.看了两天都会背了,但是感觉题目稍微变下就不会了.dp还是摸不到路子. 附ac代码: 1 #include<iostream> 2 #include<cstdio> ...

  4. URAL 1277 - Cops and Thieves - [无向图点带权的最小点割]

    题目链接:https://cn.vjudge.net/problem/URAL-1277 The Galaxy Police (Galaxpol) found out that a notorious ...

  5. linux 统计命令执行后的行数或者统计目录下文件数目

    ls |wc 是统计你这个目录下的文件数目.ls |wc -l是输出第一个结果即31即文件的数目.

  6. LEETCODE 1254 统计封闭岛屿的数目 Number of Closed Islands

    地址 https://leetcode-cn.com/contest/weekly-contest-162/problems/number-of-closed-islands/ 有一个二维矩阵 gri ...

  7. Leetcode 1254. 统计封闭岛屿的数目

    题目: 有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域(记号为 1 ). 我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座 ...

  8. 【LeetCode】1399. 统计最大组的数目 Count Largest Group

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接求 日期 题目地址:https://leetcod ...

  9. mysql 统计查询出来的数目

    select count(*) as dd from users;

随机推荐

  1. hash算法

    作者:July.wuliming.pkuoliver 说明:本文分为三部分内容, 第一部分为一道百度面试题Top K算法的详解:第二部分为关于Hash表算法的详细阐述:第三部分为打造一个最快的Hash ...

  2. 摘自:java夜未眠之java学习之道

    目前Java可以说是产业界和学术界最热门的语言,许多读者都很急切想把Java学好.除非是武侠小说中的运功传送内力的方式,否则花上一段时间苦学是免不了的.花时间,不打紧,就是怕方法错误,事倍功半.我认为 ...

  3. 12-27 UITableView常用属性及方法

    UITableView也有自己的代理协议,它本身继承自UIScrollView 一:代理要遵守代理协议<UITableViewDelegate>,代理协议中的代理方法: 1.改变某一行的行 ...

  4. 第一课~Django~简介

    Django一个可以是Web开发工作者开发工作愉快并且高效的Web 框架 . 使用Django , 使你能够以最小的代建构建和维护高质量的Web应用 . 从好的方面来看 , Web开发激动人心并且富有 ...

  5. 2013杭州现场赛B题-Rabbit Kingdom

    杭州现场赛的题.BFS+DFS #include <iostream> #include<cstdio> #include<cstring> #define inf ...

  6. HDU 5086

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 求所有连续区间的数字和 本质是一个乘法原理,当前位置的数字出现次数=这个数之前的数字个数*这个数之后的数字 ...

  7. 【转】Polymer API开发指南 (一)(翻译)

    原文转自:http://segmentfault.com/blog/windwhinny/1190000000592324 翻译的不好,轻拍 Polymer是google的一款前端开发框架,其基于Sh ...

  8. 第一个demo

    1.首先这是最初的概念模型. 2.最后设计成这样. 3.运行

  9. postgresql 分区表创建及测试

    1      建立分区 1.1.  创建主表 CREATE TABLE measurement ( city_id         int not null, logdate        date ...

  10. What books does Bjarne Stroustrup suggest to master C++?

    QUESTION : What books does Bjarne Stroustrup suggest to master C++? ANSWER: A Tour of C++ is a quick ...