[Codeforces 858] [Technocup 2018 Qual 1] F. Wizard's Tour
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 ≤, 0 ≤ m ≤ ) — 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 (), where and 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 to .
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
翻译
给你一个个点, 条边的无向图, 每次可以删掉相邻的两条边, 问最多能操作多少次, 并给出方案。
解题分析
考虑的时候会生成一个树形的结构:
剩下的边都连接同一子树内祖先节点和子节点, 不会出现横跳的情况。 我们就可以贪心在深度更深的位置优先使用这些边, 如果其它可用边为奇数条就将与父节点的边先用了。 这样做可以达到次操作(为联通块大小)。
代码如下:
#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);
}