CodeForces 723E One-Way Reform
构造。
有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等。
首先统计每个节点的度数,将度数为奇数的节点与编号为$n+1$的节点连边,这样一来,这张新图变成了每个节点的度数都是偶数的图,必然存在欧拉回路。任意一条欧拉回路都使得原图中度数为偶数的节点的出度与入度相等。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int T,n,m,ans,cnt;
vector<int>v[];
int f[];
int ss[],ee[],t[][],p[][]; void dfs(int x)
{
for(int i=v[x].size()-;i>=;i--)
{
if(t[x][v[x][i]]==) continue; t[x][v[x][i]]=t[v[x][i]][x]=;
ss[cnt]=x; ee[cnt]=v[x][i]; cnt++; dfs(v[x][i]);
}
} int main()
{
cin>>T;
for(int cas=;cas<=T;cas++)
{
cin>>n>>m; for(int i=;i<=n+;i++) v[i].clear(); memset(f,,sizeof f);
memset(p,,sizeof p); for(int i=;i<=m;i++)
{
int a,b; cin>>a>>b; v[a].push_back(b);
v[b].push_back(a);
f[a]++; f[b]++;
p[a][b]=p[b][a]=;
} ans=; vector<int>x;
for(int i=;i<=n;i++)
{
if(f[i]%==) ans++;
else x.push_back(i);
} for(int i=;i<x.size();i++)
{
v[n+].push_back(x[i]);
v[x[i]].push_back(n+);
} memset(t,,sizeof t); cnt=;
for(int i=;i<=n+;i++) dfs(i); printf("%d\n",ans); for(int i=;i<m+(n-ans);i++)
{
if(p[ss[i]][ee[i]]==)
printf("%d %d\n",ss[i],ee[i]);
} }
return ;
}
CodeForces 723E One-Way Reform的更多相关文章
- codeforces 723E:One-Way Reform
Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...
- 【codeforces 723E】One-Way Reform
[题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- codeforces 659E E. New Reform(图论)
题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【codeforces 732F】Tourist Reform
[题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...
- Codeforces 723e [图论][欧拉回路]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给你一个有n个点,m条边的无向图,给每条边规定一个方向,使得这个图变成有向图,并且使得尽可能多的点入度与出度相同. 输出有多少个这样的点并且输出 ...
- codeforces723E
One-Way Reform CodeForces - 723E There are n cities and m two-way roads in Berland, each road connec ...
- Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径
E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...
- Codeforces Round #346 (Div. 2) E. New Reform dfs
E. New Reform 题目连接: http://www.codeforces.com/contest/659/problem/E Description Berland has n cities ...
随机推荐
- Cycle Sort
Cycle sort的思想与计数排序太像了,理解了基数排序再看这个会有很大的帮助, 圈排序与计数排序的区别在于圈排序只给那些需要计数的数字计数,先看完文章吧,看完再回来理解这一句话 所谓的圈的定义,我 ...
- 元类编程--__getattr__, __getattribute__
#__getattr__, __getattribute__ #__getattr__ 就是在查找不到属性的时候调用 from datetime import date class User: def ...
- 洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging
题目描述 Bessie has taken heed of the evils of sloth and has decided to get fit by jogging from the barn ...
- python学习笔记(四)之分支和循环
python中比较操作符有: > >= < <= == != 这些操作符返回True或False 1 >>> 1 < 3 2 True 3 >&g ...
- Fetch-新一代Ajax API
AJAX半遮半掩的底层API是饱受诟病的一件事情. XMLHttpRequest 并不是专为Ajax而设计的. 虽然各种框架对 XHR 的封装已经足够好用, 但我们可以做得更好. window.fet ...
- 编译zpool命令
环境:192.168.50.239(在 illumos源码中编译zpool命令) PS:由于对zpool命令的工作原理不熟悉,所以编译,可在其中加入调试语句来明白其原理 首先介绍 illumos-so ...
- hdu 3371(prim算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Jav ...
- eclipse执行maven install命令时跳过test
在pom.xml里面配置一下代码,将跳过test. <plugins> <plugin> <groupId>org.apache.maven.plugins< ...
- mysql 1366的错误 字符集错误解决方案
最近用mysqlalchmy的时候遇到了 sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1366, "Incorrec ...
- elasticsearch索引加别名
curl -XPOST 'http://localhost:9200/_aliases' -d ' { "actions": [ {&qu ...