LightOJ - 1417 Forwarding Emails(强连通+dfs)
来源:互联网 发布:怎么设置农村淘宝收货 编辑:程序博客网 时间:2024/05/29 15:15
题目大意:你有一个秘密,但你只能告诉一个。这个人如果有认识的人的话,他就会把你的秘密告诉出去,这样就一传十,十传百了。现在你想要知道,最多能有多少人知道
解题思路:同一个连通分量内的人都是可知的,所以缩点,然后连边,从入度为0的点出发,dfs找出所能到的最远处,在dfs过程中统计一下有多少个人知道
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <map>using namespace std;const int N = 50010;struct Edge{ int u, v, next; Edge() {} Edge(int u, int v, int next): u(u), v(v), next(next) {}}E[N];int head[N], pre[N], lowlink[N], sccno[N], num[N], Stack[N], in[N];int dfs_clock, scc_cnt, top, n, m, cas = 1;bool vis[N];vector<int> V[N];void init() { scanf("%d", &n); memset(head, -1, sizeof(head)); int u, v; for (int i = 0; i < n; i++) { scanf("%d%d", &u, &v); E[i] = Edge(u, v, head[u]); head[u] = i; }}void dfs(int u, int fa) { pre[u] = lowlink[u] = ++dfs_clock; Stack[++top] = u; for (int i = head[u]; ~i; i = E[i].next) { int v = E[i].v; if (!pre[v]) { dfs(v, u); lowlink[u] = min(lowlink[u], lowlink[v]); } else if (!sccno[v]) lowlink[u] = min(lowlink[u], pre[v]); } if (pre[u] == lowlink[u]) { scc_cnt++; int x = 0; num[scc_cnt] = 0; while (1) { x = Stack[top--]; sccno[x] = scc_cnt; num[scc_cnt]++; if (x == u) break; } }}int dfs2(int u){ int Max = 0; for (int i = 0; i < V[u].size(); i++) { int v = V[u][i]; Max = max(Max, dfs2(v)); } return Max + num[u];}void solve() { memset(pre, 0, sizeof(pre)); memset(sccno, 0, sizeof(sccno)); dfs_clock = scc_cnt = 0; for (int i = 1; i <= n; i++) if (!pre[i]) dfs(i, -1); if (scc_cnt == 1) { printf("Case %d: %d\n", cas++, 1); return ; } memset(in, 0, sizeof(in)); for (int i = 1; i <= scc_cnt; i++) V[i].clear(); for (int i = 0; i < n; i++) { int u = sccno[E[i].u]; int v = sccno[E[i].v]; if (u == v) continue; in[v]++; V[u].push_back(v); } int Max = 0, pos; for (int i = 1; i <= scc_cnt; i++) if (!in[i]) { int t = dfs2(i); if (t > Max) { Max = t; pos = i; } } for (int i = 1; i <= n; i++) if (sccno[i] == pos) { printf("Case %d: %d\n", cas++, i); return ; }}int main() { int test; scanf("%d", &test); while (test--) { init(); solve(); } return 0;}
0 0
- LightOJ - 1417 Forwarding Emails(强连通+dfs)
- LightOJ - 1210 Efficient Traffic System(强连通)
- lightoj 1034【强连通+缩点】
- poj 3592 强连通+缩点+dfs
- hdu 3639(强连通分量+dfs)
- 双DFS求强连通分量
- hdoj 1269 迷宫城堡 【强连通】&&【dfs】
- 双向dfs求强连通分量
- 1013 DFS求强连通分量
- CodeForces770C【强连通分量+DFS序】
- [强连通分量]LightOJ 1210 - Efficient Traffic System
- LightOJ 1210 - Efficient Traffic System【强连通图】
- LightOJ - 1034 Hit the Light Switches(强连通)
- lightoj 1063 - Ant Hills(强连通求割点)
- pku 2762(强连通分量+缩点+DFS)
- (beginer)DFS (强连通)UVA 11504 Dominos
- POJ2375 Cow Ski Area 【强连通分量】+【DFS】
- poj 2186 Popular Cows(强连通分量缩点+dfs)
- IP协议详解---Linux学习笔记
- linux如何安装jdk
- Linux虚拟地址空间概述
- Android缓存工具类
- window.location.href不转跳的解决方法 与a标签空链接有关
- LightOJ - 1417 Forwarding Emails(强连通+dfs)
- C++在非面向对象方面的一些特性(基本)
- MySQL的安装(msi和zip格式)
- LightOJ - 1019 Brush (V)(floyd)
- ReactOS源码分析——内核加载器(一)
- Block的基本用法
- 反思自学中的误区
- 专访黄勇:Java在未来的很长一段时间仍是主流
- LightOJ - 1099 Not the Best(次短路)