题意:给一个无向图,让你指定边的方向,比如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. 长期支持版本(即不自动更新版本) - Flash Player 18.0.0.268

    无意中发现,适合不喜欢折腾的朋友. 下载链接:(官方:http://www.adobe.com/cn/products/flashplayer/distribution3.html) (分流:http ...

  2. gcc常用的编译选项

    一.程序编译过程 程序编译的时候,要分四个阶段 : 1.预处理阶段,完成宏定义和include文件展开等工作: 2.根据编译参数进行不同程度的优化,编译成汇编代码: 3.用汇编器把汇编代码进一步生成目 ...

  3. Solr4.8.0源码分析(26)之Recovery失败造成的宕机原因分析

    最近在公司做SolrCloud的容灾测试,刚好碰到了一个比较蛋疼的问题,跟SolrCloud的Recovery和leader选举有关,正好拿出来分析下. 现象是这样的:比如我有一台3个shard的So ...

  4. PHP框架、库和软件资源大全(整理篇)

    php的资料 https://github.com/ziadoz/awesome-php Awesome PHP A curated list of amazingly awesome PHP lib ...

  5. Node.js教程

    Node.js是什么? Node.js是建立在谷歌Chrome的JavaScript引擎(V8引擎)的服务器端平台.Node.js是由瑞恩·达尔在2009年开发的,它的最新版本是v0.10.36. N ...

  6. Can deep learning help you find the perfect girl?

    Can deep learning help you find the perfect girl? One of the first things I did when I moved to Mont ...

  7. oracle 中使用触发器自动生成UUID

    create or replace trigger tri_test before insert on test for each row declare begin if :new.uuid is ...

  8. Snowflake Snow Snowflakes(哈希,大素数取模)

    Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 27277   Accepted: 7197 Description You ...

  9. 矩阵(快速幂):COGS 963. [NOI2012] 随机数生成器

    963. [NOI2012] 随机数生成器 ★★   输入文件:randoma.in   输出文件:randoma.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] 栋 ...

  10. selenuim ide回放时出现的问题

    [error] Unexpected Exception: fileName -> chrome://selenium-ide/content/selenium-core/scripts/htm ...