# bzoj4331 JSOI2012 越狱老虎桥

9 篇文章 0 订阅
1 篇文章 0 订阅

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 500010

const int inf=1e7;
struct node
{
int x,y,c,next;bool mk;
}a[maxn*2],tr[maxn*2];
int len,first[maxn],ln,fir[maxn];
int mymin(int x,int y){return (x<y)?x:y;}
int mymax(int x,int y){return (x>y)?x:y;}
void ins(int x,int y,int c)
{
len++;a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=first[x];first[x]=len;a[len].mk=1;
}
int belong[maxn],cnt;
int dfn[maxn],low[maxn],sta[maxn],id,tp;
void tarjan(int x)
{
dfn[x]=low[x]=++id;
sta[++tp]=x;
for (int k=first[x];k!=-1;k=a[k].next) if (a[k].mk)
{
int y=a[k].y;
// if (y==fa) continue;
if (k&1) a[k].mk=a[k+1].mk=0;
else a[k].mk=a[k-1].mk=0;
if (dfn[y]==-1)
{
tarjan(y);
low[x]=mymin(low[x],low[y]);
if (low[y]>dfn[x])
{
cnt++;int z;
do
{
z=sta[tp--];
belong[z]=cnt;
}while (z!=y);
}
}else low[x]=mymin(low[x],dfn[y]);
}
}
struct edge{int c,id;}ed[maxn*2];int num;
bool cmp(edge x,edge y) {return x.c<y.c;}
void INS(int x,int y,int c)
{
ln++;tr[ln].x=x;tr[ln].y=y;tr[ln].c=c;
tr[ln].next=fir[x];fir[x]=ln;
}
int top[maxn],dep[maxn],siz[maxn],son[maxn],fa[maxn];
void dfs(int x)
{
siz[x]=son[x]=0;
for (int k=fir[x];k!=-1;k=tr[k].next)
{
int y=tr[k].y;
if (y==fa[x]) continue;
fa[y]=x;dep[y]=dep[x]+1;
dfs(y);
if (siz[y]>siz[son[x]]) son[x]=y;
siz[x]+=siz[y];
}
siz[x]+=1;
}
void dfs2(int x,int ttp)
{
top[x]=ttp;
if (son[x]!=0) dfs2(son[x],ttp);
for (int k=fir[x];k!=-1;k=tr[k].next)
{
int y=tr[k].y;
if (y!=fa[x] && y!=son[x]) dfs2(y,y);
}
}
int lca(int x,int y)
{
int tpx=top[x],tpy=top[y];
while (tpx!=tpy)
{
if (dep[tpx]>dep[tpy])
{
swap(tpx,tpy);
swap(x,y);
}
y=fa[top[y]];tpy=top[y];
}
if (dep[x]>dep[y]) return y;
return x;
}
bool check(int &lx,int &ly,int xx,int yy)
{
int la=lca(lx,ly);
int lc1=lca(lx,xx),lc2=lca(ly,xx);

if (lc1==lx && lc2==ly) {lx=la,ly=yy;return true;}
if (la!=xx && lca(la,xx)==xx && (la==lx || la==ly)) {lx=xx,ly=(dep[lx]>dep[ly])?lx:ly;return true;}
if (lc1==lc2 && (la==lx || la==ly)) {lx=(dep[lx]>dep[ly])?lx:ly,ly=yy;return true;}
return false;
}
int main()
{
//freopen("escape.in","r",stdin);
//freopen("escape.out","w",stdout);
int n,m,i,x,y,c;
scanf("%d%d",&n,&m);
len=0;memset(first,-1,sizeof(first));
for (i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}

tp=id=0;
for (i=1;i<=n;i++) dfn[i]=-1;
cnt=0;tarjan(1);num=0;cnt++;
while (tp) belong[sta[tp--]]=cnt;

ln=0;memset(fir,-1,sizeof(fir));
for (i=1;i<=len;i+=2)
if (belong[a[i].x]!=belong[a[i].y])
{
INS(belong[a[i].x],belong[a[i].y],a[i].c);
ed[++num].c=a[i].c;ed[num].id=ln;
INS(belong[a[i].y],belong[a[i].x],a[i].c);
}

fa[1]=0;dep[1]=0;dfs(1);dfs2(1,1);
sort(ed+1,ed+1+num,cmp);

int ans=-1;
int lx=tr[ed[1].id].x,ly=tr[ed[1].id].y;
for (i=2;i<=num;i++)
{
int xx=tr[ed[i].id].x,yy=tr[ed[i].id].y;
if (dep[xx]>dep[yy]) swap(xx,yy);
if (check(lx,ly,xx,yy)) continue;
ans=ed[i].c;
break;
}
printf("%d\n",ans);
return 0;
}

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

06-02 383
06-21 136
06-25 83
12-30 504
03-08 629
12-16 24
09-18 362
10-13 201
10-11 39
08-30 59
06-24 1504
11-07 477
02-12 362
03-24 832