[J]computer network tarjan边双联通分量+树的直径

https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031

【2012-2013 ACM Central Region of Russia Quarterfinal Programming Contest】【J】computer network

题意:

n个点,m条边,构成一个无向图,现在让你再任意连接两个点,使得整个图的割边最少。

1 ≤ n ≤ 10 000; 1≤ m ≤ 100 000; 1 ≤ xi, yi ≤ n; xi ≠ yi.

题解:
tarjan求边双联通分量,缩点,形成一棵树,求树的直径,然后把直径的两个端点连起来就好。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std; // const int maxN=100;
// int a[maxN+10];
const int N=,M=;
int dfn[N],low[N],first[N],bfir[N],v[N],c[N],s[N],id[];
int n,m,al,bl,sl,num,mx,cnt;
struct node{
int x,y,next,tmp;
}a[*M],b[*M]; int minn(int x,int y){return x<y ? x:y;} void ins(int x,int y)
{
al++;a[al].tmp=;
a[al].x=x;a[al].y=y;a[al].next=first[x];first[x]=al;
} void anins(int x,int y)
{
bl++;b[bl].tmp=;
b[bl].x=x;b[bl].y=y;b[bl].next=bfir[x];bfir[x]=bl;
} void tarjan(int x,int fa)
{
dfn[x]=low[x]=++num;
s[++sl]=x;
for(int i=first[x];i;i=a[i].next)
{
if(a[i].tmp) continue;
a[i%== ? i-:i+].tmp=;
int y=a[i].y;
if(!dfn[y])
{
tarjan(y,x);
low[x]=minn(low[x],low[y]);
}
else if(!c[y]) low[x]=minn(low[x],dfn[y]);
}
if(dfn[x]==low[x])
{
cnt++;
int z=;
while()
{
z=s[sl];sl--;
v[z]=;
c[z]=cnt;
if(z==x) break;
}
}
} void dfs(int x,int fa,int dep,int tmp)
{
if(dep>mx) mx=dep,id[tmp]=x;
for(int i=bfir[x];i;i=b[i].next)
{
int y=b[i].y;
if(y==fa) continue;
dfs(y,x,dep+,tmp);
}
} int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d%d",&n,&m);
al=;sl=;bl=;num=;cnt=;
memset(bfir,,sizeof(bfir));
memset(first,,sizeof(first));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(c,,sizeof(c));
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ins(x,y);ins(y,x);
}
for(int i=;i<=n;i++)
{
if(!dfn[i]) num=,tarjan(i,);
}
// for(int i=1;i<=n;i++) printf("c [ %d ] = %d\n",i,c[i]);
for(int i=;i<=*m;i++)
{
int x=a[i].x,y=a[i].y;
if(c[y]==c[x]) continue;
anins(c[x],c[y]);
// printf("%d -- > %d\n",c[x],c[y]);
}
mx=,dfs(,,,);
mx=,dfs(id[],,,);
for(int i=;i<=n;i++)
if(c[i]==id[]) {printf("%d ",i);break;}
for(int i=;i<=n;i++)
if(c[i]==id[]) {printf("%d\n",i);break;}
// printf("%d %d\n",id[0],id[1]);
return ;
}
上一篇:手机触屏触摸特效javascript-TouchSwipe(依赖于jquery库)中文说明


下一篇:2227 邮票--FUoj(链接表+树的直径)