URAL 1077 Travelling Tours(统计无向图中环的数目)
Travelling Tours
Memory limit: 64 MB
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:
- Each of these T tours has at least a road that does not belong to (T−1) other tours.
- T is maximum.
Input
Output
Sample
| input | output |
|---|---|
5 7 |
3 |
#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(统计无向图中环的数目)的更多相关文章
- 统计无向图中三角形的个数,复杂度m*sqrt(m).
统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #inclu ...
- asp.net——统计输入的字符数目
asp.net——统计输入的字符数目 题目: 在页面中有一个TextBox输入框,一个显示文字用的Label,一个提交按钮Button.在TextBox中输入一段英文字母,点击按钮提交后统计其中字母‘ ...
- Codeforces 11D A Simple Task 统计简单无向图中环的个数(非原创)
太难了,学不会.看了两天都会背了,但是感觉题目稍微变下就不会了.dp还是摸不到路子. 附ac代码: 1 #include<iostream> 2 #include<cstdio> ...
- URAL 1277 - Cops and Thieves - [无向图点带权的最小点割]
题目链接:https://cn.vjudge.net/problem/URAL-1277 The Galaxy Police (Galaxpol) found out that a notorious ...
- linux 统计命令执行后的行数或者统计目录下文件数目
ls |wc 是统计你这个目录下的文件数目.ls |wc -l是输出第一个结果即31即文件的数目.
- LEETCODE 1254 统计封闭岛屿的数目 Number of Closed Islands
地址 https://leetcode-cn.com/contest/weekly-contest-162/problems/number-of-closed-islands/ 有一个二维矩阵 gri ...
- Leetcode 1254. 统计封闭岛屿的数目
题目: 有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域(记号为 1 ). 我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座 ...
- 【LeetCode】1399. 统计最大组的数目 Count Largest Group
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接求 日期 题目地址:https://leetcod ...
- mysql 统计查询出来的数目
select count(*) as dd from users;
随机推荐
- 基于Lumisoft.NET组件的POP3邮件接收和删除操作
From: http://www.cnblogs.com/wuhuacong/archive/2013/05/06/3063093.html Lumisoft.NET组件是一个非常强大的邮件发送.邮件 ...
- Javascript计数器
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- PHP Array 函数
PHP Array 简介 array 函数允许您对数组进行操作. PHP 支持单维和多维的数组.同时提供了用数据库查询结果来构造数组的函数. 安装 array 函数是 PHP 核心的组成部分.无需安装 ...
- 扩展jQuery easyui datagrid增加动态改变列编辑的类型
$.extend($.fn.datagrid.methods, { addEditor : function(jq, param) { if (param instanceof Array) { $. ...
- spring六种种依赖注入方式
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
- hdu 2091
PS:PE了两次....又是这种奇怪的输出格式....两个三角形直接有空行.. 代码: #include "stdio.h" void ou(int n,char a); void ...
- python中的namespace
python中的名称空间是名称(标识符)到对象的映射. 具体来说,python为模块.函数.类.对象保存一个字典(__dict__),里面就是重名称到对象的映射. 可以参看下面python程序的输出: ...
- 数据库添加数据I
/*insert.php*/ <html> <head> <meta http-equiv="Content-Type" content=" ...
- Android获取图片资源的4种方式
1. 图片放在sdcard中 Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard) 2. 图片在项 ...
- 破解 keyme2程序(固定明码比较)
系统 : Windows xp 程序 : keyme2 程序下载地址 :http://pan.baidu.com/s/1pKqlHiF 要求 : 找出序列号 使用工具 : IDA Pro & ...