F. Graph Without Long Directed Paths
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 uiuito 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的边出现
题解:
直接二分图染色跑一遍即可
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
vector <int> G[maxn];
int n,m;
int col[maxn];
struct node
{
int u,v,x;
}a[maxn];
void dfs(int x,int color)
{
col[x]=color;
for(auto v:G[x])
{
if(!col[v])
dfs(v,-color);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
a[i].u=u,a[i].v=v;
}
dfs(1,1);
int flag=0;
for(int i=1;i<=m;i++)
{
if(col[a[i].u]!=col[a[i].v])
{
if(col[a[i].u]==1)
a[i].x=0;
else a[i].x=1;
}
else flag=1;
}
if(!flag)
{
printf("YES\n");
for(int i=1;i<=m;i++)
printf("%d",a[i].x);
}
else printf("NO\n");
return 0;
}
探讨如何在给定的无向连通图中为每条边赋予方向,确保图中不存在长度大于等于2的路径。通过二分图染色算法实现边的方向设定。
283

被折叠的 条评论
为什么被折叠?



