/*
分三种情况
1 有环:找环长的gcd作为max gcd的超过2的最小因子作为min
2 树:所有最长链的和作为max 3为min (最长链≥3)
3 两条相交链:找出所有的这样的两条链的差 同1求
显然第三种不好处理 看了别人的做法是+反向-1的边 1 3就能合并了.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 1000010
#define pa pair<int,int>
using namespace std;
int n,m,num,head[maxn],dfn[maxn],topt;
int c[maxn],sum,Gcd,ans,maxx,minn,x,y,d[maxn];
bool f[maxn];
struct node
{
int v,pre,t;
}e[maxn],ei[maxn];
int gcd(int a,int b)
{
return !b?a:gcd(b,a%b);
}
int abs(int a)
{
return a<?-a:a;
}
void Add(int from,int to,int dis)
{
num++;e[num].v=to;
e[num].t=dis;
e[num].pre=head[from];
head[from]=num;
}
void Tarjan(int x)
{
dfn[x]=++topt;f[x]=;
for(int i=head[x];i;i=e[i].pre)
{
int v=e[i].v;
if(f[v]==)d[v]=d[x]+e[i].t,Tarjan(v);
else c[++sum]=abs(d[x]-d[v]+e[i].t);
}
}
int Get_gcd()
{
Gcd=c[];
for(int i=;i<=sum;i++)
Gcd=gcd(Gcd,c[i]);
return Gcd;
}
int Get_min()
{
int tmp=Gcd+;
for(int i=;i<=Gcd;i++)
if(Gcd%i==)
tmp=min(tmp,i);
if(tmp==Gcd+)tmp=-;
return tmp;
}
void Dfs(int x)
{
f[x]=;
for(int i=head[x];i;i=e[i].pre)
{
int v=e[i].v;
if(f[v])continue;
d[v]=d[x]+e[i].t;Dfs(v);
}
maxx=max(maxx,d[x]);
minn=min(minn,d[x]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d",&x,&y),Add(x,y,),Add(y,x,-);
for(int i=;i<=n;i++)
if(dfn[i]==)Tarjan(i);
Gcd=Get_gcd();ans=Get_min();
if(Gcd)
{
if(Gcd<)Gcd=ans=-;
printf("%d %d\n",Gcd,ans);
return ;
}
ans=;memset(f,,sizeof(f));
for(int i=;i<=n;i++)
if(f[i]==)
{
maxx=;minn=maxn;Dfs(i);
ans+=maxx-minn+;
}
if(ans>)printf("%d 3\n",ans);
else printf("-1 -1\n");
return ;
}

2008年NOI全国竞赛 假面舞会的更多相关文章

  1. Codevs 1800 假面舞会 2008年NOI全国竞赛

    1800 假面舞会 2008年NOI全国竞赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 一年一度的假面舞会又开始了,栋栋也 ...

  2. Codevs 1074 食物链 2001年NOI全国竞赛

    1074 食物链 2001年NOI全国竞赛 时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 动物王国中有三类动物 A,B ...

  3. 1729 单词查找树 2000年NOI全国竞赛

    1729 单词查找树 2000年NOI全国竞赛 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master         题目描述 Description 在进行文法分析的 ...

  4. 1074 食物链 2001年NOI全国竞赛

    1074 食物链 2001年NOI全国竞赛 时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond         题目描述 Description 动物王国中有三类动物 ...

  5. CODEVS 1074 食物链 2001年NOI全国竞赛(洛谷 P2024)

    题目描述 Description 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B,B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并 ...

  6. 食物链 2001年NOI全国竞赛

    时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond   题目描述 Description 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B ...

  7. 1722 最优乘车 1997年NOI全国竞赛

    题目描述 Description H城是一个旅游胜地,每年都有成千上万的人前来观光.为方便游客,巴士公司在各个旅游景点及宾馆,饭店等地都设置了巴士站并开通了一些单程巴上线路.每条单程巴士线路从某个巴士 ...

  8. 银河英雄传说 2002年NOI全国竞赛

    时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年 ...

  9. NOI全国赛(2001)--食物链

    今天写了道并查集的题,看来并查集的题刷少了,,,,,用法好神奇啊!!!开三倍并查集 用i表示自己,i+n存天敌,i+2*n存可以克制de,再逻辑判断一下即可. 所以,要意识到并查集的分类处理可以开不同 ...

随机推荐

  1. python密码处理(可用于生产模式)

    import os from hashlib import sha256 from hmac import HMAC def encrypt_password(password, salt=None) ...

  2. 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)

    题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...

  3. 下拉列表框 select 动态赋值

    <tr> <td class="label">所属群组:</td> <td> <select name="group ...

  4. jQuery获取JSON格式数据方法

    getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ ...

  5. Day1 初识Python

    (1)变量与赋值 name = "wanghuafeng" age = 29 print(name, age) a和b交换值 a = 3 b = 5 tmp = a a = b b ...

  6. 如何在django中使用多个数据库

    http://blog.chinaunix.net/uid-16728139-id-4364343.html 

  7. 处理 InnerException 最佳方案?

    如何获取 innerException 内部错误信息 String innerMessage = (ex.InnerException != null) ? ex.InnerException.Mes ...

  8. jQuery的选择器中的通配符[id^='code'] 【转】

    JQuery 1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='cod ...

  9. Windows消息编程(写的不错,有前因后果)

    本文主要包括以下内容: 1.简单理解Windows的消息2.通过一个简单的Win32程序理解Windows消息3.通过几个Win32程序实例进一步深入理解Windows消息4.队列消息和非队列消息5. ...

  10. 324篇CSS应用效果集合(梦想天空,可同时应用于Web开发与Qt界面开发)

    http://www.cnblogs.com/lhb25/category/146075.html http://www.yyyweb.com/