POJ - 3177 Redundant Paths 说说连通分量吧
----我想说说双联通分量还有割点和桥
1.割点(一个点,如果没有这一个点,图就会变得不连通)
2.桥(一条边,断开这条边就会让图不连通)
3.点双连通(没割点的图)
4.边双连通(没桥的图)
5.割点之间不一定有桥!!!
6.桥两端不一定是割点!!!
对了,关于tarjan我还想再说说
就像下图,圈住的是点双连通分量和边双连通分量
本题要把有桥图,变成边双连通图,问你要加几条边
下图说了具体做法
这就是我找到的关于双联通分量的见解了
代码奉上
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
#define maxn 5010
#define INF 10000
using namespace std; int low[maxn], dfn[maxn], clor[maxn], df, clr;
vector<int>G[maxn];
void insert(int be, int en) {
G[be].push_back(en);
}
int n, m;
stack<int>s;
int de[maxn];
void tarjan(int x,int fa) {
low[x] = dfn[x] = ++df;
s.push(x); for (int i = 0; i < G[x].size(); i++) {
int p = G[x][i];
if (p == fa) continue;
if (!dfn[p]) {
tarjan(p, x);
low[x] = min(low[x], low[p]);
}
else {
low[x] = min(low[x], dfn[p]);
}
}
if (low[x] == dfn[x]) {
clr++;
while (1) {
int a = s.top();
s.pop();
clor[a] = clr;
if (a == x) break;
}
}
}
map<long long, int>ins;
int main() {
int be, en;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &be, &en);
long long an = be * INF + en;
long long cc = en * INF + be;
if (ins[an] == 0 || ins[cc] == 0) {
insert(be, en);
insert(en, be);
ins[cc] = 1;
ins[an] = 1;
} }
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i, -1);
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < G[i].size(); j++) {
int p = G[i][j];
if (clor[i] != clor[p]) {
de[clor[i]]++;
de[clor[p]]++;
}
}
}
int cnt = 0;
for (int i = 1; i <= clr; i++) {
if (de[i] == 2) cnt++;//因为一个边有两个端点,每个边都被算计了两次!
}
printf("%d\n", (cnt + 1) / 2);
return 0;
}
POJ - 3177 Redundant Paths 说说连通分量吧的更多相关文章
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- [双连通分量] POJ 3177 Redundant Paths
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13712 Accepted: 5821 ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- POJ 3177——Redundant Paths——————【加边形成边双连通图】
Redundant Paths Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3177 Redundant Paths
题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- POJ - 3177 Redundant Paths(边双连通分支)(模板)
1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...
随机推荐
- C++第一次作业(循环语句的使用)
一.知识点 循环结构 二.教学目的 掌握while和do...while循环语句在C++中的写法 三.教学内容 1.while语句 (1)执行顺序:先判断表达式(循环控制条件)的值,若表达式的值为tr ...
- LeetCode Weekly Contest 6
leetcode现在每周末举办比赛,这是今天上午参加的比赛的题解.题目难度不算大,两个easy,一个medium,一个hard.hard题之前接触过,所以做得比较顺利. 1. Sum of Left ...
- @codechef - BUYLAND@ Buying Land
目录 @desription@ @solution@ @accepted code@ @details@ @desription@ 给定一个 R * C 表示高度的矩阵 A,另一个 H * W 的矩阵 ...
- L05 Laravel 教程 - 电商实战
https://laravel-china.org/courses/laravel-shop https://laravel-china.org/topics/13206/laravel-shop-c ...
- 在线url网址编码、解码
>>在线url网址编码.解码<<
- 独家 | TensorFlow 2.0将把Eager Execution变为默认执行模式,你该转向动态计算图了
机器之心报道 作者:邱陆陆 8 月中旬,谷歌大脑成员 Martin Wicke 在一封公开邮件中宣布,新版本开源框架——TensorFlow 2.0 预览版将在年底之前正式发布.今日,在上海谷歌开发者 ...
- error LNK2001: unresolved external symbol __imp___rt_sdiv
这个问题搞了我 5 天(包括双休日), 我一定要记录下来. 问题描述 用 Visual Studio 2008 编译 WinCE 7 平台的应用程序,编译没问题,链接时出现了一堆 Link error ...
- Pytorch Bi-LSTM + CRF 代码详解
久闻LSTM + CRF的效果强大,最近在看Pytorch官网文档的时候,看到了这段代码,前前后后查了很多资料,终于把代码弄懂了.我希望在后来人看这段代码的时候,直接就看我的博客就能完全弄懂这段代码. ...
- thinkphp3.2.3中设置路由,优化url
需求: 访问这个目录的时候,http://xx.com/p-412313要重定向到(暂且这么叫)http://xx.com/Home/Blog/index/id/412313 就是看着好看 我的应用目 ...
- PythonWeb框架之Django
Django 简介: Django是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式,即模型Model,模板Template和视图View.此框架设计模式借鉴了MVC框架的思 ...