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安装包以后,发现原来已经有一个 ...
 
随机推荐
- [模板] manacher(教程)
			
还是不会马拉车啊.今天又学了一遍,在这里讲一下. 其实就是一个很妙的思路,就是设置一个辅助的数组len,记录每个点的最大对称长度,然后再存一个mx记录最大的对称子串的右端点.先开二倍数组,然后一点点扩 ...
 - 懒人学习automake, Makefile.am,configure.ac
			
已经存在Makefile.am,如何生成Makefile? 步骤: [root@localhost hello]# autoscan .///在当前文件夹中搜索 [root@localhost hel ...
 - springmvc的jar包
			
<!-- spring框架的的组件构成(springFramework)--> 一.核心部分Core Container 1.1 spring-core,spring-beans 提供控 ...
 - jsp中的setHeader页面跳转备忘录
			
1 <!-- response.setHeader("refresh","3;url=你想跳的页面")--> <%-- response.se ...
 - bzoj2431: [HAOI2009]逆序对数列(前缀和优化dp)
			
2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 2312 Solved: 1330[Submit][Stat ...
 - Sara Cope关于text-shadow的介绍
			
作者:Sara Cope p { text-shadow: 1px 1px 1px #000; } 你可以通过逗号“,”应用多个文本阴影. p { text-shadow: 1px 1px 1px # ...
 - Python多线程、多进程
			
1.from multiprocessing import Process ; from threading import Thread 2.进程之间的数据传输 ,一般会使用到pipes, qu ...
 - python常见的加密方式
			
1.前言 我们所说的加密方式都是对二进制编码的格式进行加密,对应到python中,则是我妈们的bytes. 所以当我们在Python中进行加密操作的时候,要确保我们的操作是bytes,否则就会报错. ...
 - synchronized关键字详解(一)
			
synchronized官方定义: 同步方法支持一种简单的策略防止线程干扰和内存一致性错误,如果一个对象对多个线程可见,则对该对象变量的所有读取或写入都是通过同步方法完成的(这一个synchroniz ...
 - spring编程框架
			
spring boot, spring data, spring framework spring / spring boot @Profile('prod'|'dev'|'other')(伴随@Be ...