CF1217D.Coloring Edges

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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

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

给你一个包含 nn 个顶点和 mm 条有向边的有向图,图中不含自环或重边。

我们定义有向图的 kk-染色如下:将每条边染成 kk 种颜色之一。当且仅当不存在由同一种颜色的边构成的有向环时,该 kk-染色称为“好”的。

请为给定的有向图找出一个“好”的 kk-染色,使得 kk 尽可能小。

输入格式

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

Next mm lines contain description of edges — one per line. Each edge is a pair of integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) — there is directed edge from uu to vv in the graph.

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

第一行包含两个整数 nn 和 mm(2≤n≤50002 \le n \le 5000,1≤m≤50001 \le m \le 5000),分别表示有向图中的顶点数和边数。

接下来的 mm 行描述各条边,每行一条边。每条边由一对整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,且 u≠vu \ne v)组成,表示图中存在一条从 uu 指向 vv 的有向边。

保证在边列表中,每个有序对 (u,v)(u, v) 至多出现一次。

输出格式

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

In the second line print mm integers c1,c2,…,cmc_1, c_2, \dots, c_m (1≤ci≤k1 \le c_i \le k), where cic_i is a color of the ii-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 kk).

第一行输出一个整数 kk —— 给定图的一个良好 kk-着色中所使用的颜色数。

第二行输出 mm 个整数 c1,c2,…,cmc_1, c_2, \dots, c_m(其中 1≤ci≤k1 \le c_i \le k),cic_i 表示第 ii 条边的颜色(按输入中给出的顺序)。

若存在多个答案,输出任意一个即可(但你仍需使 kk 最小)。

输入输出样例

  • 输入#1

    4 5
    1 2
    1 3
    3 4
    2 4
    1 4

    输出#1

    1
    1 1 1 1 1
  • 输入#2

    3 3
    1 2
    2 3
    3 1

    输出#2

    2
    1 1 2

输入解题思路,AI测评打分。不知道怎么写?

首页