LightOJ - 1034 Hit the Light Switches(强连通)
来源:互联网 发布:网络维护与故障排解 编辑:程序博客网 时间:2024/06/11 01:16
题目大意:有N盏灯,灯满足u, v要求,表示u亮了,v也就亮了
问需要开几盏灯,才能让所有的灯都亮了
解题思路:强连通块内的灯能相互作用。求出强连通块,然后缩点连边,找出入度为0的点,入度为0的点,就是要按的灯的数量
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 10010;const int M = 100010;struct Edge{ int u, v, next; Edge() {} Edge(int u, int v, int next): u(u), v(v), next(next) {}}E[M];int head[N], sccno[N], pre[N], lowlink[N], Stack[N], in[N];int n, m, scc_cnt, dfs_clock, top, cas = 1;void init() { memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); E[i] = Edge(u, v, head[u]); head[u] = i; }}void dfs(int u, int fa) { lowlink[u] = pre[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(pre[v], lowlink[u]); } } int x; if (lowlink[u] == pre[u]) { scc_cnt++; while (1) { x = Stack[top--]; sccno[x] = scc_cnt; if (x == u) break; } }}void solve() { memset(pre, 0, sizeof(pre)); memset(sccno, 0, sizeof(sccno)); dfs_clock = top = scc_cnt = 0; for (int i = 1; i <= n; i++) { if (!pre[i]) dfs(i, -1); } for (int i = 1; i <= scc_cnt; i++) in[i] = 0; for (int i = 0; i < m; i++) { int u = sccno[E[i].u]; int v = sccno[E[i].v]; if (u == v) continue; in[v]++; } int ans = 0; for (int i = 1; i <= scc_cnt; i++) if (!in[i]) ans++; printf("Case %d: %d\n", cas++, ans);}int main() { int test; scanf("%d", &test); while (test--) { init(); solve(); } return 0;}
0 0
- LightOJ - 1034 Hit the Light Switches(强连通)
- LightOJ 1034 - Hit the Light Switches【强连通最小点基】
- [最小点基]LightOJ 1034 - Hit the Light Switches
- lightoj1034 Hit the Light Switches
- lightoj 1034【强连通+缩点】
- LightOJ - 1210 Efficient Traffic System(强连通)
- LightOJ - 1417 Forwarding Emails(强连通+dfs)
- [强连通分量]LightOJ 1210 - Efficient Traffic System
- LightOJ 1210 - Efficient Traffic System【强连通图】
- lightoj 1063 - Ant Hills(强连通求割点)
- uva11324 The Largest Clique --- 强连通+dp
- 【POJ】3180 The Cow Prom 强连通
- poj3180 The Cow Prom(强连通)
- 【强连通】
- poj 2553 The Bottom of a Graph(强连通分量)
- POJ2553 The Bottom of a Graph 强连通 tarjan
- hdu 3861 The King’s Problem 强连通+二分匹配
- The Bottom of a Graph 强连通分量加缩点
- 111
- iOS开发系列--通知与消息机制
- Redis 代理服务Twemproxy
- CodeProject上的两个简单绘图程序
- min函数栈解题思路分析
- LightOJ - 1034 Hit the Light Switches(强连通)
- ubuntu 安装nginx和php
- poj2115
- Android View的属性
- 谷歌邮件智能回复系统:基于循环神经网络构建
- UVa11809 - Floating-Point Numbers
- 排序算法(七):归并排序
- Error -1Cause: org/gradle/api/publication/maven/internal Android studio配置android-maven-gradle-plugin
- LightOJ - 1210 Efficient Traffic System(强连通)