POJ-3177(tarjan缩点)
来源:互联网 发布:杭州开创网络 编辑:程序博客网 时间:2024/05/28 03:02
题目大意:至少加多少条边使得每两个点都有至少两种走法可以走
题解思路:利用tarjan缩点后找有几点只有一条边跟这个点跟它连接然后用最少的边把这些点都变成有两条边就ok.
如果不缩点那么以下样例就不用加边了
6 7
1 2
1 3
2 3
3 4
4 5
4 6
5 6
注意记得判断重边
题目链接
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<stack>using namespace std;const int mx = 1e4+5;struct node{ int next; int v;}E[mx<<3];stack<int>s;int n,m;int pre[mx],low[mx],head[mx];int deg[mx],sccno[mx];int dfn,tot,bri,cnt;void init(){ memset(pre,0,sizeof(pre)); memset(head,0,sizeof(head)); memset(deg,0,sizeof(deg)); dfn = tot = bri = cnt = 0;}void add(int u,int v){ E[++tot].v = v; E[tot].next = head[u]; head[u] = tot;}void dfs(int u,int fa){ pre[u] = low[u] = ++dfn; s.push(u); int flag = 0; for(int i = head[u]; i ; i = E[i].next){ int v = E[i].v; if(!pre[v]){ dfs(v,u); low[u] = min(low[v],low[u]); } else if(v != fa || flag) low[u] = min(low[v],low[u]); if(v == fa) flag = 1; } if(low[u] == pre[u]){ cnt++; while(1){ int v = s.top(); s.pop(); sccno[v] = cnt; if(v == u) break; } }}void solve(){ dfs(1,-1); for(int u = 1; u <= n; u++){ for(int i = head[u]; i ; i = E[i].next){ int v = E[i].v; if(sccno[v] != sccno[u]){ deg[sccno[v]]++; } } } for(int i = 1; i <= cnt; i++) if(deg[i] == 1) bri++; printf("%d\n",(bri+1)/2);}int main(){ while(~scanf("%d%d",&n,&m)){ init(); for(int i = 1; i <= m; i++){ int u,v; scanf("%d%d",&u,&v); add(u,v); add(v,u); } solve(); } return 0;}
阅读全文
0 0
- POJ-3177(tarjan缩点)
- poj Popular Cows(tarjan +缩点)
- POJ-1236(tarjan缩点)
- POJ 1236(tarjan+缩点)
- poj 2186 (tarjan 缩点)
- POJ 3352 Road Construction POJ 3177Redundant Paths(Tarjan缩点)
- POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)
- [POJ 3177]Redundant Paths[边双连通][Tarjan][缩点]
- poj 2553 (tarjan求强连通分量+缩点)
- (tarjan缩点)poj 2186 Popular Cows
- poj 2186 Popular Cows (tarjan缩点)
- poj 1236 Network of Schools(tarjan 缩点)
- POJ 3648 Wedding (Tarjan + 缩点 + 拓扑排序)
- poj 1236 Network of Schools 1 (tarjan 缩点)
- POJ 2186 Popular Cows (Tarjan + 缩点)
- POJ 2186 Popular Cows(tarjan缩点)
- POJ 1236 Network of Schools(tarjan缩点)
- 对Tarjan的理解(详) poj 2186 缩点+Tarjan
- python--open/文件操作
- Linux中的各种锁
- 128 C语言实现文件复制功能(包括文本文件和二进制文件)
- php与mysql
- App统计指标定义
- POJ-3177(tarjan缩点)
- 129 C语言FILE结构体以及缓冲区深入探讨
- 原生js实现ajax的一个简单demo
- CSS的sprite图片处理
- 左旋转字符串
- 内核中的str函数
- 130 C语言获取文件大小(长度)
- Spring boot读取配置文件总结
- Linux网络状态工具ss命令使用详解