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