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. iOS程序的启动过程-UIWindow

    UIApplicationMain main函数中执行了一个UIApplicationMain这个函数 int UIApplicationMain(int argc, char *argv[], NS ...

  2. Android基础总结

    原文  http://blog.csdn.net/heimady/article/details/10363995 1. 前言 1.1. 什么是 3G . 4G Ÿ 第三代移动通信技术(3rd - G ...

  3. objectARX 获取ucs的X方向

    struct resbuf var; acedGetVar(_T("UCSXDIR"), &var);//获取用户坐标系下X方向量 ver = asVec3d(var.re ...

  4. Image Generator (Image Builder)

    如果你想要下载一个预编译好的镜像文件,或者想要尝试整个编译过程,一个替代方案是使用镜像生成器(Image Generator)(以前被叫做Image Builder).这是一个预编译好的OpenWrt ...

  5. EditText提示文字

    如图,在没有输入文字前,会显示提醒文字. 引用的是文本文件(strings.xml)中的对应文字 activity_main.xml对应的红色代码: <?xml version="1. ...

  6. Android MotionEvent getX() getY() getRawX() getRawY() and View getTop() getLeft()

    getRowX:触摸点相对于屏幕的坐标getX: 触摸点相对于按钮的坐标getTop: 按钮左上角相对于父view(LinerLayout)的y坐标getLeft: 按钮左上角相对于父view(Lin ...

  7. vector 初始化

    //数组初始化vector int iarray[]={1,2,3,4,5,6,7,8,9,0}; //count: iarray数组个数 size_t count=sizeof(iarray)/si ...

  8. Kylin上chromium不能用flash的解决命令

    sudo apt-get update sudo apt-get install pepperflashplugin-nonfree sudo update-pepperflashplugin-non ...

  9. HDU5438--Ponds (拓排+BFS)

    点击打开链接 题意:给定n个池塘,m对池塘相连,现在要将与少于2个池塘相连的池塘拆除,形成森林,求节点数为奇数的树权值之和 思路:按照拓排的思想不断删除度数小于2的节点 #include<bit ...

  10. HDU 4160

    http://acm.hdu.edu.cn/showproblem.php?pid=4160 大娃娃可以套在小娃娃外面(各边严格小),问最后最少得到几个娃娃 题目中的娃娃可以看做点,嵌套关系可以看做有 ...