码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round 72 (Rated for Div. 2) D

时间:2019-09-07 13:32:00      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:single   lse   only   ica   vector   title   splay   stream   lin   

 
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

 

 

Codeforces Round 72 (Rated for Div. 2) D

标签:single   lse   only   ica   vector   title   splay   stream   lin   

原文地址:https://www.cnblogs.com/switch-waht/p/11480043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!