2015 多校联赛 ——HDU5348(搜索)
You are given an undirected graph with n vertexs
and m edges.
Please direct all the edges so that for every vertex in the graph the inequation |out degree − in degree|≤1 is
satisified.
The graph you are given maybe contains self loops or multiple edges.
indicating the number of testcases.
For each test case, the first line contains two integers n and m.
And the next m lines,
each line contains two integers ui and vi,
which describe an edge of the graph.
T≤100, 1≤n≤105, 1≤m≤3∗105, ∑n≤2∗105, ∑m≤7∗105.
otherwise output m lines,.
In ith
line contains a integer 1 or 0, 1 for
direct the ith
edge to ui→vi, 0 for ui←vi.
3 3
1 2
2 3
3 1
7 6
1 2
1 3
1 4
1 5
1 6
1 7
1
1
0
1
0
1
0
1
题:给出n个点,m条无向边,现在要求将无向边变为有向边,要保证每个点的出度和入度的差不超过1
思路来自:http://blog.csdn.net/winddreams/article/details/47281397
直接进行搜索,对每个点进行出度和入度的判断,如果出度大,就先进行反向的搜索(每搜索一条边u,v就认为这是一条v到u的有向边),反之,进行正向搜索(每搜到一条边u,v认为这是一条u到v的有向边),一直搜索到找不到边能继续为止。
对于已经判断过的边删去:head[u] = edge[i].next;
证明不会有-1的情况,对于一个点v,假设入度比出度多2,那么,第一:就会有一条边搜索到v后找不到后继,导致v的入度比出度多1,第二:又有一条边搜索到v,导致v的入度比出度多2,但是这样的话就会和第一条找不到后继冲突,所以不会出现入度比出度多2的情况,(其他情况也是类似),所以不会有-1的结果。
(果然稍难一点自己就不知道怎么办了 好坑orz)
#include <iostream>
#include <cstdio>
#include<algorithm>
#include<cstring>
#include<functional>
#include<queue>
typedef long long ll;
using namespace std; struct node
{
int u,v,ci;
int next;
} edge[700000];
int n,m,to;
int head[100010] , vis[700000] ;
int in[100010] , out[100010] , num[100010] ;
int ans[700000] ; void add(int u,int v,int c)
{
edge[to].u= u;
edge[to].v= v;
edge[to].ci = c;
edge[to].next = head[u];
head[u] = to++;
} void dfs1(int u) //正向搜索
{
for(int i = head[u]; ~i; i = edge[i].next)
{
if(vis[i])
{
head[u] = edge[i].next; //删边
continue;
}
int v = edge[i].v;
if(u != v && in[v] > out[v]) //u->v,in[v] > out[v]时,跳过
continue;
vis[i] = vis[i^1] = 1; //将u,v之间的两条边标记
if(i %2) //有输入可知,i为偶时u->v; i为奇时,v->u
ans[i/2] = 0;
else
ans[i/2] = 1;
out[u]++;
in[v]++;
head[u] = edge[i].next;
dfs1(v);
break;
}
} void dfs2(int u)
{
for(int i = head[u]; ~i; i = edge[i].next)
{
if(vis[i])
{
head[u] = edge[i].next;
continue;
}
int v = edge[i].v;
if(u != v && in[v] < out[v])
continue;
vis[i] = vis[i^1] = 1;
if(i %2)
ans[i/2] = 1;
else
ans[i/2] = 0;
out[v]++;
in[u]++;
head[u] = edge[i].next;
dfs2(v);
break;
}
} int main()
{
int T;
int a, b;
scanf("%d",&T);
while(T--)
{
to = 0;
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
for(int i = 0;i <= n;i++)
{
in[i] = out[i] = num[i] = 0;head[i] = -1;
}
for(int i = 0; i < m; i++)
{
scanf("%d%d",&a,&b);
add(a,b,i);
add(b,a,i);
num[a]++;
num[b]++;
} for(int i = 1; i <= n; i++)
{
while(in[i] + out[i] < num[i])
{
if(in[i] >= out[i]) //正反不停搜,直到找不到边为止
dfs1(i);
else
dfs2(i);
}
}
for(int i = 0; i < m; i++)
printf("%d\n",ans[i]);
}
return 0;
}
2015 多校联赛 ——HDU5348(搜索)的更多相关文章
- 2015 多校联赛 ——HDU5323(搜索)
Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 2015 多校联赛 ——HDU5305(搜索)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- 2015 多校联赛 ——HDU5334(构造)
Virtual Participation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 2015 多校联赛 ——HDU5335(Walk out)
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...
- 2015 多校联赛 ——HDU5302(构造)
Connect the Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2015 多校联赛 ——HDU5294(最短路,最小切割)
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 2015 多校联赛 ——HDU5325(DFS)
Crazy Bobo Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Tota ...
- 2015 多校联赛 ——HDU5316(线段树)
Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...
- 2015 多校联赛 ——HDU5319(模拟)
Painter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
随机推荐
- 使用Github pages+jekyll搭建自己的博客(windows版)
最近突发奇想,想试试GitHub pages来搭建博客.网上一搜一大堆,嗯...看来还是挺简单的...于是自己撸起袖子干...... 结果对于我这种GitHub注册过,git 没用过,ruby.jek ...
- CSS揭秘(三)形状
Chapter 3 1. 椭圆 椭圆的实现主要依靠 border-radius 属性,该属性确定边框切圆角的半径大小,可以指定数值 px,也可以使用百分比显示 而且该属性非常灵活,四个角可以分别设置 ...
- python小练习之三---购物车程序
购物车购物的例子 严格来讲,这个例子相对大一些 功能也稍完备一些,具有用户登录,商品上架,用户购物,放入购物车,展示每个用户的购物车里的商品的数量,用户账户余额,支持用户账户充值等 下面展示的代码有些 ...
- pymysql安装和使用
一.pymysql安装 安装mymysql前请确认python环境已经准备好,在之前的博文http://www.cnblogs.com/newzol/p/8682176.html有说明pythonwe ...
- Netty事件监听和处理(上)
陪产假结束了,今天又开始正常上班了,正好赶上米粉节活动,又要忙上一阵了,米粉节活动时间为4.03 - 4.10,有不少优惠,感兴趣的可以关注mi.com或小米商城app. 今天给大家送了福利:小爱音箱 ...
- GIT入门笔记(3)- git中的一些概念和原理
一.git管理过程中所处的4个阶段: 工作目录(workspace) 暂存区(index) 本地仓库(local repository) 远程仓库(remote repository) 二.工作目录+ ...
- Docker学习笔记 - Docker客户端和服务端
学习内容: Docker客户端和服务端的通讯方式:client和自定义程序 Docker客户端和服务端的连接方式:socket 演示Docker客户端和服务端之间用remote-api通讯:nc ...
- My97设置开始、结束 时间区间及输入框不能输入只能选择的方法
时间区间开始: <input type="text" id = "first_time" name="first_time" valu ...
- 爬虫必备 User-Agent 列表
USER_AGENTS = [ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR ...
- 基于哈夫曼编码的文件压缩(c++版)
本博客由Rcchio原创 我了解到很多压缩文件的程序是基于哈夫曼编码来实现的,所以产生了自己用哈夫曼编码写一个压缩软件的想法,经过查阅资料和自己的思考,我用c++语言写出了该程序,并通过这篇文章来记录 ...