4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

时间:2023-12-20 08:16:56

问加一条边,最少可以剩下几个桥。

先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥。

本题要处理重边的情况。

如果本来就两条重边,不能算是桥。

还会爆栈,只能C++交,手动加栈了

别人都是用的双连通分量,我直接无向图改成有向图搞得强连通水过。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stack> using namespace std;
const int maxn = ;
bool vis[];
struct edge
{
int v,next;
edge()
{
next = -;
}
}edges[maxn*-];
struct ed
{
int u,v;
}e[maxn*-];
int dfn[],low[],belong[];
bool inst[];
int g[maxn];
vector<int>ng[maxn]; stack<int>st;
int bcnt,cnt,time; void init(int n)
{
int i;
for(i =;i <= n;i++)
g[i] = -;
time = ;bcnt = cnt = ;
return ;
}
void addedge(int u,int v,int val)
{
struct edge o;
edges[cnt].v = v;
edges[cnt].next = g[u];
g[u] = cnt;
cnt++; return ;
}
void tarjan(int i)
{
int j;
dfn[i] = low[i] = ++time;
inst[i] = ;
st.push(i); for(j = g[i];j != -;j = edges[j].next)
{
if(vis[j]) continue;
vis[j] = vis[j^] = ;
int v;
v = edges[j].v;
if(!dfn[v])
{
tarjan(v);
low[i] = min(low[i],low[v]);
}
else if(inst[v])
low[i] = min(low[i],dfn[v]);
}
int k;
if(dfn[i] == low[i])
{ bcnt++;
do
{
k = st.top();
st.pop();
inst[k] = ;
belong[k] = bcnt; }
while(k != i);
} }
void tarjans(int n)
{
int i;
bcnt = time = ;
while(!st.empty())st.pop();
memset(dfn,,sizeof(dfn)); memset(inst,,sizeof(inst));
memset(belong,,sizeof(belong));
for(i = ;i <= n;i++)
if(!dfn[i])tarjan(i);
}
struct node
{
int s,point;
}; struct node bfs(int s)
{
int i;
for(i = ;i <= bcnt;i++)
vis[i] = ;
queue <struct node>q;
struct node p;
p.s = ;p.point = s;
q.push(p);
vis[s] = ;
struct node max;
max.s = ; while(!q.empty())
{
struct node now,temp;
now = q.front();
q.pop(); for(i = ;i < ng[now.point].size();i++)
{
int v = ng[now.point][i];
temp.s = now.s+;
temp.point = v;
if(!vis[v])
{
vis[v] = ;
// cout<<v<<"***"<<endl; if(max.s < temp.s)
max = temp;
q.push(temp);
}
}
}
return max;
}
int main()
{
int n,m;
//freopen("in.txt","r",stdin);
while(scanf("%d %d",&n,&m)&&(n||m))
{
int a,b,i;
init(n);
memset(vis,,sizeof(vis));
for(i = ;i < m;i++)
{
scanf("%d %d",&e[i].u,&e[i].v);
addedge(e[i].u,e[i].v,);
addedge(e[i].v,e[i].u,);
}
tarjans(n); for(i = ;i <= n;i++)
{
ng[i].clear();
} for(i = ;i < m;i++)
{
if(belong[e[i].u] == belong[e[i].v])
continue;
ng[belong[e[i].u]].push_back(belong[e[i].v]);
ng[belong[e[i].v]].push_back(belong[e[i].u]);
}
struct node max;
max = bfs();
max = bfs(max.point);
printf("%d\n",bcnt-max.s-);
}
return ;
}