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. scp、wget

    scp使用方法 -1  强制scp命令使用协议ssh1 -2  强制scp命令使用协议ssh2 -4  强制scp命令只使用IPv4寻址 -6  强制scp命令只使用IPv6寻址 -B  使用批处理模 ...

  2. java课程课后作业190616之个人学期总结

    在团队开始的那一周,我们做了作品的功能畅想,在讲台上谈论了自己的产品可能会有的功能,比如说课程查找功能,空教室查找功能,霸屏功能,课程留言功能等,当然,随着开发的推进,我也发现了有些功能上实现的困难, ...

  3. JDK8中的新特性

    1.lambda表达式 1.定义 Java 8 发布的最重要新特性.Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中),可以推导出来的就可以省略了,Lambda 表达式免去了使用匿 ...

  4. java笔记01

    java对象数组 Student[] Students = new Student[3]; 与普通数组无差 java集合类 集合类: 面向对象对事物的描述是通过对象来体现的. 为了方便对多个对象进行操 ...

  5. 18 12 24 html 表单学习

    html表单 表单用于搜集不同类型的用户输入,表单由不同类型的标签组成,相关标签及属性用法如下: 1.<form>标签 定义整体的表单区域 action属性 定义表单数据提交地址 meth ...

  6. clion 的 安装 变量配置的 搬运工(有点基础应该能看 大家看不懂 就是我自己看 哈哈哈哈哈哈)

    1  自行安装一个  clion 2 https://blog.csdn.net/u013023297/article/details/80723847   mingw  的配置    第二篇好像当时 ...

  7. Mysql时间范围分区(RANGE COLUMNS方式)

    1.创建测试表 CREATE TABLE `t_test` ( `id` ), `dates` DATETIME ); ALTER TABLE t_test ADD PRIMARY KEY (id); ...

  8. if case for while

    #!/bin/basha=$1if [ $a ] #判断$1是否为空then #非空echo "the input is No:$a"exit 0else #空read -p &q ...

  9. Asp.NET CORE安装部署

    先安装IIS再安装这两个,不然后面各种bug HTTP 错误 500.19 代码 0x8007000d 解决方案 for win7_64 asp.net core IIS Web Core 1.比如最 ...

  10. 干货 | 把Flutter扩展到微信小程序端的探索

    Google Flutter是一个非常优秀的跨端框架,不仅可以运行在Android. iOS平台,而且可以支持Web和桌面应用.在国内小程序是非常重要的技术平台,我们也一直思考能否把Flutter扩展 ...