HDU 5348 MZL's endless loop(DFS+邻接表)
来源:互联网 发布:求是不是质数的算法 编辑:程序博客网 时间:2024/05/29 10:38
MZL's endless loop
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Special Judge
Problem Description
As we all kown, MZL hates the endless loop deeply, and he commands you to solve this problem to end the loop.
You are given an undirected graph withn 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.
You are given an undirected graph with
The graph you are given maybe contains self loops or multiple edges.
Input
The first line of the input is a single integer T , indicating the number of testcases.
For each test case, the first line contains two integersn and m .
And the nextm 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 .
For each test case, the first line contains two integers
And the next
Output
For each test case, if there is no solution, print a single line with −1 , otherwise output m lines,.
Ini th line contains a integer 1 or 0 , 1 for direct the i th edge to ui→vi , 0 for ui←vi .
In
Sample Input
23 31 22 33 17 61 21 31 41 51 61 7
Sample Output
111010101
Source
2015 Multi-University Training Contest 5
题意:一个具有n个结点,m条无向边的无向图,要求我们规定每条边的方向,使其成为有向图之后每个结点的入度与出度差的绝对值小于1,若无解,则输出-1
解题思路:该题的解法有很多种,有用欧拉路的,也有用搜索的,而我要讲的方法就是深搜
首先我们先看看多校题解报告中的思路
出题人的想法是一张图中除了环就是树,根据左孝凌版离散数学中树的定义“无回路的连通图”可以得出这样一个结论,要么有回路(环),要么没有回路(树),无外乎这么两种情况,所以根据环与树各自的属性特点,此题从每个点开始DFS这张图就好了
下面给出我做此题时的思路
首先暂且先不考虑环还是树,直接进行深搜,对每个点进行出度和入度的判断,如果出度大,就先进行反向的搜索(每搜索一条边u,v就认为这是一条从v到u的有向边),如果入度大,进行正向搜索(每搜到一条边u,v认为这是一条从u到v的有向边),一直搜索到找不到边能继续为止。
注意:
①已经使用过的边为了防止再次被遍历到,可以修改head[u]的值,即head[u] = edge[i].next
②注意自环,因为能否继续向下层搜索是根据v的入度和出度大小来决定的,但是如果是自环的话可能会影响继续搜索。
另外,此题不存在无解的可能
反证法证明:假设存在一个结点v,满足该结点的入度比出度多2,那么,当有一条边搜索到v后找不到后继,导致v的入度比出度多1,这时又有一条边搜索到v,导致v的入度比出度多2,但是这样的话就会与第一条找不到后继的情况冲突,所以不会出现入度比出度多2的情况,其余情况亦是如此,所以不会有无解的结果。
闲话不多说,上AC代码#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<math.h>#include<vector>#include<map>#include<set>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 100005;const int inf = 1000000000;struct node{ int to,next;}edge[6*N];int k,head[N],num[N],in[N],out[N],n,ans[N*3];bool vis[N*6];void add_edge(int from,int to){ edge[k].to=to; edge[k].next=head[from]; head[from]=k++;}void DFS1(int u){ int i,v; for(i=head[u];i+1;i=edge[i].next) { if(vis[i]) { head[u]=edge[i].next; continue; } v=edge[i].to; if(u!=v&&in[v]>out[v]) continue; out[u]++; in[v]++; vis[i]=vis[i^1]=true; ans[i/2]=i%2?0:1; head[u]=edge[i].next; DFS1(v); break; }}void DFS2(int u){ int i,v; for(i=head[u];i+1;i=edge[i].next) { if(vis[i]) { head[u]=edge[i].next; continue; } v=edge[i].to; if(u!=v&&in[v]<out[v]) continue; out[v]++; in[u]++; vis[i]=vis[i^1]=true; ans[i/2]=i%2?1:0; head[u]=edge[i].next; DFS2(v); break; }}int main(){ int t,m,i,u,v; scanf("%d",&t); while(t--) { k=0; memset(head,-1,sizeof(head)); memset(num,0,sizeof(num)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(vis,false,sizeof(vis)); scanf("%d%d",&n,&m); for(i=0;i<m;i++) { scanf("%d%d",&u,&v); add_edge(u,v); add_edge(v,u); num[u]++; num[v]++; } for(i=1;i<=n;i++) while(in[i]+out[i]<num[i]) if(in[i]>=out[i]) DFS1(i); else DFS2(i); for(i=0;i<m;i++) printf("%d\n",ans[i]); } return 0;}
菜鸟成长记
0 0
- HDU 5348 MZL's endless loop(DFS+邻接表)
- hdu 5348 MZL's endless loop dfs
- hdu 5348 MZL's endless loop(dfs+图论)
- hdu 5348 MZL's endless loop(dfs)
- HDU 5348 MZL's endless loop (dfs+删边)
- hdu 5348 MZL's endless loop
- HDU 5348 MZL's endless loop
- *HDU 5348 - MZL's endless loop(搜索)
- hdu 5348 MZL's endless loop
- hdu 5348 MZL's endless loop 暴搜
- Hdu 5348 MZL's endless loop (构造)
- hdu 5348 MZL's endless loop 2015多校联合训练赛#5 找环+dfs
- hdu 5348 MZL's endless loop 欧拉回路
- HDOJ 5348 MZL's endless loop 乱搞
- HDOJ 5348 MZL's endless loop
- hdu 5348 MZL's endless loop(欧拉路径+欧拉环)
- HDU 5348 MZL's endless loop(思想用的是深搜)经典
- hdu 5348 MZL's endless loop(15多校第五场1006) 欧拉路
- 经理给我们带了龙眼
- 虚拟化项目之运维实践 3
- SendMessage和PostMessage的用法
- shell 中数学计算总结
- 2014微软秋季校招算法笔试题
- HDU 5348 MZL's endless loop(DFS+邻接表)
- 美国公开课网址
- Iterator
- AD的精度与分辨率
- [leetcode-77]Combinations(java)
- myeclipse中的SpringMVC项目创建(引入velocity框架)
- iOS之UIView的层次结构变更
- 【leetcode】 database Delete Duplicate Emails
- Oracle删除重复的记录