https://www.luogu.org/problem/show?pid=2661

傻逼图论题,把我写成傻逼了。

DFS找环,每个结点第二次访问时更新答案。

但是图会有几个连通块,所以要分开讨论。

#include <iostream>
#include <cstring>
#include <cmath>
#define maxn 200005
using namespace std;
int n, nxt[maxn];
int dfn[maxn], color[maxn], ans = 0x7f7f7f7f;
void dfs(int v, int d, int c)
{
if (dfn[v])
{
if(color[v] == c)
ans = min(ans, d - dfn[v]);
}
else
{
dfn[v] = d;
color[v] = c;
dfs(nxt[v], d + , c);
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n;
for (int i = ; i <= n; i++)
cin >> nxt[i];
for (int i = ; i <= n; i++)
{
if (!dfn[i])
dfs(i, , i);
}
cout << ans;
return ;
}

最新文章

  1. Jmeter MySQL数据库性能测试
  2. slick for play 使用原生sql查询以及拼接sql
  3. Windows Azure Active Directory (3) China Azure AD增加新用户
  4. topcoder SRM 623 DIV2 CatAndRat
  5. OS X 下iso刻录U盘
  6. JavaScript+IndexedDB实现留言板:客户端存储数据
  7. Spring MVC Controller 单元测试
  8. 对话 UNIX: 关于 inode
  9. UVALive 6584 Escape (Regionals 2013 &gt;&gt; Europe - Central)
  10. css——样式表分类,选择器
  11. NYOJ127 星际之门(一)(最小生成数的个数+高速幂)
  12. Android开发之Android&#160;Context&#160;Menu
  13. sql查询一个字段多列值合并为一列
  14. k8s应用首页临时改成升级维护页面
  15. JS 中的 __proto__ 、prototype、constructor
  16. vue2的缓存问题(非原创)
  17. 【源码分析】HashMap源码再读-基于Java8
  18. idea下增加scala
  19. [bzoj1021][SHOI2008]Debt 循环的债务 (动态规划)
  20. VirtualBox network / study environment setup for RHEL

热门文章

  1. Be the Winner
  2. IdentityServer4 登录使用数据库
  3. 一键生成koa/koa2项目:
  4. 全局作用域 eval
  5. 虚拟软件vmware安装
  6. C语言实现二叉树的基本操作
  7. HTTP响应头信息(Response Headers)与请求头信息(Request Headers)
  8. 通过geotools读写shp文件
  9. Vue.js优雅的实现列表清单的操作
  10. hadoop启动name失败