D. Coloring Edges
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.
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.
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).
4 5
1 2
1 3
3 4
2 4
1 4
1
1 1 1 1 1
3 3
1 2
2 3
3 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的更多相关文章
- codeforces#1217D. Coloring Edges(图上染色)
题目链接: https://codeforces.com/contest/1217/problem/D 题意: 给图染上$k$种颜色,相同颜色不能形成一个环 数据范围: $1\leq n \leq 5 ...
- Coloring Edges 【拓扑判环】
题目链接:https://vjudge.net/contest/330119#problem/A 题目大意: 1.给出一张有向图,给该图涂色,要求同一个环里的边不可以全部都为同一种颜色.问最少需要多少 ...
- Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)
题意:https://codeforc.es/contest/1217/problem/D 给你一个有向图,要求一个循环里不能有相同颜色的边,问你最小要几种颜色染色,怎么染色? 思路: 如果没有环,那 ...
- Educational Codeforces Round 72 (Rated for Div. 2)
https://www.cnblogs.com/31415926535x/p/11601964.html 这场只做了前四道,,感觉学到的东西也很多,,最后两道数据结构的题没有补... A. Creat ...
- Educational Codeforces Round 72
目录 Contest Info Solutions A. Creating a Character B. Zmei Gorynich C. The Number Of Good Substrings ...
- 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 ...
- codeforces1217-edu
C The Number Of Good Substrings 我原来的基本思路也是这样,但是写的不够好 注意算前缀和的时候,字符串起始最好从1开始. #include<cstdio> # ...
- POJ 1419 Graph Coloring(最大独立集/补图的最大团)
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4893 Accepted: 2271 ...
- POJ1419 Graph Coloring(最大独立集)(最大团)
Graph Coloring Time Limit: 1000MS Memor ...
随机推荐
- C++基础--引用的一点补充
这一篇是对引用的一点补充,内容基本上是来自<C++ primer plus>一书第八章的内容. 前面一篇介绍了引用的一点特点,这里补充一个,将引用用于类对象的时候,有一个体现继承的特征,就 ...
- EF Core的安装及入门
一.环境准备 1.开发环境:.NET Core 3.1 2.IDE工具:Visual Studio 2019 3.数据库:SQL Server 2012 二.EF Core的安装 1.新建一个项目,如 ...
- html 鼠标样式 鼠标悬停 小手样式
在style中添加cursor:pointer 实现鼠标悬停变成小手样式 先来一个示例 <div style="float:right"> <a class=&q ...
- java中内存的划分
java中内存的划分 栈(stack):存放的都是方法中的局部变量.方法的运行一定要在栈当中运行. 局部变量:方法的参数,或者是方法{}内部的变量 作用域:一旦超出作用域,立刻从占内存当中消失. 堆( ...
- 低JAVA版本,高兼容性启动
低JAVA版本,高兼容性启动 背景:部分操作系统java环境低版本,暂时无法更新最新版本,新系统需要使用较高版本Java环境 1.JAVA低版本不兼容当前应用 2.解压安装JAVA,无需配置环境变量 ...
- java 的二分算法
二分算法 就是在 一组 有序 数组中 通过中间值(数组中间的那个数字)的方法 找到 某个数的下标,如果大于中间值 ,则在中间值与最大值之间 的中间值再比较. public class two { // ...
- 18 12 14 python提高 装饰器
---恢复内容开始--- 装饰器还真的有些没看懂 一个任意传参的装饰器 一个通用装饰器 def set_func(func): print("------开始进行装饰") # ...
- i春秋-web-爆破-1
题目内容:flag就在某六位变量中. 题目 include "flag.php"; $a = @$_REQUEST['hello']; if(!preg_match('/^\w*$ ...
- hibernate结果集多种映射方案
String sql = "select marker_no AS markerNo,name from lv_marker"; String sqlMo = "sele ...
- kotlin黑马影音项目学习笔记
1.包布局 --------model--------presenter----------------impl----------------interf--------view--------ui ...