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 ...
随机推荐
- Egret Engine 2D - 项目配置
todo 看三个示例项目的完整源码和资源 <e:Group name="Button" height = "300" verticalCent ...
- 汇编,寄存器,内存,mov指令
一.代码 和 汇编 和 二进制之间的关系 二.复习一下计算机组成原理的知识 1.寄存器 计算机中有三个存储 32位cpu提供的寄存器有三种类型8位 16位 32位 64位的只是32位的扩展 并且程序大 ...
- equals与hashcode分析
我们经常在面经中看到这样的问题,为什么重写equals方法就一定要重写hashcode方法.本文就是分析这个问题. <!--more--> 在阿里巴巴java开发手册中就给出了这样的规则. ...
- Javascript里EQ、NE、GT、LT、GE、LE含义
EQ 就是 EQUAL等于 NE就是 NOT EQUAL不等于 GT 就是 GREATER THAN大于 LT 就是 LESS THAN小于 GE 就是 GREATER THAN OR EQUAL ...
- POJ 1164:The Castle
The Castle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6677 Accepted: 3767 Descri ...
- 转 SQL 的数据库 架构规范 之 58到家数据库30条军规解读
军规适用场景:并发量大.数据量大的互联网业务 军规:介绍内容 解读:讲解原因,解读比军规更重要 一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存 ...
- dxSkinController1 皮肤使用
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- MySQL中间件介绍
360 Atlas Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化 ...
- Mac系统的SVN客户端:Snail SVN 精简版
Mac系统的SVN客户端:Snail SVN 精简版 前言 本人在公司中,使用的是windows操作系统,svn客户端自然也就使用tortoise svn.但自从男朋友给我买了台macbook pro ...
- image compression with libjpeg
http://www.aaronmr.com/en/2010/03/test/ Working on the project I've seen in the need for compression ...