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?'\n':' ');
        }
        return 0;
    }
    cout<<2<<endl;
    for(int i=1;i<=m;i++)
    {
        if(dfn[X[i]]<dfn[Y[i]])
            printf("1%c",i==m?'\n':' ');
        else
            printf("2%c",i==m?'\n':' ');
    }
}
View Code

 

 

转载于:https://www.cnblogs.com/switch-waht/p/11480043.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值