hdu 5348 MZL's endless loop 暴搜

9 篇文章 0 订阅

链接: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 with  n  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.
 

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 integers  n  and  m .
And the next  m  lines, each line contains two integers  ui  and  vi , which describe an edge of the graph.
T100 1n105 1m3105 n2105 m7105 .
 

Output
For each test case, if there is no solution, print a single line with  1 , otherwise output  m  lines,.
In  i th line contains a integer  1  or  0 1  for direct the  i th edge to  uivi 0  for  uivi .
 

Sample Input
  
  
2 3 3 1 2 2 3 3 1 7 6 1 2 1 3 1 4 1 5 1 6 1 7
 

Sample Output
  
  
1 1 1 0 1 0 1 0 1
 


题意:给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和0

void 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);
			}
			else
				continue; 
			break;
		} 
		else
			head[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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值