Croc Champ 2012 - Final
C
Cyclic Coloring
Codeforces
2200
原题
← BD →
翻译中…

You are given a directed graph $*G*$ with $*n*$ vertices and $*m*$ arcs (multiple arcs and self-loops are allowed). You have to paint each vertex of the graph into one of the $*k*$ $(*k* ≤ *n*)$ colors in such way that for all arcs of the graph leading from a vertex $*u*$ to vertex $*v*$, vertex $*v*$ is painted with the next color of the color used to paint vertex $*u*$.

翻译中…

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

翻译中…

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

翻译中…

Input

翻译中…

The first line contains two space-separated integers $*n*$ and $*m*$ ($1 ≤ *n*, *m* ≤ 10^{5}$), denoting the number of vertices and the number of arcs of the given digraph, respectively.

翻译中…

Then $*m*$ lines follow, each line will contain two space-separated integers $*a*_{*i*}$ and $*b*_{*i*}$ ($1 ≤ *a*_{*i*}, *b*_{*i*} ≤ *n*$), which means that the $*i*$-th arc goes from vertex $*a*_{*i*}$ to vertex $*b*_{*i*}$.

翻译中…

Multiple arcs and self-loops are allowed.

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

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

翻译中…

image

翻译中…

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

翻译中…

image

翻译中…

It can be proven that no larger value for $*k*$ exists for this test case.

翻译中…

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

翻译中…

image

翻译中…

A possible coloring of the graph is:

翻译中…

image

翻译中…

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

翻译中…

image

翻译中…

A possible coloring of the graph is:

翻译中…

image

翻译中…

样例

Input #1
4 4
1 2
2 1
3 4
4 3
Output #1
2
InputCopy OutputCopy
Input #2
5 2
1 4
2 5
Output #2
5
InputCopy OutputCopy
Input #3
4 5
1 2
2 3
3 1
2 4
4 1
Output #3
3
InputCopy OutputCopy
Input #4
4 4
1 1
1 2
2 1
1 2
Output #4
1
InputCopy OutputCopy
题目信息
题号
183C
时间限制
4 s
内存限制
256 MB
标签
DFS 及类似

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。