Wizard's Tour
2 seconds
256 megabytes
standard input
standard output
All Berland residents are waiting for an unprecedented tour of wizard in his Blue Helicopter over the cities of Berland!
It is well-known that there are n cities in Berland, some pairs of which are connected by bidirectional roads. Each pair of cities is connected by no more than one road. It is not guaranteed that the road network is connected, i.e. it is possible that you can't reach some city from some other.
The tour will contain several episodes. In each of the episodes:
- the wizard will disembark at some city x from the Helicopter;
- he will give a performance and show a movie for free at the city x;
- he will drive to some neighboring city y using a road;
- he will give a performance and show a movie for free at the city y;
- he will drive to some neighboring to y city z;
- he will give a performance and show a movie for free at the city z;
- he will embark the Helicopter and fly away from the city z.
It is known that the wizard doesn't like to use roads, so he agrees to use each road at most once (regardless of direction). In other words, for road between a and b he only can drive once from a to b, or drive once from b to a, or do not use this road at all.
The wizards wants to plan as many episodes as possible without violation the above rules. Help the wizard!
Please note that the wizard can visit the same city multiple times, the restriction is on roads only.
The first line contains two integers n, m (1 ≤ n ≤ 2·105, 0 ≤ m ≤ 2·105) — the number of cities and the number of roads in Berland, respectively.
The roads description follow, one in each line. Each description is a pair of two integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), where ai and bi are the ids of the cities connected by the i-th road. It is guaranteed that there are no two roads connecting the same pair of cities. Every road is bidirectional. The cities are numbered from 1 to n.
It is possible that the road network in Berland is not connected.
In the first line print w — the maximum possible number of episodes. The next w lines should contain the episodes in format x, y, z — the three integers denoting the ids of the cities in the order of the wizard's visits.
4 5
1 2
3 2
2 4
3 4
4 1
2
1 4 2
4 3 2
5 8
5 3
1 2
4 5
5 1
2 5
4 3
1 4
3 2
4
1 4 5
2 3 4
1 5 3
5 2 1
分析:给一个图,求最多能组成多少个V图形,其中每条边只能用一次;
可以证明,对于每个联通块,最多可以组成edge/2个V图形;
考虑递归处理;
对于当前节点,标记所有没用的边,并把节点放入当前集合;
递归处理集合中的节点,如果没有访问过,则递归该节点;
如果递归返回一个节点,说明有未配对边,与当前边配对;
否则,当前边未配对,在全部结束后两两配对即可;
若配对后剩下一条边,返回到父亲即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define inf 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls (rt<<1)
#define rs (rt<<1|1)
#define all(x) x.begin(),x.end()
const int maxn=2e5+;
const int N=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=f*p%mo;p=p*p%mo;q>>=;}return f;}
int n,m,k,t;
map<ll,int>p,w;
vi e[maxn];
bool vis[maxn];
struct node
{
int x,y,z;
};
vector<node>ret;
bool ok(int x,int y,int z)
{
int ex=x,ey=y;
if(ex>ey)swap(ex,ey);
w[1LL*ex*N+ey]=;
ex=y,ey=z;
if(ex>ey)swap(ex,ey);
w[1LL*ex*N+ey]=;
}
int dfs(int x)
{
int i;
vis[x]=true;
vi bl;
rep(i,,e[x].size()-)
{
int y=e[x][i];
int z=x;
if(y>z)swap(y,z);
if(!p.count(1LL*y*N+z))
{
bl.pb(e[x][i]),
p[1LL*y*N+z]=;
}
}
rep(i,,bl.size()-)
{
int y=bl[i];
if(vis[y])continue;
int z=dfs(y);
if(z)ret.pb(node{x,y,z}),ok(x,y,z);
}
int y=,z=;
rep(i,,bl.size()-)
{
z=bl[i];
if(!w.count(1LL*min(z,x)*N+max(z,x)))
{
if(y)
{
ret.pb(node{y,x,z});
ok(y,x,z);
y=z=;
}
else y=z,z=;
}
}
return y;
}
int main(){
int i,j;
scanf("%d%d",&n,&m);
rep(i,,m)
{
int x,y;
scanf("%d%d",&x,&y);
e[x].pb(y),e[y].pb(x);
}
rep(i,,n)if(!vis[i])dfs(i);
printf("%d\n",ret.size());
rep(i,,ret.size()-)
{
printf("%d %d %d\n",ret[i].x,ret[i].y,ret[i].z);
}
return ;
}
Wizard's Tour的更多相关文章
- 【Codeforces858F】Wizard's Tour [构造]
Wizard's Tour Time Limit: 50 Sec Memory Limit: 512 MB Description Input Output Sample Input 4 5 1 2 ...
- CodeForces 860D Wizard's Tour
题意 给出一张无向图,要求找出尽量多的长度为2的不同路径(边不可以重复使用,点可以重复使用) 分析 yzy:这是原题 http://www.lydsy.com/JudgeOnline/problem. ...
- CF858F Wizard's Tour 解题报告
题目描述 给定一张 \(n\) 个点 \(m\) 条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通. 你想在这张图上进行若干次旅游,每次旅游可以任选一个点 \(x\) 作为起点,再走到一个 ...
- CF858F Wizard's Tour
也许更好的阅读体验 \(\mathcal{Description}\) 给定一张 \(n\) 个点 \(m\) 条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通. 你想在这张图上进行若干次 ...
- Wizard's Tour CodeForces - 860D (图,构造)
大意: 给定$n$节点$m$条边无向图, 不保证连通, 求选出最多邻接边, 每条边最多选一次. 上界为$\lfloor\frac{m}{2}\rfloor$, $dfs$贪心划分显然可以达到上界. # ...
- 「CF858F」 Wizard's Tour
传送门 Luogu 解题思路 首先对于树的情况,我们很显然有一种贪心策略: 对于每一个节点先匹配子树,然后在还可以匹配的儿子间尽可能匹配,要是多出来一个就往上匹配. 推广到图的情况... 我们在图的生 ...
- Codeforces Round #434 (Div. 2)
Codeforces Round #434 (Div. 2) 刚好时间对得上,就去打了一场cf,发现自己的代码正确度有待提高. A. k-rounding 题目描述:给定两个整数\(n, k\),求一 ...
- salesforce 零基础学习(六十)Wizard样式创建数据
项目中表之间关联关系特别多,比如三个表中A,B,C C作为主表,A,B作为从表,有时候C表需要创建数据时,同时需要创建A,B两个表的数据,这种情况下,使用Wizard样式会更加友好. 以Goods_ ...
- Wizard Framework:一个自己开发的基于Windows Forms的向导开发框架
最近因项目需要,我自己设计开发了一个基于Windows Forms的向导开发框架,目前我已经将其开源,并发布了一个NuGet安装包.比较囧的一件事是,当我发布了NuGet安装包以后,发现原来已经有一个 ...
随机推荐
- poj3737 UmBasketella 真正的三分
之前用二分写三分的板子...现在正式写一个三分,但是也不难,就是把区间分为三段就行了.求二次函数的峰值,每次取大的区间就行了. 题干: 最近几天,人们总是设计出多功能的新东西.例如,您不仅可以使用手机 ...
- 【转载】基于AFNetWorking3.0的图片缓存分析
原文出处: Yasin的简书 理论 不喜欢理论的可以直接跳到下面的Demo实践部分 缓存介绍 缓存按照保存位置可以分为两类:内存缓存.硬盘缓存(FMDB.CoreData…).我们常说的网络请求缓存包 ...
- ubuntu/linuxmint下java环境变量设置
1.root权限下使用vi或gedit打开/etc目录下的profile文件,末尾加入环境变量. 1)命令: sudo gedit /etc/profile 2)环境变量个人案例: export JA ...
- struts表单验证xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC &quo ...
- form表单点击后验证
function check(){ var customertype = document.getElementById("customertype"); //alert(cust ...
- SpringBoot集成Redis来实现缓存技术方案
概述 在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. ...
- mybatis多个参数查询问题
一.话不多数,错误如下 Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException ...
- Netty(1) - 理解
官网:netty.io ---------------------------------------------------------------------------------------- ...
- 动态规划DP入门
百度百科↓ 动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法.20世纪50年代初美国数学家R.E.Bellman ...
- Elasticsearch的索引模块(正排索引、倒排索引、索引分析模块Analyzer、索引和搜索、停用词、中文分词器)
正向索引的结构如下: “文档1”的ID > 单词1:出现次数,出现位置列表:单词2:出现次数,出现位置列表:…………. “文档2”的ID > 此文档出现的关键词列表. 一般是通过key,去 ...