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 ...
随机推荐
- vue2 前端搜索实现
项目数据少的时候,搜索这样的小事情就要交给咱们前端来做了,重要声明,适用于小项目!!!!! 其实原理很简单,小demo是做搜索市区名称或者按照排名搜索. <div> <input t ...
- Android 4.4 沉浸式透明状态栏
原文链接:http://www.bkjia.com/Androidjc/913061.html 第一种方法 这里写代码片第一种方法,在代码设置: if(VERSION.SDK_INT >= VE ...
- AWS EC2服务器的HTTPS负载均衡器配置过程
AWS EC2服务器配置负载均衡器步骤: 1.普通负载均衡器 至少两台EC2实例,这里以Centos6.7系统为例 启动之后先安装个apache的httpd服务器默认80端口,或者使用其他服务 ...
- XML之自动生成类,添加,修改,删除类的属性
1. class ClassHelperDemo { public static void Main() { #region 演示一:动态生成类. //生成一个类t. Type t = ClassHe ...
- Ubuntu 17.10.1安装, 定制
p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 2018.4.7 Ubuntu 17.10.1安装, 定制, 后续搭建LAMP环境 ...
- vue jsx 使用指南
vue jsx 使用指南 vue jsx 语法与 react jsx 还是有些不一样,在这里记录下. let component = null // if 语句 if (true) { compone ...
- Mybatis的原始dao开发方法
在进入主题之前先提一下sqlSession.sqlSession是一个面向用户(程序员)的接口. sqlSession中提供了很多操作数据库的方法,如: selectOne(返回单个对象).selec ...
- Python内置函数(60)——compile
英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...
- axure 预览"HTTP/1.1 302 Found"
使用Axure编辑原型时,点击预览出现"HTTP/1.1 302 Found" 第一想到的就是重新安装Axure和检查原型文件是否损坏,验证后证明前Axure和.rp文件都是完好的 ...
- unity3D 知识点随手记
最近闲来无事,记记unity3D相关的一些知识点吧,也当作笔记存储.转载请标明出处:http://www.cnblogs.com/zblade/ 1.unity是如何调用Start/Awake等相关函 ...