因为公用一个系统所以大家求gcd;衡量各点之间的拓扑位置,如果到达同一点有不同的长度则取gcd。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = 1e5 + 5;
int n, m, ans;
int val[maxn];
vector<int> adj[maxn], len[maxn];
bool vis[maxn]; int gcd(int a, int b) {
if (!a || !b) return a + b;//ans初始为0所以有此写法
return gcd(b, a % b);
} void dfs(int cur, int id) {
vis[cur] = 1;
val[cur] = id;
for (int i = 0; i < adj[cur].size(); i++) {
int son = adj[cur][i], l = len[cur][i];
if (!vis[son]) {
dfs(son, id + l);
} else if (val[son] != id + l) {//到达同一点有不同长度
ans = gcd(ans, abs(id + l - val[son]));
}
}
} int main() {
scanf("%d %d", &n, &m);
for (int i = 1, u, v; i <= m; i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
len[u].push_back(1);
//通过这种方式来计算两两点之间的拓扑相对位置
adj[v].push_back(u);
len[v].push_back(-1);
}
for (int i = 1; i <= n; i++)
if (!vis[i])
dfs(i, 0);
if (!ans) ans = n;
return !printf("%d\n", ans);
}

Codeforces 183C(有向图上的环长度)的更多相关文章

  1. [hdu5348]图上找环,删环

    http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...

  2. HIT 2739 - The Chinese Postman Problem - [带权有向图上的中国邮路问题][最小费用最大流]

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2739 Time limit : 1 sec Memory limit : 64 M A Chinese ...

  3. Tree Operations 打印出有向图中的环

    题目: You are given a binary tree with unique integer values on each node. However, the child pointers ...

  4. 非负权值有向图上的单源最短路径算法之Dijkstra算法

    问题的提法是:给定一个没有负权值的有向图和其中一个点src作为源点(source),求从点src到其余个点的最短路径及路径长度.求解该问题的算法一般为Dijkstra算法. 假设图顶点个数为n,则针对 ...

  5. Codeforces - 1020B Badge(找环)

    题意: 以每个点为起点,找到第一个出现两次的点 解析: 我是先找出来所有的环  环上的点找出来的肯定是自己 bz[i]  = i; 然后去遍历不在环上的点j  如果通过这个点找到一个已经标记的的点i ...

  6. codeforces 897B Chtholly's request 偶数长度回文数

    codeforces 897B Chtholly's request 题目链接: http://codeforces.com/problemset/problem/897/B 思路: 暴力求出这100 ...

  7. Codeforces 884C.Bertown Subway ----判环,思路

    The construction of subway in Bertown is almost finished! The President of Berland will visit this c ...

  8. GYM 101572I(有向图上最小环)

    逗号空格是假的,全都直接连边就行. 提供一个迪杰n次的图上最小环板子. #include <cstdio> #include <cstring> #include <io ...

  9. Android开发经验之获取画在画布上的字符串长度、宽度(所占像素宽度)

    Android中获取字符串长度.宽度(所占像素宽度) 计算出当前绘制出来的字符串有多宽,可以这么来! 方法1: Paint paint = new Paint(); Rect rect = new R ...

随机推荐

  1. Mac系统存储-其他存储无故增大

    解决办法:打开Finder:安全倾倒废纸篓就会减少很大一部分存储.

  2. BZOJ 2020 [Usaco2010 Jan]Buying Feed,II:贪心【定义价值】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2020 题意: FJ开车去买K份食物. 如果他的车上有X份食物,每走一里就花费X元. FJ的 ...

  3. L91

    Make Healthy Choices Easier Options Telling people to change unhealthy behaviors doesn't work. Other ...

  4. linux 进程学习笔记-运行新进程

    我们知道,当用fork启动一个新进程以后,新进程会复制父进程的大部份内存空间并接着运行父进程中的代码,如果我们使新进程不运行原父进程的代码,转而运行另外一个程序集中的代码,这就相当于启动了一个新程序. ...

  5. linux网络编程 gethostbyname()

    gethostbyname()返回对应于给定主机名的包含主机名字和地址信息的hostent结构指针.结构的声明与gethostaddr()中一致. 返回对应于给定主机名的主机信息. #include ...

  6. Early Media and Music on Hold

    Early media refers to any media that is played to the initial caller’s phone before the remote party ...

  7. Linux下的Tomcat JVM 调优

    1. 适用场景 Tomcat 运行过程遇到Caused by: java.lang.OutOfMemoryError: PermGen space或者java.lang.OutOfMemoryErro ...

  8. ZigBee自组网地址分配与路由协议概述

    1. ZigBee简介 ZigBee是基于IEEE802.15.4标准的低功耗局域网协议.根据国际标准规定,ZigBee技术是一种短距离.低功耗的无线通信技术. ZigBee协议从下到上分别为物理层( ...

  9. MUI框架开发HTML5手机APP

    出处:http://www.cnblogs.com/jerehedu/p/7832808.html  前  言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用H ...

  10. python list列表sort、sorted、reverse排序

    列表排序:sort是修改原列表,sorted提供原列表的一个有序副本 li=[2,1,4,5,0]li.sort() #默认从小到大print li结果:[0, 1, 2, 4, 5] li=[2,1 ...