zoukankan      html  css  js  c++  java
  • F. Graph Without Long Directed Paths(F. Codeforces Round #550 (Div. 3))(dfs)

    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:

    此题并不难

    将点分成两类

    入点 和出点 入点所有的边都是入度 出点相反

    由题意可知 两个相邻点必为两种类型不同的点

    因此 此题只需要跑一遍dfs看是否有冲突的现象即可

    ac代码

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <cstdlib>
    #include <queue>
    #include <stack>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include<algorithm>
    #include <set>
    using namespace std;
    int n,m;
    int a[200005];
    int first[400005];
    int to[400005];
    int nxt[400005];
    int stu[200005];
    int flag=0;
    void dfs(int x,int lst)
    {
        if(stu[x]==-1) stu[x]=lst^1;
        else if(stu[x]==lst)
        {
            flag=-1;
            printf("NO
    ");
            exit(0);
        }
        else
        {
            return ;
        }
        for(int i=first[x]; i!=-1; i=nxt[i])
        {
            dfs(to[i],stu[x]);
        }
        return ;
    }
    int main()
    {
        memset(stu,-1,sizeof(stu));
        memset(first,-1,sizeof(first));
        memset(to,-1,sizeof(to));
        memset(nxt,-1,sizeof(nxt));
        scanf("%d%d",&n,&m);
        int cnt=0;
        for(int i=1; i<=m; i++)
        {
            int temp1;
            int temp2;
            scanf("%d%d",&temp1,&temp2);
            a[i]=temp1;
            cnt++;
            nxt[cnt]=first[temp1];
            to[cnt]=temp2;
            first[temp1]=cnt;
            cnt++;
            nxt[cnt]=first[temp2];
            to[cnt]=temp1;
            first[temp2]=cnt;
        }
        dfs(1,0);
        printf("YES
    ");
        for(int i=1;i<=m;i++)
        {
            printf("%d",stu[a[i]]);
        }
        return 0;
    }
  • 相关阅读:
    记录一次redis-shake跨云迁移阿里云redis混合型到腾讯云tendis
    nginx 通过配置文件切割访问日志
    saltstack 批量修改hostname
    linux 免交互式修改密码
    zabbix 接入钉钉机器人报警
    监控体系
    zabbix自动化监控三种方式
    zabbix 自定义脚本监控activemq
    2021.07.20 总结
    微信小程序总结
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852301.html
Copyright © 2011-2022 走看看