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安装包以后,发现原来已经有一个 ...
随机推荐
- POJ3090 Visible Lattice Points 欧拉函数
欧拉函数裸题,直接欧拉函数值乘二加一就行了.具体证明略,反正很简单. 题干: Description A lattice point (x, y) in the first quadrant (x a ...
- Scala 是一门怎样的语言,具有哪些优缺点?
保罗·格雷厄姆在<黑客与画家>中写道,Java属于B&D(捆绑与束缚)类型的语言.为何束缚手脚?因为要让新手和明星程序员写出类似质量的代 码,尽可能的抹消人的才华对程序的影响.不同 ...
- Java开源JSP标签库
01displytag 与Struts结合使用最出名的一个tag主要是显示表格数据很漂亮.完善. 02cewolf tag 用来在web上显示复杂图形报表的一个jsp tag. 03Loading T ...
- PCB OD工具破解实例应用
以下破解Genesis为例,对OD工具使用进行实例讲解 工具简单 介绍下下载地址: OD工具:是一个新的动态追踪工具,将IDA与SoftICE结合起来的思想,Ring 3级调试器, 是为当今最为流行的 ...
- In 7-bit
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3713 题意:给定一个字符串,首先输出这个字符串的长度(以两位的十六进制的形 ...
- shopnc学习
---恢复内容开始--- 以前没有怎么接触过shopnc,感觉界面挺漂亮的,不过后来自己需要开发一个电商系统,就顺便参考了下,感觉构架垃圾的一塌糊涂.不过平时做这个系统二次开发的业务比较多,所以简单的 ...
- input如何去掉边框
outline: none; border:solid 0px; 两个属性,ok.
- GIT分支的一些开发心得
从本地的master分支创建新的分支 $ git checkout -b dev Switched to a new branch 'dev' 上面那条命令可以分为两步 $ git branch de ...
- go 成长路上的坑(1)
一.先来看一段代码 package main import "fmt" type X struct{} func (x *X) test(){ println("h1&q ...
- [转]Android监听ListView里Button事件
本文转自:http://blog.csdn.net/lovediji/article/details/6753349 public View getView(int position, View co ...