zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 72 (Rated for Div. 2)D. Coloring Edges(想法)

    D. Coloring Edges

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given a directed graph with nn vertices and mm directed edges without self-loops or multiple edges.

    Let's denote the kk-coloring of a digraph as following: you color each edge in one of kk colors. The kk-coloring is good if and only if there no cycle formed by edges of same color.

    Find a good kk-coloring of given digraph with minimum possible kk.

    Input

    The first line contains two integers nn and mm (2≤n≤50002≤n≤5000, 1≤m≤50001≤m≤5000) — the number of vertices and edges in the digraph, respectively.

    Next mm lines contain description of edges — one per line. Each edge is a pair of integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) — there is directed edge from uu to vv in the graph.

    It is guaranteed that each ordered pair (u,v)(u,v) appears in the list of edges at most once.

    Output

    In the first line print single integer kk — the number of used colors in a good kk-coloring of given graph.

    In the second line print mm integers c1,c2,…,cmc1,c2,…,cm (1≤ci≤k1≤ci≤k), where cici is a color of the ii-th edge (in order as they are given in the input).

    If there are multiple answers print any of them (you still have to minimize kk).

    Examples

    input

    Copy

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

    output

    Copy

    1
    1 1 1 1 1 
    

    input

    Copy

    3 3
    1 2
    2 3
    3 1
    

    output

    Copy

    2
    1 1 2 

    看到群里的解法

    就是先用类似拓扑排序的思想

    先判断一下是否存在环

    如果存在环就是大☞小为1 反之为2

    因为是一个环所有肯定会有两种情况

    #include<bits/stdc++.h>
    using namespace std;
    vector<int>ve[5005];
    int n,m;
    int IN[5005];
    int tuopu()
    {
        queue<int>q;
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            if(IN[i]==0)
            {
                q.push(i);
                ans++;
            }
        }
        while(q.size())
        {
            int u=q.front();
            q.pop();
            for(auto v:ve[u])
            {
                IN[v]--;
                if(IN[v]==0)
                {
                    q.push(v);
                    ans++;
                }
            }
        }
        return ans==n;
    }
    int tmp1[5005];
    int tmp2[5005];
    int main()
    {
        scanf("%d%d",&n,&m);
        memset(IN,0,sizeof(IN));
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&tmp1[i],&tmp2[i]);
            ve[tmp1[i]].push_back(tmp2[i]);
            IN[tmp2[i]]++;
        }
        if(tuopu())
        {
            printf("1
    ");
            for(int i=1; i<=m; i++)
            {
                printf("1%s",i==m?"
    ":" ");
            }
        }
        else
        {
            printf("2
    ");
            for(int i=1; i<=m; i++)
            {
                if(tmp1[i]>tmp2[i])
                {
                    printf("1%s",i==m?"
    ":" ");
                }
                else
                {
                    printf("2%s",i==m?"
    ":" ");
                }
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    每日英语:Why Sit Up Straight?
    每日英语:Doctor's Orders: 20 Minutes Of Meditation Twice A Day
    每日英语:SamsungApple Patent Fight: Is It Worth It?
    每日英语:Better Than Buffett, This Investor Made Me Rich for Life
    每日英语:Coming Soon to China: AtHome Toxic Food Test Kits
    去了家新公司,技术总监不让用 IntelliJ IDEA!!想离职了。。
    IDEA 进行远程 Debug,这个太强了。。
    雷军做程序员时写的博客,太牛了。。
    Spring Boot 如何获取 Controller 方法名和注解信息?
    sql 2005/2008中直接创建web service,允许客户机通过HTTP访问
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852208.html
Copyright © 2011-2022 走看看