Noi2008 masked dance

Source: Internet
Author: User
1064: [noi2008] masked ball time limit: 10 sec memory limit: 162 MB
Submit: 883 solved: 462
[Submit] [Status] Description

The annual fake Noodle dance started again, and Dongdong attended this year's dance with great enthusiasm. This year's masks were specially customized by the organizers. Everyone attending the dance can select a mask that they like when entering the venue. Each mask has a number, and the organizer will tell the person who takes the mask. To make the dance more mysterious, the organizer divides the mask into K (k ≥ 3) categories and uses special technologies to mark each mask with numbers, only those wearing class I masks can see the ID of the person wearing the class I + 1 mask, and those wearing the class K mask can see the number of the person wearing the class I mask. The person attending the dance did not know how many masks there were, but Dong was very curious about this. He wanted to figure out how many masks he had, so he began to collect information in the crowd. The information collected by Dongdong is the number of the mask worn by the person wearing the mask. For example, the person wearing the mask No. 2nd saw the mask number. Dong himself will also see some numbers, and he will also add the information according to his mask number. Because not everyone can remember all the numbers they see, the information collected by Dongdong cannot guarantee its integrity. Please calculate the number of masks at most and at least according to the information currently obtained by Dongdong. Since the organizer has declared k ≥ 3, you must take this information into consideration.

Input

The first line contains two integers, N and M, separated by a space. N represents the total number of masks prepared by the organizer, and m represents the number of pieces of information collected by the building. Next, in line m, two integers A and B separated by spaces indicate that the person wearing the mask a sees the number of the mask B. The same number of pairs A and B may appear multiple times in the input file.

Output

Contains two numbers. The first number is the maximum possible number of mask classes, and the second number is the minimum possible number of mask classes. If all masks cannot be divided into at least three categories so that all the information is satisfied, the information collected by Dong is considered wrong and two-1 is output.

Sample input [input Example 1]

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

[Example 2]

3 3
1 2
2 1
2 3

Sample output [sample output]

4

[Output Example 2]

-1-1
[Data scale and Conventions]

50% of data, meeting n ≤ 300, m ≤ 1000;
100% of data, meeting n ≤ 100000, m ≤ 1000000.
Hint

Question:

How can I think of such questions in the test room? Lyd question

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This question is easy to think of as a graph topic. If a can see B, it means that the number of B is 1 larger than that of A, and A is connected to an edge with a weight of 1 from A to B. However, this is not enough. A side with a weight of-1 needs to be connected from B to a (which will be explained later ). After creating the graph, perform DFS for each connected component and obtain the length of each ring (the length is 0 when the number of edge bars passing through 1 and-1 are equal, this is not counted as a ring). GCD is the maximum answer for the length of all rings. The minimum approximate number greater than or equal to 3 is the minimum answer. If no ring exists, the sum of the maximum depth of all connected components is obtained as the maximum answer. The minimum answer is 3. If the maximum answer is less than 3, no solution is obtained and-1 is output.

The specific method for calculating the length of a ring is as follows: Take a vertex label as 0, and then perform DFS. The number of points that reach through an edge with a weight of 1 is the current vertex label + 1, the number of points that reach through an edge with the weight of-1 is the current vertex number-1. If the point (marked as X) that is reached by an edge has been accessed, it means that a ring is found and its length is: now we want to subtract the absolute value of the original number of X points from the number of X points. At this time, the number of X points remains unchanged.

Now explain why the-1 side is connected. Similar to a-> B-> C-> D, a-> E-> D, this graph is an inverse example, because although this graph does not have a ring, however, the numbers of A and D are unique. It is definitely wrong to process the sum of the maximum depth as a acyclic graph. This graph has no solution. Therefore, we need to connect the edge of-1. At this time, there is a ring with a length of 1, 1 <3, so there is no solution.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Ms dfs, BFs is king

Code:

  1 const maxn=100000+10;maxm=1000000+10;  2 type node=record  3      from,go,next,w:longint;  4      end;  5 var e:array[0..maxm] of node;  6     mi,mx,n,m,x,y,i,ans1,ans2,tot:longint;  7     head,d:array[0..maxn] of longint;  8     v:array[0..maxn] of boolean;  9     circle:boolean; 10     function gcd(a,b:longint):longint; 11      begin 12      if b=0 then exit(a) else exit(gcd(b, a mod b)); 13      end; 14  15 function min(x,y:longint):longint; 16  begin 17  if x<y then exit(x) else exit(y); 18  end; 19 function max(x,y:longint):longint; 20  begin 21  if x>y then exit(x) else exit(y); 22  end; 23  24 procedure insert(x,y,Z:longint); 25  begin 26  inc(tot); 27  with e[tot] do 28  begin 29  from:=x;go:=y;w:=z;next:=head[x];head[x]:=tot; 30  end; 31  end; 32 procedure init; 33  begin 34  readln(n,m); 35  for i:=1 to m do 36   begin 37   readln(x,y); 38   insert(x,y,1);insert(y,x,-1); 39   end; 40  end; 41 procedure dfs(x:longint); 42  var i,y:longint; 43  begin 44  v[x]:=true; 45  i:=head[x]; 46  while i<>0 do 47   begin 48   y:=e[i].go; 49   if (v[y]) and (d[y]<>d[x]+e[i].w) then 50     begin 51       if circle then ans1:=gcd(ans1,abs(d[y]-(d[x]+e[i].w))) 52       else ans1:=abs(d[y]-(d[x]+e[i].w)); 53       circle:=true; 54     end; 55   if (not(v[y])) then 56     begin 57       d[y]:=d[x]+e[i].w; 58       mi:=min(mi,d[y]); 59       mx:=max(mx,d[y]); 60       dfs(y); 61     end; 62   i:=e[i].next; 63   end; 64  end; 65  66 procedure main; 67  begin 68  circle:=false; 69  fillchar(v,sizeof(v),false); 70  fillchar(d,sizeof(d),false); 71  for i:=1 to n do 72    begin 73     if not(v[i]) then 74       begin 75         mi:=0;mx:=0; 76         dfs(i); 77         inc(ans2,mx-mi+1); 78       end; 79    end; 80   if not circle then 81    begin 82      if ans2<3 then write(-1,‘ ‘,-1) 83      else write(ans2,‘ ‘,3); 84    end 85   else 86    begin 87      if ans1<3 then write(-1,‘ ‘,-1) 88      else 89       begin 90         write(ans1,‘ ‘); 91         for i:=3 to ans1 do 92          if ans1 mod i=0 then break; 93         write(i); 94       end; 95    end; 96 end; 97  98  99 begin100   assign(input,‘party.in‘);assign(output,‘party.out‘);101   reset(input);rewrite(output);102   init;103   main;104   close(input);close(output);105 end.                                    
View code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.