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. vim配置及插件安装管理(超级详细)

    1 写在前面   Linux下编程一直被诟病的一点是: 没有一个好用的IDE, 但是听说Linux牛人, 黑客之类的也都不用IDE. 但是对我等从Windows平台转移过来的Coder来说, 一个好用 ...

  2. AbstractMap学习记录

    package java.util;import java.util.Map.Entry; /** * This class provides a skeletal implementation of ...

  3. python版恶俗古风自动生成器.py

    python版恶俗古风自动生成器.py """ python版恶俗古风自动生成器.py 模仿自: http://www.jianshu.com/p/f893291674c ...

  4. 【iOS开发】企业版证书($299)In-House方式发布指南 (转)

    一.明确几个概念 1.企业版IDP:即iOS Development Enterprise Program.注意是$299/Year那种,并不是$99/Year的那种. 2.In House:是只企业 ...

  5. poj2137 dp

    //Accepted 228K 32MS //dp[k][i][j] 表示从1的k号节点到i的j号节点的最小花费 //dp[k][i][j]=min(dp[k][i-1][h]+cost) cost为 ...

  6. ARC以及MRC中setter方法

    ARC以及MRC中setter方法的差异 有时候,你会需要重写setter或者getter方法,你知道么,ARC与MRC的setter方法是有着差异的呢. 先看下MRC下的setter方法: 在看下A ...

  7. TPLink 备份文件bin文件解析

    TPLink 路由器备份文件bin文件 测试路由器 WR885,备份文件加密方式DES,密钥:478DA50BF9E3D2CF linux端: openssl enc -d -des-ecb -nop ...

  8. (转)HTML文档头部信息

    原文:http://www.cnblogs.com/sunyunh/archive/2012/07/25/2609199.html HTML(3)HTML文档头部信息   <!DOCTYPE h ...

  9. BigBlueButton的安装

    下来VM文件,打开系统后 sudo apt-get updatesudo apt-get upgradesudo apt-get dist-upgrade sudo apt-get install b ...

  10. linux命令:cd

    1.介绍: cd可以说是linux下最基本的命令,全称是change directory. 2.命令格式: cd [DIRECTORY] 3.命令功能: 切换到指定目录 4.常用范例: 例一:切换到根 ...