# BZOJ 1064 假面舞会

http://www.lydsy.com/JudgeOnline/problem.php?id=1064

 1 #include<algorithm>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<iostream>
6 int tot,go[2100005],next[2100005],first[2100005];
7 int c[200005],dis[200005],vis[200005],n,m,len,val[2100005];
9     int t=0,f=1;char ch=getchar();
10     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
11     while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
12     return t*f;
13 }
14 void insert(int x,int y,int z){
15     tot++;
16     go[tot]=y;
17     next[tot]=first[x];
18     first[x]=tot;
19     val[tot]=z;
20 }
22     insert(x,y,1);insert(y,x,-1);
23 }
24 int gcd(int a,int b){
25     if (b==0) return a;
26     else return gcd(b,a%b);
27 }
28 int bfs(int x){
29     int h=1,t=1;c[1]=x;vis[x]=1;dis[x]=0;
30     int mxdis=0,mndis=0;
31     while (h<=t){
32         int now=c[h++];
33         for (int i=first[now];i;i=next[i]){
34             int pur=go[i];
35             if (vis[pur]){
36                 len=gcd(len,val[i]+dis[now]-dis[pur]);
37                 continue;
38             }
39             vis[pur]=1;
40             c[++t]=pur;
41             dis[pur]=dis[now]+val[i];
42             mxdis=std::max(mxdis,dis[pur]);
43             mndis=std::min(mndis,dis[pur]);
44         }
45     }
46     return mxdis-mndis+1;
47 }
48 int main(){
50     for (int i=1;i<=m;i++){
53     }
54     int sum=0;
55     for (int i=1;i<=n;i++)
56      if (!vis[i]) sum+=bfs(i);
57     len=std::abs(len);
58     if (len){
59         if (len<3) {
60             printf("-1 -1\n");
61             return 0;
62         }
63         printf("%d ",len);
64         for (int i=3;i<=len;i++)
65          if (len%i==0) {
66                 printf("%d\n",i);
67                 break;
68          }
69         return 0;
70     }else
71     if (sum<3){
72         printf("-1 -1\n");
73         return 0;
74     }else{
75         printf("%d 3\n",sum);
76         return 0;
77     }
78 }

posted @ 2016-06-17 12:03  GFY  阅读(300)  评论(0编辑  收藏