codeforces (F. Graph Without Long Directed Paths 图染色,dfs)

12 篇文章 0 订阅

F. Graph Without Long Directed Paths

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

6 5
1 5
2 1
1 4
3 1
6 1

output

Copy

YES
10100

Note

The picture corresponding to the first example:

And one of possible answers:

题目:给定n个点,m条边,保证任意两点连通,现在让你给这个图规定边的方向,使得图中任意点之间的距离小于2,问是否可以,并输出。

题目解答:咱们可以转化这个问题,对于某个点来说,它要么入度为0,要么出度为q(q为未规定方向前所有与它直接相连的边的数量),我们可以抽象成给每一个点染色(0|1),如果有两个点颜色一样时,就是不行的(可以自己理解一下)。

代码:

#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2000005;
int n, m;
vector<int>v[maxn];
int x[maxn], y[maxn];
int color[maxn];
int flag;
void dfs(int e,int pre,int col)
{
	color[e] = col;
	for (int i = 0; i < v[e].size(); i++)
	{
		int u = v[e][i];
		if (u == pre)
			continue;
		if (color[u] == -1)
			dfs(u, e, 1 - col);
		else if (color[u] == color[e])
			flag = 1;
	}
}
int main()
{
	cin >> n >> m;
	memset(color, -1, sizeof(color));
	for (int i = 0; i < m; i++)
	{
		int a, b;
		cin >> a >> b;
		x[i] = a; y[i] = b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	flag = 0;
	dfs(1, -1, 0);
	if (flag)
	{
		cout << "NO\n";
		return 0;
	}
	cout << "YES\n";
	for (int i = 0; i < m; i++)
	{
		if (color[x[i]] == 1)
			cout << 1;
		else
			cout << 0;
	}
	cout << endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值