POJ 3177 Redundant Paths(边双连通分量+tarjan)
来源:互联网 发布:mysql怎么进入数据库 编辑:程序博客网 时间:2024/05/29 18:55
题目链接:http://poj.org/problem?id=3177
题目大意:给你一个连通图,问你最少添加几条边能组成一个边双连通图,有重边
思路:我们将所有的双连通块看成一个点,因为他们是连通的且不成一个环,利用tarjan缩点后就可以看做一棵树
这样的树至少需要加多少条边就能构成一个双连通图呢,我们只需要将叶子节点连起来即可,因为是无向图,所以
度为1的就是叶节点而不是度为0,这样我们要添加的边数就为(叶子结点总数+1)/2,加1是因为叶子结点可能为
奇数的,而且要注意tarjan在无向图与有向图的不同写法
上代码:
#include<iostream>#include<cmath>#include<cstring>#include<string>#include<cstdio>#include<queue>#include<stack>#include<vector>#include<algorithm>using namespace std;#define inf 0x3f3f3f3f#define mod 1e9+7#define ll long long#define maxn 5000+10vector<int>G[maxn];bool visit[maxn],falg[maxn][maxn];int dfn[maxn], low[maxn], cnt[maxn];int n, m,index;void tarjan(int u, int father){dfn[u] = low[u] = ++index;visit[u] = true;for (int i = 0; i < G[u].size(); i++){int k = G[u][i];if (!visit[k]){tarjan(k, u);low[u] = min(low[u], low[k]);}else if (visit[k]&&(k != father))//自己不能回到父节点,必须透过其他路径回去low[u] = min(low[u], dfn[k]);//为什么是dfn[v],因为如果low[v]<dfn[v]就说明//v已经是别的双连通分支的点了,不能再去动用他了}}void solve(){memset(visit, false, sizeof(visit));memset(low, 0, sizeof(low));memset(dfn, 0, sizeof(dfn));memset(cnt, 0, sizeof(cnt));index = 0;tarjan(1, 1);//如果是不连通的图,加个for(1 to n)for (int i = 1; i <= n; i++){for (int j = 0; j < G[i].size(); j++){if (low[G[i][j]] != low[i])cnt[low[i]]++;//计算缩点后每个点的度}}int sum = 0;for (int i = 1; i <= n; i++)if (cnt[i] == 1)//度为1为叶节点sum++;printf("%d\n", (sum + 1) / 2);//为什么是(sum+1)/2呢,画图好理解for (int i = 1; i <= n; i++)G[i].clear();}int main(){//freopen("Text.txt", "r", stdin);int u, v;while (scanf("%d%d", &n, &m) != EOF){memset(falg, false, sizeof(falg));while (m--){scanf("%d%d", &u, &v);if(!falg[u][v])//去重边{ G[u].push_back(v);G[v].push_back(u);falg[u][v] = true;falg[v][u] = true;}}solve();}return 0;}
阅读全文
0 0
- POJ-3177 Redundant Paths (边双连通分量[Tarjan])
- poj 3177 Redundant Paths(Tarjan,边双连通分量)
- POJ 3177 Redundant Paths(边双连通分量+tarjan)
- poj 3177 Redundant Paths(双连通分量)
- 【POJ 3177】Redundant Paths(Tarjan求桥、边双连通分量)
- POJ 3177 Redundant Paths 边的双连通分量
- [边双连通分量] poj 3177 Redundant Paths
- POJ 3177 Redundant Paths(边双连通分量,3级)
- POJ 3177 Redundant Paths / 边双连通分量
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- poj 3177 Redundant Paths 边双连通分量+缩点
- poj 3177 Redundant Paths 边双连通分量
- POJ 3177 Redundant Paths 边双连通分量+缩点
- POJ 3177 Redundant Paths(边双连通分量)
- poj 3177 Redundant Paths 边双连通分量
- POJ 3177Redundant Paths 边双连通分量
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- POJ 3177 Redundant Paths <边双连通分量>
- 【架构】关于RabbitMQ
- jQuery课程表
- 24:单词的长度
- 关于EnumerateObjectsUsingBlock和for-in之间的较量
- keil55.24打开一个工程出现the project references devices,files or libraries that are not installed
- POJ 3177 Redundant Paths(边双连通分量+tarjan)
- linux部分命令
- HttpClient解析数据
- Mac下通过Homebrew安装redis
- 1133. Splitting A Linked List (25)
- jQuery事件
- Maven生成可以直接运行的jar包的多种方式
- vs创建个文件夹,生成解决方案时,bin文件下没有
- leetcode 15 : 3sum