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

    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 (2n21052≤n≤2⋅105, n1m2105n−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 (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).

    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:

    题意就是给一个无环无重边的无向图,把相邻两个点染异色,如果可以输出染色情况,否则输出NO

    扫一遍图就行了

    FUCK 我试着用bfs写 怎么都写不对 mdzz

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=2e5+5;
    const int INF=1e9+5;
    typedef pair<int,int> pii;
    int n,m,vis[maxn],cnt,ans[maxn],a[maxn],f,pre[maxn];
    int mp[maxn];
    
    vector<pii>e[maxn];
    
    void init(int x)
    {
        for(int i=0; i<=x; i++) e[i].clear();
        memset(vis,0,sizeof(vis));
        memset(ans,-1,sizeof(ans));
        memset(pre,-1,sizeof(pre));
    }
    
    void dfs(int now,int pre,int t)
    {
        ans[now]=t;
        for(int i=0; i<e[now].size(); i++)
        {
            int v=e[now][i].first;
            if(v==pre) continue;
            if(ans[v]==-1) dfs(v,now,!t);
            else if(ans[v]==ans[now]) f=1;
        }
        return;
    }
    
    int main()
    {
        cin>>n>>m;
        init(n);
        for(int i=1; i<=m; i++)
        {
            int x,y;
            cin>>x>>y;
            a[i]=x;
            e[x].push_back(make_pair(y,-1));
            e[y].push_back(make_pair(x,-1));
        }
        dfs(1,-1,0);
        if(f!=1)
        {
            cout<<"YES"<<endl;
            for(int i=1; i<=m; i++)
                cout<<ans[a[i]];
            cout<<endl;
        }
        else
            cout<<"NO"<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    WebService安全 文件夹 目录安全性 身份验证与访问控制
    爱情故事[转载自:学狼网]
    未来中国最受宠的人才
    [引]SQL Server : 系统存储过程
    VS 安装项目 :通过文本框得到用户输入 以及 安装后运行某程序(如打开C:\\a.html)
    人人都能成为百万富翁
    成功原来这样简单
    精典谚语
    利用WinForm 更好的实现Web安装程序的更多功能
    升级安装包的制作
  • 原文地址:https://www.cnblogs.com/youchandaisuki/p/10651437.html
Copyright © 2011-2022 走看看