zoukankan      html  css  js  c++  java
  • Codeforces Round 72 (Rated for Div. 2) D

     
    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 (2n50002≤n≤5000, 1m50001≤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 (1u,vn1≤u,v≤n, uvu≠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 (1cik1≤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).

    题目大意:

    有一个有向图,可以用K种颜色为每条边涂色,要求强联通分量中的边不能全是同一颜色,求最小的颜色数K和每条边允许的颜色(符合情况即可)。

    思路:首先,K的值肯定不会超过2,因为两种颜色就可以把所有图画出。所以当图没有强联通分量时,K=1。有强联通分量时,K=2,然后按时间戳涂色即可。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<vector>
    using namespace std;
    const int MAXN=5050;
    vector <int> vec[MAXN];
    int vis1[MAXN],vis2[MAXN];
    int dfn[MAXN];
    int X[MAXN],Y[MAXN];
    int cnt,n,m;
    int x,y;
    int flag=0;
    void dfs(int rt)
    {
        vis1[rt]=1;//将此点标记,但此点与后面的点并未搜完
        dfn[rt]=++cnt;
        int l=vec[rt].size();
        for(int i=0;i<l;i++)
        {
            int v=vec[rt][i];
            if(vis2[v])//后面的点已搜过
                continue;
            if(vis1[v])//在搜索时形成强联通分量.
                flag=1;
            else
                dfs(v);
        }
        vis2[rt]=1;//此点与后面可以搜到的所有点全部搜完
    }
    int main()
    {
        cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            vec[x].push_back(y);
            X[i]=x;
            Y[i]=y;
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis1[i])
                dfs(i);
        }
        if(!flag)
        {
            cout<<1<<endl;
            for(int i=1;i<=m;i++)
            {
                printf("1%c",i==m?'
    ':' ');
            }
            return 0;
        }
        cout<<2<<endl;
        for(int i=1;i<=m;i++)
        {
            if(dfn[X[i]]<dfn[Y[i]])
                printf("1%c",i==m?'
    ':' ');
            else
                printf("2%c",i==m?'
    ':' ');
        }
    }
    View Code
  • 相关阅读:
    通过域名方式决定使用哪个数据库
    OpenERP/Odoo命令行参数
    修改pip源
    解决python "Non-ASCII character"错误
    Synergy 鼠标和键盘共享软件
    java 线程复习笔记
    常用设计模式--代理模式
    数据结构--二叉树
    mysql 索引的数据结构(B树和B+树)
    JS更改树型json的key键
  • 原文地址:https://www.cnblogs.com/switch-waht/p/11480043.html
Copyright © 2011-2022 走看看