因为公用一个系统所以大家求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. Codeforces 148D Bag of mice:概率dp 记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...

  3. longtable 跨越多个页面时,如何在跨页时自动断行并加上横线及去掉页眉

    参考: http://users.sdsc.edu/~ssmallen/latex/longtable.html 一般的,在首行后面加上 \endfirsthead\hline\endhead\hli ...

  4. 生成0-42之间的7个不重复的int值

    public static void main(String[] args) { //set集合存储不重复无序的值 Set<Integer> set = new HashSet<In ...

  5. uC/OS-II源码分析(六)

    μC/OS-Ⅱ总是运行进入就绪态任务中优先级最高的那一个.确定哪个任务优先级最高, 下面该哪个任务运行了的工作是由调度器(Scheduler)完成的.任务级的调度是由函数 OSSched()完成的.中 ...

  6. 关于Android的HAL的一些理解

    之前一直在学习基于Linux内核的一些字符型驱动的编程,对Linux内核驱动也算有了一些基本的了解吧,后来也做过一些基于Linux内核的驱动开发,像基于Android的CC1101高频模块的驱动开发, ...

  7. Windows C/C++调试

    windows兼容dirent.h error c4996: 'fopen': This function or variable may be unsafe This file requires _ ...

  8. POJ1330(LCA入门题)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23388   Accept ...

  9. 如何解决 Matlab 画图时中文显示乱码的问题?

    使用的是win10系统,从前几个月某一天,我的matlab的figure里的中文都变成了口口.很是郁闷,还以为是动到了什么配置引起的. 前几天更新了matlab 2018b,发现还有这个问题.就觉得不 ...

  10. STL::next_permutation();

    next_permutation()可以按字典序生成所给区间的全排列. 在STL中,除了next_permutation()外,还有一个函数prev_permutation(),两者都是用来计算排列组 ...