1800 Masked Ball

Source: Internet
Author: User
Tags gcd greatest common divisor

1800 Masked Ball

2008 NOI National Competition

time limit: 1 sspace limit: 128000 KBtopic rank: Master Master SolvingView Run ResultsTitle 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.

Category labels Tags Click here to expandMainland Region Noi national competition 2008The following:

1, when the diagram has a ring, K must be the ring length of the approximate, then the answer is all the ring greatest common divisor and the minimum number of more than 3 of the Convention, if greatest common divisor less than 3 is no solution;

When there is no ring in the diagram, the K-Max is the longest chain of all the unicom blocks and
2, when implemented, all sides of the length of 1 of the forward edge and length of 1 of the opposite edge, it will be easy to handle a lot of

AC Code:

#include <cstdio>#include<cstring>#include<cstdlib>#include<iostream>using namespacestd;Const intn=2e5+Ten;Const intm=2e6+Ten;intN,m,ans1,ans2,mi,ma,tot,d[n],v[m],w[m],next[m],head[m];BOOLVis[n];inlineintRead () {registerintx=0, f=1; RegisterCharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}voidAddintXintYintz) {v[++tot]=y;w[tot]=z;next[tot]=head[x];head[x]=tot;}intgcdintAintb) {    return!B?A:GCD (b,a%b);}voidTarjan (intx) {//determine if there is a ringvis[x]=1;  for(intI=head[x];i;i=Next[i])if(Vis[v[i]]) ans1=gcd (Ans1,abs (d[x]+w[i]-d[v[i])); Elsed[v[i]]=d[x]+W[i],tarjan (V[i]);}voidDfsintx) {Vis[x]=1; Ma=max (Ma,d[x]), mi=min (mi,d[x]);  for(intI=head[x];i;i=Next[i])if(!vis[v[i]]) d[v[i]]=d[x]+W[i],dfs (V[i]);}intMain () {n=read (); m=read ();  for(intI=1, a,b;i<=m;i++) A=read (), B=read (), add (b,1), Add (b,a,-1);  for(intI=1; i<=n;i++)if(!Vis[i]) Tarjan (i); if(ANS1) for(ans2=3; ans2<ans1&&ans1%ans2;ans2++);//with Ring    Else{//No ringmemset (Vis,0,sizeofvis);  for(intI=1; i<=n;i++){            if(!Vis[i]) {Ma=mi=d[i]=0;                DFS (i); Ans1+=ma-mi+1; }} ans2=3; }    if(ans1<3) ans1=ans2=-1; printf ("%d%d\n", ANS1,ANS2); return 0;} 

1800 Masked Ball

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.