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

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

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

Input

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

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

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

Output

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

In the second line print m integers 1,2,…,c1,c2,…,cm (1≤≤1≤ci≤k), where ci is a color of the i-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 k).

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即可,否则只需将从小指向大的边染为1,否则染为2即可.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=100010;
vector<int>G[maxn];
int flag;
int u[maxn],v[maxn],vis[maxn];
void DFS(int u)
{
if(flag)return ;
vis[u]=1;//正在访问
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(vis[v]==0)DFS(v);//没访问过
else if(vis[v]==1){//下一个节点正在访问,即有环
flag=1;
return ;
}
}
vis[u]=2;//访问结束
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>u[i]>>v[i];
G[u[i]].push_back(v[i]);
}
for(int i=1;i<=n;i++){
if(!vis[i]){
DFS(i);
}
}
if(!flag){
cout<<1<<endl;
for(int i=1;i<=m;i++)cout<<1<<" ";
cout<<endl;
}
else{
cout<<2<<endl;
for(int i=1;i<=m;i++){
if(u[i]<v[i])cout<<1<<" ";
else cout<<2<<" ";
}
cout<<endl;
}
return 0;
}

D. Coloring Edges的更多相关文章

  1. codeforces#1217D. Coloring Edges(图上染色)

    题目链接: https://codeforces.com/contest/1217/problem/D 题意: 给图染上$k$种颜色,相同颜色不能形成一个环 数据范围: $1\leq n \leq 5 ...

  2. Coloring Edges 【拓扑判环】

    题目链接:https://vjudge.net/contest/330119#problem/A 题目大意: 1.给出一张有向图,给该图涂色,要求同一个环里的边不可以全部都为同一种颜色.问最少需要多少 ...

  3. Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)

    题意:https://codeforc.es/contest/1217/problem/D 给你一个有向图,要求一个循环里不能有相同颜色的边,问你最小要几种颜色染色,怎么染色? 思路: 如果没有环,那 ...

  4. Educational Codeforces Round 72 (Rated for Div. 2)

    https://www.cnblogs.com/31415926535x/p/11601964.html 这场只做了前四道,,感觉学到的东西也很多,,最后两道数据结构的题没有补... A. Creat ...

  5. Educational Codeforces Round 72

    目录 Contest Info Solutions A. Creating a Character B. Zmei Gorynich C. The Number Of Good Substrings ...

  6. Educational Codeforces Round 72 (Rated for Div. 2) Solution

    传送门 A. Creating a Character 设读入的数据分别为 $a,b,c$ 对于一种合法的分配,设分了 $x$ 给 $a$ 那么有 $a+x>b+(c-x)$,整理得到 $x&g ...

  7. codeforces1217-edu

    C The Number Of Good Substrings 我原来的基本思路也是这样,但是写的不够好 注意算前缀和的时候,字符串起始最好从1开始. #include<cstdio> # ...

  8. POJ 1419 Graph Coloring(最大独立集/补图的最大团)

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4893   Accepted: 2271   ...

  9. POJ1419 Graph Coloring(最大独立集)(最大团)

                                                               Graph Coloring Time Limit: 1000MS   Memor ...

随机推荐

  1. JS - 局部方法改变全局变量的值

    var a = "" function aa() {  a="卡卡" } aa()    // 注意,aa() 一定要执行,局部赋值全局变量才会有作用 aler ...

  2. date linux系统校正时间

    date命令使用 -d<字符串>  显示字符串所指的日期与时间.字符串前后必须加上双引号.   date -d '13 second ago' ‘+%T’  13秒前   date +%T ...

  3. JSP页面获取其他页面传递的参数

    jstl表达式获取方式: ${param.pid} el表达式获取方式: ${requestScope.attr}  el表达式获取方式: ${attr} ---------------------- ...

  4. hibernate.hbm.xml必须必须配置主键

    hibernate.hbm.xml必须必须配置主键 <id name="XXid" type="java.lang.long" column=" ...

  5. docker创建redis容器

    1.拉取最新的redis镜像 docker pull redis; 2.创建存放redis数据的目录 mkdir /redis/data 3.查询redis镜像id docker images; RE ...

  6. Hour of Code|京东云邀您一起,“码”上行动

    "如果我并不希望成为一名程序员,那么为什么需要学习编程呢?" 相信很多人对于现在鼓励从小就学习编程的趋势都在心里问过这样的一个问题.在回答这个问题前,先和大家分享一个小故事吧. 1 ...

  7. 第二届中国“AI+”创新创业大赛完美收官,京东云赛道硕果累累

    聚焦南京产业发展核心诉求,京东云携手南京政府构建的"平台+生态+赋能"的产业体系,搭建产业创新云平台,以人工智能产业创新链要素补齐为核心,围绕"研.产.供.销.服&quo ...

  8. springboot-jar-web

    预览 与springboot-jar的区别是: 1.pom.xml 将 <dependency> <groupId>org.springframework.boot</g ...

  9. JZOJ-2019-11-7 A组

    T1 Input 从文件 awesome.in 中读入数据. 第一行 2 个用空格隔开的整数 \(n\), \(P\). 第二行 n 个用空格隔开的整数 \(A_1, \cdots , A_n\). ...

  10. tfield的字段名和显示名

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...