CF183C.Cyclic Coloring

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a directed graph GG with nn vertices and mm arcs (multiple arcs and self-loops are allowed). You have to paint each vertex of the graph into one of the kk (k<=n)(k<=n) colors in such way that for all arcs of the graph leading from a vertex uu to vertex vv , vertex vv is painted with the next color of the color used to paint vertex uu .

The colors are numbered cyclically 11 through kk . This means that for each color ii (i<k) its next color is color i+1i+1 . In addition, the next color of color kk is color 11 . Note, that if k=1k=1 , then the next color for color 11 is again color 11 .

Your task is to find and print the largest possible value of kk (k<=n)(k<=n) such that it's possible to color GG as described above with kk colors. Note that you don't necessarily use all the kk colors (that is, for each color ii there does not necessarily exist a vertex that is colored with color ii ).

输入格式

The first line contains two space-separated integers nn and mm ( 1<=n,m<=1051<=n,m<=10^{5} ), denoting the number of vertices and the number of arcs of the given digraph, respectively.

Then mm lines follow, each line will contain two space-separated integers aia_{i} and bib_{i} ( 1<=ai,bi<=n1<=a_{i},b_{i}<=n ), which means that the ii -th arc goes from vertex aia_{i} to vertex bib_{i} .

Multiple arcs and self-loops are allowed.

输出格式

Print a single integer — the maximum possible number of the colors that can be used to paint the digraph (i.e. kk , as described in the problem statement). Note that the desired value of kk must satisfy the inequality 1<=k<=n1<=k<=n .

输入输出样例

  • 输入#1

    4 4
    1 2
    2 1
    3 4
    4 3
    

    输出#1

    2
    
  • 输入#2

    5 2
    1 4
    2 5
    

    输出#2

    5
    
  • 输入#3

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

    输出#3

    3
    
  • 输入#4

    4 4
    1 1
    1 2
    2 1
    1 2
    

    输出#4

    1
    

说明/提示

For the first example, with k=2k=2 , this picture depicts the two colors (arrows denote the next color of that color).

With k=2k=2 a possible way to paint the graph is as follows.

It can be proven that no larger value for kk exists for this test case.

For the second example, here's the picture of the k=5k=5 colors.

A possible coloring of the graph is:

For the third example, here's the picture of the k=3k=3 colors.

A possible coloring of the graph is:

首页