hdu 5348 MZL's endless loop 暴搜
来源:互联网 发布:无锡作协网络分会成立 编辑:程序博客网 时间:2024/05/18 01:11
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348
MZL's endless loop
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1426 Accepted Submission(s): 319
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
题意:给n点m条无向边,要给每条边定一个方向,最后图中所有点的出入度差小于等于1.
做法:暴搜,注意用完的边要删去,更新下head就可以删边了。 然后 每个dfs 只找一条路径, 找到后就break。
#pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h>#include <string>#include <iostream>using namespace std;const int N=100100;const int M=300100;struct Edge{int to, nex , id , fan;}edge[M*2];int head[N],edgenum;//2个要初始化-1和0void add(int u, int v, int id ,int fan){Edge E = { v, head[u], id, fan};edge[ edgenum ] = E;head[ u ] = edgenum++;}void init(){edgenum=0;}//for(int i=head[nw];i!=-1;i=edge[i].nex)int du[N];//出减 入加int bian[M];void dfs(int nw,int fx){for(int i=head[nw];i!=-1;i=head[nw]){int to=edge[i].to;if(bian[edge[i].id]==-1)//还没确定方向{if(du[nw]!=-1&&du[to]!=1&&fx!=2){ du[nw]--;du[to]++;bian[edge[i].id]=(1^edge[i].fan); head[nw]=edge[i].nex;dfs(to,1);}else if(du[nw]!=1&&du[to]!=-1&&fx!=1){ du[nw]++;du[to]--; bian[edge[i].id]=(0^edge[i].fan); head[nw]=edge[i].nex;dfs(to,2);}elsecontinue; break;} elsehead[nw]=edge[i].nex;//删边 }}int main(){int t;scanf("%d",&t);while(t--){int n,m;init();scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) head[i]=-1;for(int i=0;i<m;i++){bian[i]=-1;int u,v;scanf("%d%d",&u,&v);if(u==v){bian[i]=0;continue;}add(u,v,i,0);add(v,u,i,1);}for(int i=1;i<=n;i++) du[i]=0;for(int i=1;i<=n;i++) {while(head[i]!=-1)dfs(i,0);}for(int i=0;i<m;i++) printf("%d\n",bian[i]); }return 0;}
0 0
- 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 dfs
- Hdu 5348 MZL's endless loop (构造)
- 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(DFS+邻接表)
- HDU 5348 MZL's endless loop (dfs+删边)
- 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) 欧拉路
- hdu 5348 MZL's endless loop 2015 Multi-University Training Contest 5
- Java数组反转
- spring 中 applicationEvent的使用 广播
- Android禁止发送表情图片
- hdoj-3579-Hello Kiki【中国剩余定理 & 除数不互质】
- HDU1.3.2 今年暑假不AC
- hdu 5348 MZL's endless loop 暴搜
- 怎么去掉Xcode工程中的某种类型的警告
- jquery选中checkbox的问题
- 指针数组的应用场景之main函数
- 基于caffe特征可视化 以及 用训练好的模型进行分类
- mybatis自动生成mapper.xml的方法
- 素数伴侣
- 高质量CSS
- 【转】Qt5.2.1移植新唐n32905测试 转的一位朋友的过程,基本能够通过。