#P1477. [NOI2008] 假面舞会

    ID: 468 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>图论2008NOI 系列深度优先搜索,DFS最大公约数,gcd

[NOI2008] 假面舞会

Description

The annual masquerade has begun, and Dongdong is excited to attend this year’s party.

This year’s masks are specially made by the organizers. Each participant can choose a mask they like upon entry. Every mask has an ID number, and the organizer tells the wearer that number.

To add mystery, the organizer divides masks into kk classes (k3k \geq 3). Using special techniques, the number of each mask is marked so that only people wearing a class ii mask can see the numbers of people wearing class i+1i+1 masks, and people wearing class kk masks can see the numbers of people wearing class 11 masks.

Participants do not know how many classes there are, but Dongdong is very curious and wants to figure it out himself, so he starts collecting information from the crowd.

The information Dongdong collects is of the form: “the wearer of mask number aa saw the number of mask bb.” For example, the wearer of mask 22 saw the number of mask 55. Dongdong also sees some numbers himself, and he adds the corresponding information using his own mask number.

Since not everyone can remember all the numbers they saw, Dongdong’s collected information may be incomplete. Please compute, based on the information Dongdong currently has, the maximum and the minimum possible numbers of mask classes. Since the organizer has declared k3k \geq 3, you must also take this into account.

Input Format

The first line contains two integers n,mn, m, separated by a space, where nn is the total number of masks prepared by the organizer, and mm is the number of pieces of information Dongdong collected. The next mm lines each contain two integers a,ba, b separated by a space, meaning the wearer of mask aa saw the number of mask bb. The same pair a,ba, b may appear multiple times in the input.

Output Format

Output two numbers: the first is the maximum possible number of mask classes, and the second is the minimum possible number of mask classes. If it is impossible to partition all masks into at least 33 classes so that all the information is satisfied, print two -1.

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

3 3
1 2
2 1
2 3
-1 -1

Hint

  • For 50%50\% of the testdata, n300n \leq 300, m103m \leq 10^3.
  • For 100%100\% of the testdata, n105n \leq 10^5, m106m \leq 10^6.

Translated by ChatGPT 5