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. Ajax校验用户名是否可用

    准备 导包:DBUtil,JDBC,C3P0 在src下导入c3p0-config.xml 导入JDBCUtil 创建数据库 第2.3.条查看https://blog.csdn.net/weixin_ ...

  2. Spring注解 @Autowired

    @Autowired可以对成员变量.方法和构造函数进行标注,来完成自动装配的工作,这里必须明确:@Autowired是根据类型进行自动装配的,如果需要按名称进行装配,则需要配合@Qualifier使用

  3. 吴裕雄--天生自然TensorFlow2教程:数据统计

    import tensorflow as tf a = tf.ones([2, 2]) a tf.norm(a) tf.sqrt(tf.reduce_sum(tf.square(a))) a = tf ...

  4. Tomcat解压版Windows配置(运行环境非开发环境)

    tomcat官网下载的9.0.19,解压后目录如下: java官网下载的jre8 (8u131),目录如下(应该是下载的解压版): 打开tomcat9.0.19根目录下的RUNNING.txt,里面有 ...

  5. NoSQL:

    NoSQL:NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 在现代的计算系统上每天网络上都会产生庞大的数据量. 这些数据有很大一部分是由关系数据 ...

  6. [GXYCTF2019]BabySQli

    0x00 知识点 emmm这道题目就是脑洞得大,能猜后端源码 0x01 解题 查看源码: base32,base64解码得到 select * from user where username = ' ...

  7. XML--XML Schema Definition(四)

    参考 http://www.w3school.com.cn/schema/index.asp XSD 复合类型指示器 通过指示器,我们可以控制在文档中使用元素的方式.有七种指示器: Order 指示器 ...

  8. mariabd mysql升级mariadb

    还有错误 [root@localhost /]# mysqldump --all-databases --user=root --password --master-data > backupd ...

  9. centos系统将shell脚本改成systemctl启动的形式

    说明: CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,就将程序存在系统服务里,即 ...

  10. Vue专题-路由系统

    一切分离都是为了更好的结合,本文详细介绍了前后端分离架构之后,前端如果实现路由控制,即Vue路由系统. Vue路由系统 VueRouter实现原理 VueRouter的实现原理是根据监控锚点值的改变, ...