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& ...
随机推荐
- 利用python进行数据分析 (学习笔记)
第一章:准备工作 1.重要的Python库 (1)NumPy:Python科学计算的基础包.功能有:
- strong和copy的区别
问题描述 在定义一个类的property时候,为property选择strong还是copy特别注意和研究明白的,如果property是NSString或者NSArray及其子类的时候,最好选择使用c ...
- HDU 2236:无题II(二分搜索+二分匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2236 题意:中文题意. 思路:先找出最大和最小值,然后二分差值,对于每一个差值从下界开始枚举判断能不能二分匹配. ...
- C#:通过Window API接口实现WiFi
1.获取Mac地址 //WiFi通知回调 private WlanApi.WLAN_NOTIFICATION_CALLBACK _notificationCallback; this._notific ...
- JAVA中int、String的类型相互转换
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- css2---必须学的经典---定位问题
定位 position body<html<文档 定位是相对文档的 不是相对 body 或 html 1.position:relative 相对定位 a.不能使内嵌元素支持宽高 b. ...
- PHP生成 excl、word文件
PHP生成 excl.word文件 $time = time(); $filename = date("Y年m月d日h点m分s秒", $time).'问卷数据'; $rows ...
- OracleHelper
/// <summary> /// OracleServer数据库访问的通用工具类 /// </summary> public abstract class OracleHel ...
- 洛谷P1017 进制转换
洛谷P1017 进制转换 题目描述 我们可以用这样的方式来表示一个十进制数: 将每个阿拉伯数字乘以一个以该数字所处位置的(值减1)为指数,以10为底数的幂之和的形式.例如:123可表示为 \(1*10 ...
- 样式其他与JS脚本语言
样式其他:display(显示block和隐藏none,不占位置) visibility(显示visible和隐藏hidden,占位置) overflow(超出范围 hidden隐藏) 透明(op ...