【Codeforces858F】Wizard's Tour [构造]
Wizard's Tour
Time Limit: 50 Sec Memory Limit: 512 MB
Description

Input

Output

Sample Input
4 5
1 2
3 2
2 4
3 4
4 1
Sample Output
2
4 1 2
4 3 2
HINT

Solution
首先,一个连通块的答案可以是floor(m / 2)。考虑如何构造出一种解。
首先我们先搞出一个dfs树。
那么现在对于一个点,有三种边:1. 非树边;2. 儿子边;3. 父亲边。
我们将非树边和儿子边的优先级看做一样的,父亲边优先级最低。
考虑将边配给点,即这个点是一种走法中的中点。从叶子节点往上做。两两配对这些边。
显然每条边都被尽可能利用了,最后只有与根相连的边可能会有最多一条用不了。
这样就是一种解了。
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64; const int ONE = ;
const int MOD = 1e9 + ; int get()
{
int res = , Q = ; char c;
while( (c = getchar()) < || c > )
if(c == '-') Q = -;
if(Q) res = c - ;
while( (c = getchar()) >= && c <= )
res = res * + c - ;
return res * Q;
} int ans_len;
struct power
{
int l, mid, r;
}Ans[ONE * ]; int n, m;
int x, y;
int next[ONE * ], first[ONE], go[ONE * ], tot;
int vis[ONE * ]; void Add(int u, int v)
{
next[++tot] = first[u], first[u] = tot, go[tot] = v;
next[++tot] = first[v], first[v] = tot, go[tot] = u;
} vector <power> A;
int fat[ONE]; void Dfs(int u)
{
for(int e = first[u]; e; e = next[e])
{
int v = go[e];
if(fat[v] || vis[e]) continue;
fat[v] = u, Dfs(v);
} A.clear();
for(int e = first[u]; e; e = next[e])
if(!vis[e] && fat[u] != go[e]) A.push_back((power){go[e], , e});
for(int e = first[u]; e; e = next[e])
if(!vis[e] && fat[u] == go[e]) A.push_back((power){go[e], , e}); int p = , len = A.size();
for(int j = ; j + < len; j += )
{
vis[A[j].r] = vis[(A[j].r-^)+] = ;
vis[A[j+].r] = vis[(A[j+].r-^)+] = ;
Ans[++ans_len] = (power){A[j].l, u, A[j + ].l};
}
} int main()
{
n = get(); m = get();
for(int i = ; i <= m; i++)
x = get(), y = get(), Add(x, y); for(int i = ; i <= n; i++)
if(!fat[i]) Dfs(i); printf("%d\n", ans_len);
for(int i = ; i <= ans_len; i++)
printf("%d %d %d\n", Ans[i].l, Ans[i].mid, Ans[i].r);
}
【Codeforces858F】Wizard's Tour [构造]的更多相关文章
- Wizard's Tour
F. Wizard's Tour time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Wizard's Tour CodeForces - 860D (图,构造)
大意: 给定$n$节点$m$条边无向图, 不保证连通, 求选出最多邻接边, 每条边最多选一次. 上界为$\lfloor\frac{m}{2}\rfloor$, $dfs$贪心划分显然可以达到上界. # ...
- CF1053E Euler tour 构造
正解:构造 解题报告: 传送门! 这种题目一般都是首先考虑合法性 这题也不例外,思考怎么样是合法的呢? 有四点: 1)a[1]=a[2n-1],显然不说 2)若a[i]=a[j],则(j-i)& ...
- 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\) 条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通. 你想在这张图上进行若干次 ...
- 「CF858F」 Wizard's Tour
传送门 Luogu 解题思路 首先对于树的情况,我们很显然有一种贪心策略: 对于每一个节点先匹配子树,然后在还可以匹配的儿子间尽可能匹配,要是多出来一个就往上匹配. 推广到图的情况... 我们在图的生 ...
- Codeforces Round #434 (Div. 2)
Codeforces Round #434 (Div. 2) 刚好时间对得上,就去打了一场cf,发现自己的代码正确度有待提高. A. k-rounding 题目描述:给定两个整数\(n, k\),求一 ...
- poj 题目分类(2)
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj329 ...
随机推荐
- Scrum 项目7.0——第一个Sprint的演示和回顾
MY—HR 成员: 角色分配 学号 博客园 团队贡献分 丘惠敏 PM项目经理 201406114203 http://www.cnblogs.com/qiuhuimin/ 21 郭明茵 用户 2014 ...
- cxgrid中,如何根据列名或字段名取得footer值
注意,不是根据index取得footer值cxgrdtbv1.DataController.Summary.FooterSummaryValues[0]; ------解决方案------------ ...
- poj 3067 Japan(树状数组求逆序数)
链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...
- SpringBoot(十)_springboot集成Redis
Redis 介绍 Redis是一款开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存也可持久化的日志型.Key-Value高性能数据库. 数据模型 Redis 数据模型不仅与关系数据 ...
- SPOJ Triple Sums(FFT+容斥原理)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream& ...
- InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
在莫烦Python教程的“Dropout 解决 overfitting”一节中,出现错误如下: InvalidArgumentError: You must feed a value for plac ...
- Rabbit and Grass HDU - 1849 (Bash+Nim)
就是Bash 和 Nim 博弈的结合 可以直接 res ^= (Li + 1) % Mi 也可以 sg打个表 我打了个表 #include <iostream> #include &l ...
- 读取Maven项目下resources目录下的配置文件(properties为例)
配置文件:xxxxx.properties a.url=******************** b.url=---------------------------------- 读取配置文件: im ...
- Android Paging库使用详解
Android分页包能够更轻易地在RecyclerView里面缓慢且优雅地加载数据. 许多应用从数据源消耗数据, 数据源里面有大量的数据, 但是一次却只展示一小部分. 分页包帮助应用观测和展示大量数据 ...
- 《Linux内核设计与实现》学习总结 Chap18
一.准备开始 1.一个确定的bug,但大部分bug通常都不是行为可靠且定义明确的. 2.一个藏匿bug的内核版本. 3.相关内核代码的知识和运气. 二.内核中的bug 1.bug的表象: 明白无误的错 ...