「CF858F」 Wizard's Tour
传送门
Luogu
解题思路
首先对于树的情况,我们很显然有一种贪心策略:
对于每一个节点先匹配子树,然后在还可以匹配的儿子间尽可能匹配,要是多出来一个就往上匹配。
推广到图的情况。。。
我们在图的生成树上 \(\text{DFS}\) ,即时删边,防止重复访问。
然后记录一个 \(f[x]\),表示直接与 \(x\) 节点并且还可以用来匹配的点。
那么我们直接一边 \(\text{DFS}\) ,一边匹配就好了。
最后输出答案即可。
细节注意事项
- 没什么细节,就是要仔细想一想
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <set>
#define rg register
#define file(x) \
freopen(x".in", "r", stdin); \
freopen(x".out", "w", stdout);
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 200010;
const int __ = 400010;
int tot = 1, head[_], nxt[__], ver[__];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
int n, m, vis[_] = { 1 }, f[_];
int cnt; struct node{ int x, y, z; }t[_];
inline void dfs(int u) {
vis[u] = 1;
for (rg int v, i = head[u]; i; i = nxt[i]) {
v = ver[i], ver[i] = ver[i ^ 1] = 0;
if (v != 0) {
if (!vis[v]) dfs(v);
if (f[v]) t[++cnt] = (node) { u, v, f[v] }, f[v] = 0;
else if (f[u]) t[++cnt] = (node) { f[u], u, v }, f[u] = 0;
else f[u] = v;
}
}
}
int main() {
read(n), read(m);
for (rg int u, v, i = 1; i <= m; ++i)
read(u), read(v), Add_edge(u, v), Add_edge(v, u);
for (rg int i = 1; i <= n; ++i) if (!vis[i]) dfs(i);
printf("%d\n", cnt);
for (rg int i = 1; i <= cnt; ++i)
printf("%d %d %d\n", t[i].x, t[i].y, t[i].z);
return 0;
}
完结撒花 \(qwq\)
「CF858F」 Wizard's Tour的更多相关文章
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
- 「2014-3-13」Javascript Engine, Java VM, Python interpreter, PyPy – a glance
提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...
随机推荐
- Charles 查看https请求数据 Mac/ android
Charles_v4.0.1_Mac_破解版下载地址:https://pan.baidu.com/s/1c23VPuS 1.在Mac电脑上安装Charles的根证书 打开Charles->菜单H ...
- vue mvvm原理与简单实现 -- 上篇
Object.defineProperty介绍-- let obj = {}; Object.defineProperty(obj,'school',{ configurable : true, // ...
- 用OpenCV读取摄像头
首先插入摄像头 在电脑中查看 #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp ...
- 阻止click点击事件
遇到一个屏蔽点击事件,以前一般都是通过js控制,阻止事件,今天看到css加一个样式就能屏蔽,来记录一下 //css禁用鼠标点击事件 .test { pointer-events: none; } 随便 ...
- gradle-技能保存
gradle编译java springboot,指定使用哪个环境配置文件 首先在build.gradle里面声明一个变量 def profileName = project.hasProperty(& ...
- 【Python】 注释
确保对模块, 函数, 方法和行内注释使用正确的风格 Python中的注释有单行注释和多行注释: Python中单行注释以 # 开头,例如:: # 这是一个注释 print("Hello, W ...
- pyodbc-的一些说明
cursor的description 可以获得一些关于表的信息 info=cursor.description 此时表中有多少列就有多少个元素,一个元素就是一列的信息(格式是tuple),所以这里的i ...
- python 让异常名称显示出来
一 try: pass except Exception as e: print(e) 二 import sys try: pass except: print(sys.exc_info()) 下面有 ...
- 关于使用ssm与spring时,配置tomcat 虚拟目录( doBase )中的一些坑
一.使用SSM需要 配置虚拟目录时 tomcat的配置 在tomcat server.xml的<HOST></HOST>中加入以下内容 在配置完成之后,当我们访问URL 为 ...
- frm、myd、myi、opt、par文件
.frm 表结构文件 .myd 表数据文件 .myi 表索引文件 .opr 储存数据库的默认字符集 .par 储存分区信息 mysql 5.6版本分区表有一个文件:表名.par, 该文件在5.7.6版 ...