Masked Ball (Codevs 1800)

Source: Internet
Author: User
Tags greatest common divisor

Title Description Description

The annual masquerade starts again, and the building is in the mood for this year's dance. This year's masks are specially customized by the organizers. Everyone who attends the party can choose a mask they like when they enter. Each mask has a number that the organizer will tell the person who took the mask. In order to make the dance more mysterious, the organizer divides the mask into K (k≥3) class, and uses the special technique to mark each mask's number on the mask, only the Daidi I masks can see the number of the person wearing the I+1 class mask, the person wearing the category K mask can see the number of the person wearing the 1th class mask. The party did not know how many masks there were, but the building was particularly curious about how many masks he wanted to figure out, and he began to gather information in the crowd. The information gathered by the building was the number of the masked person who had seen the first-date mask. The person wearing the 2nd mask saw the number of the 5th number. The building itself will also see some numbers, and he will add the information according to his mask number. Since not everyone can remember all the numbers they see, the information collected by the building does not guarantee its integrity. Now, please calculate, according to the current information obtained in the building, at most and at least how many kinds of masks. As the organizer has declared the k≥3, you must take this information into account as well.

Enter a description Input Description

Input file party.in The first line contains two integers n, m, separated by a space, n means how many masks are prepared by the organizer, and M indicates how many messages the building collects. Next M-line, each act two separated by a space of integer A, b, the person wearing the mask a saw the number of the Mask B number. The same number of pairs A, B may appear multiple times in the input file.

Output description Output Description

The output file Party.out contains two numbers, the first number is the maximum possible mask class number, the second number is the smallest possible mask class number. If it is not possible to divide all masks into at least 3 classes so that the information is satisfied, it is considered that the information collected by the building is faulty and outputs two-1.

Sample input Sample Input

"Input Sample One"
6 5 1 2 2 3 3 4 4 1 3 5
"Input Sample Two"
3 3 1 2 2 1 2 3

Sample output Sample Output

"Output Example One"
4 4
"Output Example II"
-1-1

Data range and Tips Data Size & Hint

50% of data, meet n≤300, m≤1000;

100% of data, meet n≤100000, m≤1000000.

/*first clear the idea: when the ring, all the ring length of the greatest common divisor is Ansmax, ansmin for the ansmax of the smallest approximate, no ring, all straight chain length and for the ansmax,ansmin of 3 (ANS>3) procedure: In the establishment of the change,   Creates an edge with a side right of-1 in reverse.  We want to maintain a timestamp that records the time of the first search to a point, and when the point is searched again, the depth-timestamp is the ring length. Explanation: For example, a->b->c->d,a->e->d, although there is no ring, but will be repeated search D, then the timestamp of D is 3, when the "ring Length" is abs (2-3) = 1. */#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#defineN 100010#defineM 1000010using namespacestd;inthead[n],dfn[n],vis[n],cnt,ansmax,ansmin,mx,mn;structnode{intV,t,pre;}; Node E[m];intgcdintAintb) {    if(!B)returnA; returnGCD (b,a%b);}intRead () {CharC=getchar ();intnum=0;  while(c<'0'|| C>'9') {c=GetChar ();}  while(c>='0'&&c<='9') {num=num*Ten+c-'0'; c=GetChar ();} returnnum;}voidAddintXintYintz) {e[++cnt].v=y; E[CNT].T=Z; E[cnt].pre=Head[x]; HEAD[X]=CNT;}voidDFS1 (intx) {Vis[x]=1;  for(intI=head[x];i;i=e[i].pre)if(!VIS[E[I].V]) {DFN[E[I].V]=dfn[x]+e[i].t;      DFS1 (E[I].V); }      ElseANSMAX=GCD (Ansmax,abs (dfn[x]+e[i].t-DFN[E[I].V]));}voidDFS2 (intx) {Vis[x]=1; MX=Max (mx,dfn[x]); MN=min (mn,dfn[x]);  for(intI=head[x];i;i=e[i].pre)if(!VIS[E[I].V]) {DFN[E[I].V]=dfn[x]+e[i].t;      DFS2 (E[I].V); }}intMain () {intN=read (), m=read ();  for(intI=1; i<=m;i++)    {        intX=read (), y=read (); Add (x, Y,1); Add (y,x,-1); }     for(intI=1; i<=n;i++)      if(!Vis[i]) dfs1 (i); if(Ansmax) for(ansmin=3; ansmin<ansmax&&ansmax%ansmin;ansmin++); Else{memset (DFN,0,sizeof(DFN)); memset (Vis,0,sizeof(VIS));  for(intI=1; i<=n;i++)          if(!Vis[i]) {MX=0; mn=0;              DFS2 (i); Ansmax+=mx-mn+1; } ansmin=3; }    if(ansmax<3) printf ("-1-1"); Elseprintf"%d%d", ansmax,ansmin); return 0;}
View Code

Masked Ball (Codevs 1800)

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.