Codeforces Round #375 (Div. 2)E. One-Way Reform
2024-10-18 03:05:34
题目链接:传送门
题目大意:一副无向图,要求你给边定向(变为有向图),使出度等于入度的点最多,输出有多少
个点,并且输出定向后的边(前为起点,后为终点)
题目思路:欧拉路
我们这样考虑,先考虑无向图的点的度数,如果为奇数则一定无法变为题目要求的点,ans-1
对于度为偶数的点则一定可以通过调整满足。
处理方法:新建一个虚拟节点0,使所有度为奇数的点向其连一条边,那么最终图中的点的度数都为偶数。
这样就满足欧拉路的条件了。我们只需要跑欧拉路并且将走过的路径保留下来即可。
注意将与虚拟节点连的边删去。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 50005
#define maxn 30010
typedef pair<int,int> PII;
typedef long long LL;
LL read(){
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,k,ans,in[];
int vis[][];
set<int>S[];
vector<PII >V;
void dfs(int u){
while(S[u].size()){
int x=*S[u].begin();S[u].erase(S[u].begin());
if(vis[u][x])continue;
vis[u][x]=vis[x][u]=;
V.push_back(make_pair(u,x));
dfs(x);
}
}
int main(){
int i,j,group,x,y,v,Case=;
group=read();
while(group--){
n=read(),m=read();
mst(in,);V.clear();
mst(vis,);
for(i=;i<=n;++i) S[i].clear();
for(i=;i<=m;++i){
x=read(),y=read();
S[x].insert(y),S[y].insert(x);
++in[x],++in[y];
}
int ans=n;
for(i=;i<=n;++i)if(in[i]&){
--ans;
S[].insert(i),S[i].insert();
}
for(i=;i<=n;++i)
if(S[i].size())
dfs(i);
printf("%d\n",ans);
for(PII u:V)if(u.fi!=&&u.se!=){
printf("%d %d\n",u.fi,u.se);
}
}
return ;
}
最新文章
- ";我爱记单词";测试报告兼功能展示
- 最长公共上升子序列(LCIS)
- 021ARM处理器工作模式
- Netsharp快速入门(之14) 销售管理(报表A 热销滞销品统计)
- http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html
- lightOJ 1326 Race(第二类Stirling数)
- C笔记01:关于printf函数输出先后顺序的讲解
- Java双向链表实现
- 设置c#windows服务描述及允许服务与桌面交互的几种方法(转)
- Sqrt(x) 牛顿迭代法
- AspNetPager使用指南
- python实现:最长子字符串
- SWUST OJ(1038)
- 【页面加载】【九九乘法表】【document.write的功能_】【<;script>;直接显示数组】【声明新变量】
- SIP协议流程
- ES6 学习笔记(整理一遍阮一峰大神得入门文档,纯自己理解使用)
- Mysql 实列结构-进程
- 每个Android开发者必须知道的内存管理知识
- 国内四大炒股软件APP 全面技术解析
- Linux的进程与服务(二)
热门文章
- oracle 字符集 AL32UTF8、UTF8
- jquery轮播控件
- elasticsearch安装与使用(4)-- 安装中文分词插件elasticsearch 的 jdbc
- org.hibernate.hql.internal.ast.QuerySyntaxExceptionunexpected token: on near line 1
- 【转】 web 测试使用的chrome插件
- 关于Cocos2d-x中根据分数增加游戏难度的方法
- Android BitmapFactory
- debian下系列下的apt-get 命令与deb包的手动安装的dpkg命令
- 深入浅出Redis-redis哨兵集群[转]
- 反编译工具 jad