Codeforces-1144F

題面:ios

題目連接ide

 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.oop

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).ui

Input

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

The following mm lines contain edges: edge ii is given as a pair of vertices uiui, vivi (1ui,vin1≤ui,vi≤n, uiviui≠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).spa

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.code

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.xml

Example
input
6 5
1 5
2 1
1 4
3 1
6 1
output
YES
10100

題目大意:給定一個無向圖,要求經過添加方向使其成爲有向圖,條件是不能存在長度大於1的路徑。其中每一個點全部的邊只能有指向它或者背向它。blog

解題思路:二分匹配ip

#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cassert> #include<complex>//real().imag().
#include<cctype> #include<algorithm> #include<iomanip> #include<stack> #include<queue> #include<list> #include<map> #include<set> #include<deque> #include<string> #include<utility> #include<iterator>
#define pii pair<int,int>
#define make_pair mp
using namespace std; typedef long long ll; const int N = 200005; int n, m,u,v,flag=1; vector<int> vc[N]; int color[N]; int solve[N]; void dfs(int u, int cl) { color[u] = cl; for (int i = 0, j = vc[u].size(); i < j; i++) { int t = vc[u][i]; if (color[u] == color[t]) { flag = 0; } if (color[t] == -1) { dfs(t, !cl); } } } int main() { // freopen("D:\\in.txt","r",stdin); // freopen("D:\\out.txt","w",stdout); //ios::sync_with_stdio(false);
    cin >> n >> m; for (int i = 0; i < m; i++) { cin >> u >> v; vc[u].push_back(v); vc[v].push_back(u); solve[i] = u; } memset(color, -1, sizeof(color)); for (int i = 1; i <= n; ++i) { if (color[i] == -1) { dfs(i, 0); } } if (flag) { cout << "YES" << endl; for (int i = 0; i < m; i++) { cout << !color[solve[i]]; } } else cout << "NO" << endl; //system("pause");
    return 0; }
View Code
本站公眾號
   歡迎關注本站公眾號,獲取更多信息