首页 > 其他分享 >Educational Codeforces Round 72 (Rated for Div. 2)D. Coloring Edges 神仙规律

Educational Codeforces Round 72 (Rated for Div. 2)D. Coloring Edges 神仙规律

时间:2023-02-07 12:31:48浏览次数:45  
标签:coloring Educational Rated kk Codeforces int edges MAXN input

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

 

题意:

一张有向图,每条边染色,要使得每个环都不能全部为同一种颜色,输出最少需要的颜色和每条边的颜色?

分析:

对于有向图来说,一个环一定包含小号到大号的边和大号到小号的边,所以最多就两种颜色,一看代码就什么都明白了。

 

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN=50100;
LL n,m,vis[MAXN],in[MAXN],a[MAXN],b[MAXN];
vector<LL> G[MAXN];
int top_sort()
{
    int cnt=0;
    queue<int> q;
    for(int i=1; i<=n; i++)
        if(!in[i])
            q.push(i);
    while(!q.empty())
    {
        int u=q.front();
        cnt++;
        q.pop();
        for(int i=0; i<G[u].size(); i++)
            if(--in[G[u][i]]==0)
                q.push(G[u][i]);
    }
    return (n==cnt);
}
signed main()
{
    cin>>n>>m;
    for(int i=1; i<=m; i++)
    {
        cin>>a[i]>>b[i];
        G[a[i]].push_back(b[i]);
        in[b[i]]++;
    }
    if(top_sort())
    {
        printf("1\n");
        for(int i=1; i<=m; i++)
            printf("1 ");
       cout<<endl;
        return 0;
    }
    printf("2\n");
    for(int i=1; i<=m; i++)
    {
        if(a[i]>b[i])
            printf("1 ");
        else
            printf("2 ");
    }
    cout<<endl;
    return 0;
}

 

标签:coloring,Educational,Rated,kk,Codeforces,int,edges,MAXN,input
From: https://blog.51cto.com/u_14932227/6041986

相关文章

  • CodeForces - 141C Queue
    C.Queuetimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputIntheMainBerlandBank n peoplestandinaque......
  • CodeForces 1423G Growing flowers
    洛谷传送门CF传送门先离散化颜色。考虑对每种颜色单独求出答案。对于颜色\(x\),可以用总方案数\(n-k+1\)减去一个\(x\)都不包含的区间数量。对于这个,假设相邻两个颜......
  • Codeforces Round #846 (Div. 2)(B,E)
    CodeforcesRound#846(Div.2)(B,E)BB题目大意是给一个长度为n的数组,我们可以把这个数组分成k段(k>1),我们有一个值是每一段的和的gcd\(gcd(sum[1],sum[2]+...+sum[k])\)......
  • Codeforces Round #850 (Div. 2, based on VK Cup 2022 - Final Round) D-F
    D题解题思路本质上就是个贪心,因为比较简单所以不多写,在代码实现上注意利用stlvec.back()//返回尾部的值vec.pop_back()//弹出尾部(next_permutation(vec.begin(),vec......
  • Codeforces Round #840 (Div. 2) and Enigma 2022 - Cybros LNMIIT C. Another Array
    题目链接:https://codeforces.com/problemset/problem/1763/C   大致题意: 给你长度为n的数组,你可以进行任意次操作,操作内容如下:选择俩个下标i,j;对于i......
  • Codeforces Round #849 (Div. 4)
    A.CodeforcesChecking题意每个案例给一个字符,如果在”codeforces“中出现过,输出YES,否则输出NOcode/***@author:Changersh*@date:2023/2/322:37*/i......
  • Codeforces Round #236 (Div. 2) E - Strictly Positive Matrix
    根据线性代数的知识可知邻接矩阵自乘相当于做floyed把输入转化为01矩阵(显然>1的数和1是等价的)得到邻接矩阵问是否存在k次后所有数都为正数等价为自乘k次后所有点两两可......
  • Codeforces Round #244 (Div. 2) C. Checkposts
    裸的tarjan依题意有向图上i和j之间能互相到达,i和j肯定在同一个scc内最小的代价就是Σ每个scc内最小的cost方案就是每个scc内最小值的数的乘积#include<bits/stdc++.h>......
  • Codeforces Round #848 (Div. 2)D. Flexible String Revisit-dp、初等数学
    题目:https://codeforces.com/problemset/problem/1778/D场内打的,首先很容易想到答案来自于a、b不同的位置有几个,设\(f_k\)表示当前有k个不同的位置要复原到完全一样需要多......
  • Codeforces Round #840 (Div. 2) E. Node Pairs-图论、小dp
    题目链接:https://codeforces.com/problemset/problem/1763/E题意有点绕,大概就是给一个p,现在希望找到一个n个点的有向图G,恰好有p个点对\(1\lequ<v\leqn\)使得\(u\tov\)......