[Codeforces 858] [Technocup 2018 Qual 1] F. Wizard's Tour

26 篇文章 0 订阅
22 篇文章 0 订阅
Codeforces传送门

Description

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.

Input

The first line contains two integers n, m (1 ≤ n ≤   2 × 1 0 5  2\times 10^5 2×105, 0 ≤ m ≤  2 × 1 0 5 2\times 10^5 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 a i ,   b i a_i, b_i ai,bi ( 1   ≤   a i ,   b i   ≤   n , a i   ≠   b i 1 ≤ a_i, b_i ≤ n, a_i ≠ b_i 1ai,bin,ai̸=bi), where a i a_i ai and b i b_i 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 1 1 to n n n.

It is possible that the road network in Berland is not connected.

Output

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.

Examples

Input 1
4 5
1 2
3 2
2 4
3 4
4 1
Output1
2
1 4 2
4 3 2
Input 2
5 8
5 3
1 2
4 5
5 1
2 5
4 3
1 4
3 2
Output 2
4
1 4 5
2 3 4
1 5 3
5 2 1

翻译

给你一个 n n n个点, m m m条边的无向图, 每次可以删掉相邻的两条边, 问最多能操作多少次, 并给出方案。

解题分析

考虑 D F S DFS DFS的时候会生成一个树形的结构:

剩下的边都连接同一子树内祖先节点和子节点, 不会出现横跳的情况。 我们就可以贪心在深度更深的位置优先使用这些边, 如果其它可用边为奇数条就将与父节点的边先用了。 这样做可以达到 ∑ ⌊ S 2 ⌋ \sum\lfloor\frac{S}{2}\rfloor 2S次操作( S S S为联通块大小)。

代码如下:

#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 200500
template <class T>
IN void in(T &x)
{
	x = 0; R char c = gc;
	for (; !isdigit(c); c = gc);
	for (;  isdigit(c); c = gc)
	x = (x << 1) + (x << 3) + c - 48;
}
int dot, line, cnt = -1, top, ct;
struct Edge {int to, nex;} edge[MX << 1];
struct INFO {int to, id;} sta[MX];
struct Ans {int from, mid, to;} ans[MX];
int head[MX];
bool used[MX << 1], vis[MX];
IN void add(R int from, R int to)
{edge[++cnt] = {to, head[from]}, head[from] = cnt;}
void DFS(R int now, R int fa, R int id)
{
	vis[now] = true;
	for (R int i = head[now]; ~i; i = edge[i].nex)
	{
		if(edge[i].to == fa) continue;
		if(!vis[edge[i].to]) DFS(edge[i].to, now, i);
	}
	top = 0;
	for (R int i = head[now]; ~i; i = edge[i].nex)
	if(!used[i] && edge[i].to != fa) sta[++top] = {edge[i].to, i};
	W (top > 1) 
	{
		ans[++ct] = {sta[top - 1].to, now, sta[top].to};
		used[sta[top - 1].id] = used[sta[top - 1].id ^ 1] = true;
		used[sta[top].id] = used[sta[top].id ^ 1] = true;
		top -= 2;
	}
	if(top)
	{
		if(!fa) return;
		ans[++ct] = {sta[top].to, now, fa};
		used[sta[top].id] = used[sta[top].id ^ 1] = true;
		used[id] = used[id ^ 1] = true;
	}
}
int main(void)
{
	std::memset(head, -1, sizeof(head));
	int a, b;
	in(dot), in(line);
	for (R int i = 1; i <= line; ++i)
	in(a), in(b), add(a, b), add(b, a);
	for (R int i = 1; i <= dot; ++i)
	if(!vis[i]) DFS(i, 0, 0);
	printf("%d\n", ct);
	for (R int i = 1; i <= ct; ++i) printf("%d %d %d\n", ans[i].from, ans[i].mid, ans[i].to);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值