Graph Cutting

不会写。。

dfs的过程中把回边丢到它的祖先中去, 回溯的时候两两配对。感觉好神奇啊。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m;
bool vis[N];
vector<int> G[N];
queue<int> que[N];
vector<pair<int, PII>> ans; void dfs(int u, int fa) {
vis[u] = true;
for(auto& v : G[u]) {
if(!vis[v]) {
dfs(v, u);
} else if(v != fa && vis[v]) {
que[v].push(u);
}
}
while(SZ(que[u]) >= ) {
int x = que[u].front(); que[u].pop();
int y = que[u].front(); que[u].pop();
ans.push_back(mk(x, mk(u, y)));
}
if(SZ(que[u])) {
if(!fa) {
puts("No solution");
exit();
}
int x = que[u].front(); que[u].pop();
ans.push_back(mk(x, mk(u, fa)));
} else {
que[fa].push(u);
}
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
if(m & ) puts("No solution");
else {
dfs(, );
for(auto& t : ans)
printf("%d %d %d\n", t.fi, t.se.fi, t.se.se);
}
return ;
} /*
*/

Codeforces 405E Graph Cutting的更多相关文章

  1. Codeforces 538 A. Cutting Banner-substr()函数字符串拼接

      A. Cutting Banner   time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. CodeForces 1118F2. Tree Cutting (Hard Version)

    题目简述:给定$n \leq 3 \times 10^5$个节点的树,其中一部分节点被染色,一共有$k$种不同的颜色.求将树划分成 $k$ 个不相交的部分的方案数,使得每个部分中除了未染色的节点以外的 ...

  3. [Codeforces 623A] Graph and String

    [题目链接] http://codeforces.com/contest/623/problem/A [算法] 首先 , 所有与其他节点都有连边的节点需标号为'b' 然后 , 我们任选一个节点 , 将 ...

  4. Codeforces 990D - Graph And Its Complement

    传送门:http://codeforces.com/contest/990/problem/D 这是一个构造问题. 构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b. 对于 ...

  5. 【codeforces 794B】Cutting Carrot

    [题目链接]:http://codeforces.com/contest/794/problem/B [题意] 给你一个等腰三角形; 它的底边为1; 高为h; 要求你把这个等腰三角形分成n份面积相等的 ...

  6. CodeForces 990D Graph And Its Complement(图和补图、构造)

    http://codeforces.com/problemset/problem/990/D 题意: 构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b. 题解: 第一眼看到 ...

  7. Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)

    Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...

  8. codeforces 624C Graph and String

    C. Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. codeforces 623A. Graph and String 构造

    题目链接 给出一个图, 每个节点只有三种情况, a,b, c. a能和a, b连边, b能和a, b, c,连边, c能和b, c连边, 且无重边以及自环.给出初始的连边情况, 判断这个图是否满足条件 ...

随机推荐

  1. MT【187】余弦的线性组合

    已知$\alpha+\beta+\gamma=\pi,(\alpha,\beta,\gamma\ge0)$ 求:$3\cos\alpha+4\cos\beta+5\cos\gamma$的最大值____ ...

  2. C/C++ Bug记录

    链接错误解决方法 1. 检查库是否链接正确 2. 库的顺序是否合理(如果库之间有依赖关系的话) 3. 检查obj文件是否存在既有C编译的,也有C++编译的,如果存在,用extern "C&q ...

  3. 洛谷 P4070 [SDOI2016]生成魔咒 解题报告

    P4070 [SDOI2016]生成魔咒 题目描述 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 \(1\).\(2\) 拼凑起来形成一个魔咒串 \([1,2]\). 一个魔咒 ...

  4. 用HashSet存储不重复的对象

    直接进入主题,先来创建一个类:Bean public class Bean { private int id; private String name; public Bean() { super() ...

  5. 在CentOS上导出JVM内存信息

    首先看下Tomcat的进程Id: [root@iZ25Z ~]# ps aux | grep java www 2111 4.0 23.5 1637648 452756 ? Sl 10:12 4:35 ...

  6. Bugfree——CentOS6.8搭建测试环境

    参考资料:http://blog.csdn.net/qq_29227939/article/details/52295917 BugFree基于PHP和MySQL开发,是免费且开发源代码的缺陷管理系统 ...

  7. bzoj千题计划289:bzoj 2707: [SDOI2012]走迷宫

    http://www.lydsy.com/JudgeOnline/problem.php?id=2707 dp[i] 表示从点i到终点的期望步数 dp[i]= Σ (dp[j]+1)/out[i] j ...

  8. Codeforces 923 C. Perfect Security

    http://codeforces.com/contest/923/problem/C Trie树 #include<cstdio> #include<iostream> us ...

  9. 转自知乎大神----JS 的 new 到底是干什么的?

    大部分讲 new 的文章会从面向对象的思路讲起,但是我始终认为,在解释一个事物的时候,不应该引入另一个更复杂的事物. 今天我从「省代码」的角度来讲 new. --------------------- ...

  10. 略显犀利的 js 判断闰年

    /** * 判断闰年函数 * @param {number} year 要判断的年份 * @return {bool} 返回布尔值 * * 其实只要满足下面几个条件即可. * 1.普通年能被4整除且不 ...