题意:给一个无向图,让你指定边的方向,比如a→b为1,a←b为0,在给所有边指定方向后,对无向图上的每个顶点,如果满足|出度-入度|<2,那么输出一种方案。

思路:从结论入手,|出度-入度|<2,那么只能为0或1,对于0的情况,应该是出度等于入度,所以每个顶点都有偶数个度,既偶数条边,如果为1的话,一定是奇数条边,所以对于第二种情况,因为奇数边的点要么是起点,要么是终点(搜索),我们先对奇数边的点dfs,然后更改奇数边点的度数(删边),使之全部为偶数边,用line来存储相关联的两条边,index存储边的下标,value存储边的方向,total存放点的总度数。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#define MAX 500050
using namespace std; vector<int> line[MAX],index[MAX],value[MAX];
int total[MAX],flag[MAX],ans[MAX];
int t,n,m; void init ()
{
for ( int i = 0 ; i <= n ; i++ )
{
line[i].clear();
index[i].clear();
value[i].clear();
}
memset ( total , 0 , sizeof ( total ));
memset ( flag , 0 , sizeof ( flag ));
} void dfs ( int u )
{
int len = line[u].size()-1;
for ( int i = len ; i >= 0; i-- )
{
int v = line[u][i];
int x = index[u][i];
int c = value[u][i];
line[u].pop_back();
if ( flag[x] ) continue;
total[u]--;
total[v]--;
ans[x] = c;
flag[x] = 1;
dfs ( v );
break;
}
} void solve()
{
for ( int u=1 ; u<=n;u++ )
if (total[u]&1)
dfs(u);
for (int u=1;u<=n;u++)
dfs(u);
for (int i=0;i<m;i++ )
printf("%d\n",ans[i]);
} int main ( )
{
scanf ( "%d" , &t );
while ( t-- )
{
scanf ("%d%d" , &n , &m );
init();
for ( int i = 0 ; i < m ; i++ )
{
int u,v;
scanf ( "%d%d" , &u , &v );
line[u].push_back ( v );
index[u].push_back ( i );
value[u].push_back ( 1 );
line[v].push_back ( u );
index[v].push_back ( i );
value[v].push_back ( 0 );
total[u]++;
total[v]++;
}
solve();
}
}

HDU5348的更多相关文章

  1. [2015hdu多校联赛补题]hdu5348 MZL's endless loop

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...

  2. hdu5348 MZL's endless loop(欧拉回路)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's endless loop Time Limit: 3000/1500 ...

  3. 2015 多校联赛 ——HDU5348(搜索)

    Problem Description As we all kown, MZL hates the endless loop deeply, and he commands you to solve ...

  4. [hdu5348]图上找环,删环

    http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...

随机推荐

  1. 安装search everything中文语言包

    Everything 作为很多人的必备工具,特写这篇文章,一方面让想使用国外优秀软件的英语小白有一段过渡期,另一方面也为自己整理下资料.当然,这个可不是不学好英语的正当理由. 步骤: 1. 下载好se ...

  2. Pair of Numbers

    Codeforces Round #209 (Div. 2) D:http://codeforces.com/contest/359/problem/D 题意:给以一个n个数的序列,然后问你最大的区间 ...

  3. Area

    http://poj.org/problem?id=1265 #include<cstdio> #include<istream> #include<algorithm& ...

  4. 人在江湖飘,哪能不挨刀。CENTOS之后,UBUNTU,FEDORA都要安装起来作测试啊

    还好,我们有VIRTUAL BOX.

  5. Linux Shell编程(8)——变量详解

    不同与许多其他的编程语言,Bash不以"类型"来区分变量.本质上来说,Bash变量是字符串,但是根据环境的不同,Bash允许变量有整数计算和比较.其中的决定因素是变量的值是不是只含 ...

  6. vim学习与理解

    1. 转变思维 --> vim 无鼠标文本编辑工具 在鸟哥的linux私房菜中,是这么说明linux的: 1. vim是linux like系统中非常强大的一个文本编辑工具,历史悠久,很多系统都 ...

  7. 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings

    E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  8. 组合数学(Pólya计数原理):UvaOJ 10601 Cubes

    Cubes You are given 12 rods of equal length. Each of them is colored in certain color. Your task is ...

  9. 贪心(数据结构):COGS 468. [NOI2010]超级钢琴

    ★★★☆   输入文件:piano.in   输出文件:piano.out   简单对比 时间限制:2 s   内存限制:512 MB 超级钢琴 [问题描述] 小Z是一个小有名气的钢琴家,最近C博士送 ...

  10. 如何在不影响数据库的正常使用的情况下得到数据的完整.mdf和.ldf文

    一:完整备份数据库 二:还原数据库 四:分离数据库即可得到.mdf和.ldf文件