Graph Without Long Directed Paths CodeForces - 1144F(二分图染色)

You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self-loops or multiple edges in the given graph.

You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the number of traversed edges).

Input
The first line contains two integer numbers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤2⋅105n−1≤m≤2⋅105) — the number of vertices and edges, respectively.

The following mm lines contain edges: edge ii is given as a pair of vertices uiui, vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi). There are no multiple edges in the given graph, i. e. for each pair (ui,viui,vi) there are no other pairs (ui,viui,vi) and (vi,uivi,ui) in the list of edges. It is also guaranteed that the given graph is connected (there is a path between any pair of vertex in the given graph).

Output
If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print “NO” in the first line.

Otherwise print “YES” in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of ‘0’ and ‘1’) of length mm. The ii-th element of this string should be ‘0’ if the ii-th edge of the graph should be directed from uiui to vivi, and ‘1’ otherwise. Edges are numbered in the order they are given in the input.

Example
Input
6 5
1 5
2 1
1 4
3 1
6 1
Output
YES
10100
Note
The picture corresponding to the first example:
在这里插入图片描述
And one of possible answers:
在这里插入图片描述
要求把所有的无向边变成有向边,要求图中不能有长度大于等于2的路径。不能的话就输出-1。
本质上就是二分图染色。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
struct edge{
	int to,next;
}e[maxx<<1];
struct node{
	int x,y;
}p[maxx];
int head[maxx<<1],cor[maxx];
int n,m,tot;
/*----------事前准备---------*/
inline void init()
{
	memset(head,-1,sizeof(head));
	tot=0;
}
inline void add(int u,int v)
{
	e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
/*-----------dfs-----------*/
inline void dfs(int u,int c,int &flag)
{
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(cor[to]) 
		{
			if(cor[to]==c) 
			{
				flag=0;
				return ;
			}
			else continue;
		}
		else if(cor[to]==0)
		{
			cor[to]=-c;
			dfs(to,-c,flag);
		}
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	init();
	int x,y;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
		p[i].x=x,p[i].y=y;
	}
	cor[1]=1;
	int flag=1;
	dfs(1,1,flag);
	if(flag==0) puts("NO");
	else 
	{
		puts("YES");
		for(int i=1;i<=m;i++)
		{
			if(cor[p[i].x]==1&&cor[p[i].y]==-1) cout<<1;
			else cout<<0;
		}
		cout<<endl;
	}
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值