Codeforces723E One-Way Reform【欧拉回路】
题意:给你n点m边的图,然后让你确定每条边的方向,使得入度=出度的点最多 。
度数为偶数的点均能满足入度 = 出度。
证明:度数为奇数的点有偶数个,奇度点两两配对连无向边,则新图存在欧拉回路,则可使新图所有点入度 = 出度。
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
typedef long long ll;
const int N = ;
int d[N], head[N], tot;
int to[N*N], nex[N*N], vis[N*N];
vector< pair<int, int> > ans;
void init(){
tot = ;
memset(d, , sizeof(d));
memset(head, -, sizeof(head));
ans.clear();
}
void add(int u, int v, int w){// w = 0, exit; w = -1, add
d[v]++;
to[tot] = v;
nex[tot] = head[u];
vis[tot] = w;
head[u] = tot++;
}
void dfs(int x){
for(int& i = head[x]; ~i; ){
if(vis[i] == ) {
i = nex[i];
continue ;
}
if(vis[i] == ) ans.push_back( make_pair(x, to[i]) );
vis[i] = vis[i^] = ;
dfs(to[i]);
}
} int main(){
int t; scanf("%d", &t);
while(t--){
init();
int n, m, u, v;
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++){
scanf("%d%d", &u, &v);
add(u, v, );
add(v, u, );
}
vector<int> tmp;
int ret = n;
for(int i = ; i <= n; i++)
if(d[i]&) tmp.push_back(i), ret--;
for(int i = ; i < tmp.size(); i += ){
add(tmp[i], tmp[i+], -);
add(tmp[i+], tmp[i], -);
}
for(int i = ; i <= n; i++)
dfs(i);
printf("%d\n", ret);
for(int i = ; i < ans.size(); i++)
printf("%d %d\n", ans[i].X, ans[i].Y);
}
return ;
}
Codeforces723E One-Way Reform【欧拉回路】的更多相关文章
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- URAL 1137 Bus Routes(欧拉回路路径)
1137. Bus Routes Time limit: 1.0 secondMemory limit: 64 MB Several bus routes were in the city of Fi ...
- One-Way Reform
One-Way Reform time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)
//网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- UVA 10054 the necklace 欧拉回路
有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...
- POJ 1637 混合图的欧拉回路判定
题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...
- UVa 12118 检查员的难题(dfs+欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- C#:文件、路径(Path_File)
public class Path_File { public string AppPath { get { return AppDomain.CurrentDomain.BaseDirectory; ...
- ACM题目————已知前序和中序求后序
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ], z ...
- Android onTouchEvent, onClick及onLongClick的调用机制
针对屏幕上的一个View控件,Android如何区分应当触发onTouchEvent,还是onClick,亦或是onLongClick事件? 在Android中,一次用户操作可以被不同的View按次序 ...
- jqeury之平移轮播
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- poj3342 Party at Hali-Bula
树形dp题,状态转移方程应该很好推,但一定要细心. http://poj.org/problem?id=3342 #include <cstdio> #include <cstrin ...
- muduo库安装
一.简介 Muduo(木铎)是基于 Reactor 模式的网络库. 二.安装 从github库下载源码安装:https://github.com/chenshuo/muduo muduo依赖了很多的库 ...
- Oracle之ORA-00972: identifier is too long
一.前言 今天在程序的日志中出现这个错误,网上搜了一下发现,说是Oracle的对象名字最多是30个字符,不能超过30,而我出错的sql是: "select * from test where ...
- torch基本操作
1.在terminal中th进入troch,th+文件名.lua运行文件.进入torch之后,dofile+"文件名.lua"运行文件
- 几篇不错的基础css博客转载
CSS 巧用 :before和:after:http://web.jobbole.com/85083/ css清除元素间距:http://ouvens.github.io/frontend-css/2 ...
- Educational Codeforces Round 1 A
In this problem you are to calculate the sum of all integers from 1 to n, but you should take all po ...