题意:给一个无向图,让你指定边的方向,比如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. Zephyr-开发流程

    开发流程 前提1:检查你的Linux主机满足入门指南中规定的最低要求. 具体请参考 :  物联网操作系统-Zephyr 前提2: 确保SDK的环境变量和zephyr项目的环境变量. 终端执行: $ e ...

  2. Canny边缘检测-Wiki

    Canny edge dector 由 John F. Canny 在1986年提出. Canny 算法的发展 Canny算法的步骤 2.1 降噪 2.2 寻找图像的亮度梯度 2.3 非极大值抑制 2 ...

  3. 剖析并利用Visual Studio Code在Mac上编译、调试c#程序【转】

    0x00 前言 一周多以前的微软的Build大会上,微软发布了一个让很多人眼前一亮的工具,也是本文的主角——Visual Studio Code.很多使用Windows的朋友都很高兴,认为又多了一个很 ...

  4. MFC之MessageBox用法

    一    函数原型及参数 function MessageBox(hWnd: HWND; Text, Caption: PChar; Type: Word): Integer; hWnd:对话框父窗口 ...

  5. python Hbase Thrift pycharm 及引入包

    cp -r hbase/ /usr/lib/python2.7/site-packages/ 官方示例子http://code.google.com/p/hbase-thrift/source/bro ...

  6. tomcat源码阅读

    1      工具准备 需要SVN.Maven.JDK.Eclipse.Eclipse M2插件 2      下载源码及发布包 源码在这里:http://svn.apache.org/repos/a ...

  7. 15.禁用ViewState

    默认情况下ASP.net是启用ViewState的,这样在页面中会生成冗长的隐藏字段,ViewState对于需要PostBack处理的页面才可能有用,对于不需要交互的页面则完全没有必要用ViewSta ...

  8. 如何解决编译linux内核(解决声卡问题),遭遇fatal error: linux/limits.h: 没有那个文件或目录

    最近帮一位上海的朋友搞一块小板,在ubuntu15.04 vivid上已经加载了对应了.ko驱动包 但关键是系统根本就枚举不到该声卡ALC5640,试了OpenSUSE也是一样的结果,看来是内核漏加载 ...

  9. HDOJ 2056 Rectangles

    Problem Description Given two rectangles and the coordinates of two points on the diagonals of each ...

  10. UVa1453或La4728 凸包+枚举(或旋转卡壳)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...