POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths

POJ 3352 Road Construction

题目链接

题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图

思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了)

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1005;
const int M = 20005;

int n, m;
struct Edge {
	int u, v, id;
	bool iscut;
	Edge() {}
	Edge(int u, int v, int id) {
		this->u = u;
		this->v = v;
		this->id = id;
		this->iscut = false;
	}
} edge[M];

int first[N], next[M], en;

void add_edge(int u, int v, int id) {
	edge[en] = Edge(u, v, id);
	next[en] = first[u];
	first[u] = en++;
}

void init() {
	en = 0;
	memset(first, -1, sizeof(first));
}

int pre[N], dfn[N], dfs_clock, bccno[N], bccn;

void dfs_cut(int u, int id) {
	pre[u] = dfn[u] = ++dfs_clock;
	for (int i = first[u]; i + 1; i = next[i]) {
		if (edge[i].id == id) continue;
		int v = edge[i].v;
		if (!pre[v]) {
			dfs_cut(v, edge[i].id);
			dfn[u] = min(dfn[u], dfn[v]);
			if (dfn[v] > pre[u])
				edge[i].iscut = edge[i^1].iscut = true;
		} else dfn[u] = min(dfn[u], pre[v]);
	}
}

void find_cut() {
	dfs_clock = 0;
	memset(pre, 0, sizeof(pre));
	for (int i = 1; i <= n; i++)
		if (!pre[i]) dfs_cut(i, -1);
}

void dfs_bcc(int u) {
	bccno[u] = bccn;
	for (int i = first[u]; i + 1; i = next[i]) {
		if (edge[i].iscut) continue;
		int v = edge[i].v;
		if (bccno[v]) continue;
		dfs_bcc(v);
	}
}

void find_bcc() {
	bccn = 0;
	memset(bccno, 0, sizeof(bccno));
	for (int i = 1; i <= n; i++) {
		if (!bccno[i]) {
			bccn++;
			dfs_bcc(i);
		}
	}
}

int du[N];

int main() {
	while (~scanf("%d%d", &n, &m)) {
		int u, v;
		init();
		for (int i = 0; i < m; i++) {
			scanf("%d%d", &u, &v);
			add_edge(u, v, i);
			add_edge(v, u, i);
		}
		find_cut();
		find_bcc();
		memset(du, 0, sizeof(du));
		for (int i = 0; i < en; i += 2) {
			if (!edge[i].iscut) continue;
			int u = bccno[edge[i].u], v = bccno[edge[i].v];
			if (u == v) continue;
			du[u]++; du[v]++;
		}
		int cnt = 0;
		for (int i = 1; i <= bccn; i++)
			if (du[i] == 1) cnt++;
		printf("%d\n", (cnt + 1) / 2);
	}
	return 0;
}
时间: 2024-10-12 19:53:17

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)的相关文章

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点) ACM 题目地址: POJ 3352 Road Construction POJ 3177 Redundant Paths 题意: 问要添加几条边才能使所给无向图图变成边双连通图. 分析: 边连通度:使无向图G不连通的最少删边数量为其边连通度. 边双连通图:边连通度大于1的无向图. 首先缩点,让图变成一个DAG. 现在问题转化为:在树中至少添加多少条边能使图变

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re

poj 3177 Redundant Paths

Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forc

POJ 3177 Redundant Paths(Tarjan)

题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf,最后要添加的边的条数就是(leaf+1)/2 : 1 // 3177 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using

poj 3177 Redundant Paths (双联通)

/******************************************************* 题目:Redundant Paths (poj 2177) 链接:http://poj.org/problem?id=3177 算法:双联通+缩点 思路:先找出所有双联通分量,把这些分量缩成一个点 再找出所有度为一的点,用这些点数加一除2就可以了 ********************************************************/ #include<cs

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

POJ 3177 Redundant Paths (双连通)

题目地址:POJ 3177 找出各个双连通分量度数为1的点,然后作为叶子节点,那么ans=(叶子结点数+1)/2.需要注意的是有重边. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include

poj 3177 Redundant Paths(tarjan边双连通)

题目链接:http://poj.org/problem?id=3177 题意:求最少加几条边使得没对点都有至少两条路互通. 题解:边双连通顾名思义,可以先求一下连通块显然连通块里的点都是双连通的,然后就是各个连通块之间的问题. 也就是说只要求一下桥,然后某个连通块桥的个数位1的总数,结果就是(ans+1)/2.为什么是这个结果自行画图 理解一下,挺好理解的. #include <iostream> #include <cstring> #include <cstdio>

[双连通分量] POJ 3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the