【HDOJ4612】【双连通分量缩点+找树的直径】

时间:2022-03-17 22:17:05

http://acm.hdu.edu.cn/showproblem.php?pid=4612

Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 8309    Accepted Submission(s): 1905

Problem Description
  N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
  If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
  Note that there could be more than one channel between two planets.
 
Input
  The input contains multiple cases.
  Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
  (2<=N<=200000, 1<=M<=1000000)
  Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
  A line with two integers '0' terminates the input.
 
Output
  For each case, output the minimal number of bridges after building a new channel in a line.
 
Sample Input
4 4
1 2
1 3
1 4
2 3
0 0
 
Sample Output
题目大意:给一个连通图,求加一条边之后最少还有多少个桥。
题目分析:首先看到要求是"改造桥",则先进行缩点,由于是改造桥,所以进行边双连通的缩点,然后求树的直径【树上桥最多的一条路】,则最后结果ans=原来桥数-直径。
题解:Tarjan缩点求桥数+两次bfs求树的直径
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
queue<int>pq;
struct edge {
int fr;
int to;
int next;
}e[], e2[];
int cnt, scc_cnt, ccnt, tim;
int head[], head2[], dfn[], low[], stk[], col[],d[], tp,dis,node;
bool in_stk[], vis[];
int ans = ;
void dfs(int x, int fa) {
d[x] = dis++;
for (int i = head2[x]; i != -; i = e2[i].next) {
int v = e2[i].to;
if (v == fa)continue;
if (!vis[v]) {
vis[v] = ;
dfs(v, x);
}
}
return;
}
void bfs(int x) {
while (!pq.empty())pq.pop();
pq.push(x);
memset(d, , sizeof(d));
memset(vis, , sizeof(vis));
while (!pq.empty()) {
int s = pq.front(); pq.pop();
for (int i = head2[s]; i != -; i = e2[i].next) {
int v = e2[i].to;
if (!vis[v]) {
vis[v] = ;
d[v] = d[s] + ;
pq.push(v);
if (d[v] > ans) {
node = v;
ans = d[v];
}
}
}
}
}
void Tarjan(int u, int fa, int id) {
dfn[u] = low[u] = ++tim;
in_stk[u] = ;
stk[tp++] = u;
for (int i = head[u]; i != -; i = e[i].next) {
int v = e[i].to;
if (!dfn[v]) {
Tarjan(v, u, i);
if (low[v] < low[u])low[u] = low[v];
}
else if (in_stk[v] && ((i ^ ) != id)) {
if (dfn[v] < low[u])low[u] = dfn[v];//这里使用dfn[v]和low[v]都可以
}
}
if (dfn[u] == low[u]) {
scc_cnt++;
do {
tp--;
int tt = stk[tp];
col[tt] = scc_cnt;
in_stk[tt] = ;
} while (stk[tp] != u);
}
}
void add(int x, int y) {
e[cnt].fr = x;
e[cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt++;
}
void add2(int x, int y) {
e2[ccnt].fr = x;
e2[ccnt].to = y;
e2[ccnt].next = head2[x];
head2[x] = ccnt++;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
while (n || m) {
cnt = ;
tp = ;
scc_cnt = ;
ccnt = ;
ans = ;
tim = ;
memset(vis, , sizeof(vis));
memset(in_stk, , sizeof(in_stk));
memset(col, , sizeof(col));
memset(head, -, sizeof(head));
memset(head2, -, sizeof(head2));
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
}
for (int i = ; i <= n; i++) {
if (!dfn[i]) {
Tarjan(i, -, -);
}
}
for (int i = ; i < cnt; i += ) {
if (col[e[i].fr] != col[e[i].to]) {
add2(col[e[i].fr], col[e[i].to]);
add2(col[e[i].to], col[e[i].fr]);
}
}
bfs();
bfs(node);
cout << ccnt / - ans << endl;
scanf("%d%d", &n, &m);
}
return ;
}