HDU5348
题意:给一个无向图,让你指定边的方向,比如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的更多相关文章
- [2015hdu多校联赛补题]hdu5348 MZL's endless loop
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...
- hdu5348 MZL's endless loop(欧拉回路)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud MZL's endless loop Time Limit: 3000/1500 ...
- 2015 多校联赛 ——HDU5348(搜索)
Problem Description As we all kown, MZL hates the endless loop deeply, and he commands you to solve ...
- [hdu5348]图上找环,删环
http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...
随机推荐
- 【算法】简单选择排序 O(n^2) 不稳定的 C语言
简单选择排序 一.算法描述 假设序列中有N个元素: 第1趟找到第1到N个元素之间最小的一个,与第1个元素进行交换 第2趟找到第2到N个元素之间最小的一个,与第2个元素进行交换 第3趟找到第3到N个元素 ...
- [BZOJ 3626] [LNOI2014] LCA 【树链剖分 + 离线 + 差分询问】
题目链接: BZOJ - 3626 题目分析 考虑这样的等价问题,如果我们把一个点 x 到 Root 的路径上每个点的权值赋为 1 ,其余点的权值为 0,那么从 LCA(x, y) 的 Depth 就 ...
- 【Linux】鸟哥的Linux私房菜基础学习篇整理(十)
1. at [-mldv] TIME/at -c 工作号码:单一工作调度.参数:-m:当at的工作完成后,即使没有输出信息,以email通知用户该工作已完成:-l:相当于atq,列出目前系统上面的所有 ...
- 【高精度】NCPC 2014 C catalansqure
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1789 题目大意: 求大卡特兰数..公式如下.输入n求Sn(n<=5000) 题目 ...
- 【模拟】Codeforces 707A Brain's Photos
题目链接: http://codeforces.com/problemset/problem/707/A 题目大意: 给一张N*M的图,只有六种颜色(如下),只含B,W,G的是黑白图,否则是彩色图.问 ...
- Unique Paths ——LeetCode
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- bzoj 2245 [SDOI2011]工作安排(最小费用最大流)
2245: [SDOI2011]工作安排 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1197 Solved: 580[Submit][Statu ...
- bithrtree
#include "stdio.h" #include "stdlib.h" #define OK 1 #define ERROR 0 typedef char ...
- nyist 58 最小步数 BFS
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0 ...
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of ...