# BZOJ3732: Network

BZOJ3732: Network

## Output

对每个询问，输出最长的边最小值是多少。

6 6 8
1 2 5
2 3 4
3 4 3
1 4 8
2 5 7
4 6 2
1 2
1 3
1 4
2 3
2 4
5 1
6 2
6 1

5
5
5
4
4
7
4
5

## HINT

1 <= N <= 15,000
1 <= M <= 30,000
1 <= d_j <= 1,000,000,000
1 <= K <= 15,000

#include<iostream>
#include<algorithm>
#include<cstdio>
#define LSON rt<<1
#define RSON rt<<1|1
#define DATA(x) b[x].data
#define LSIDE(x) b[x].l
#define RSIDE(x) b[x].r
#define MAXN 20010
using namespace std;
int n,m,q,c=1,d=1;
struct Tree{
int next,to;
}a[MAXN<<1];
struct Segment_Tree{
int data,l,r;
}b[MAXN<<2];
struct Edge{
int u,v,w;
bool used;
}g[MAXN<<1];
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
inline bool cmp(const Edge &p,const Edge &q){return p.w<q.w;}
int find(int x){return father[x]==x?x:father[x]=find(father[x]);}
inline void uniun(int x,int y){x=find(x);y=find(y);if(x!=y)father[y]=x;}
}
void dfs1(int rt){
son[rt]=0;size[rt]=1;
int will=a[i].to;
if(!deep[will]){
deep[will]=deep[rt]+1;
fa[will]=rt;
dfs1(will);
size[rt]+=size[will];
if(size[son[rt]]<size[will])son[rt]=will;
}
}
}
void dfs2(int rt,int f){
id[rt]=d++;top[rt]=f;
if(son[rt])dfs2(son[rt],f);
int will=a[i].to;
if(will!=fa[rt]&&will!=son[rt])dfs2(will,will);
}
}
inline void pushup(int rt){
DATA(rt)=max(DATA(LSON),DATA(RSON));
}
void buildtree(int l,int r,int rt){
LSIDE(rt)=l;RSIDE(rt)=r;
if(l==r){
DATA(rt)=0;
return;
}
int mid=l+r>>1;
buildtree(l,mid,LSON);
buildtree(mid+1,r,RSON);
pushup(rt);
}
void update(int l,int r,int c,int rt){
if(l<=LSIDE(rt)&&RSIDE(rt)<=r){
DATA(rt)=c;
return;
}
int mid=LSIDE(rt)+RSIDE(rt)>>1;
if(l<=mid)update(l,r,c,LSON);
if(mid<r)update(l,r,c,RSON);
pushup(rt);
}
int query(int l,int r,int rt){
int ans=0;
if(l<=LSIDE(rt)&&RSIDE(rt)<=r)return DATA(rt);
int mid=LSIDE(rt)+RSIDE(rt)>>1;
if(l<=mid)ans=max(ans,query(l,r,LSON));
if(mid<r)ans=max(ans,query(l,r,RSON));
return ans;
}
void kruskal(){
int s=0;
for(int i=1;i<=m&&s<n-1;i++)
if(find(g[i].u)!=find(g[i].v)){
uniun(g[i].u,g[i].v);
g[i].used=true;
s++;
}
for(int i=1;i<=n;i++)
if(!deep[i]){
deep[i]=1;
dfs1(i);
dfs2(i,i);
}
buildtree(1,n,1);
for(int i=1;i<=m;i++)
if(g[i].used){
if(deep[g[i].u]>deep[g[i].v])swap(g[i].u,g[i].v);
update(id[g[i].v],id[g[i].v],g[i].w,1);
}
}
void solve(int x,int y){
int s=0;
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
s=max(s,query(id[top[x]],id[x],1));
x=fa[top[x]];
}
if(deep[x]>deep[y])swap(x,y);
if(x!=y)s=max(s,query(id[x]+1,id[y],1));
printf("%d\n",s);
}
void work(){
int x,y;
while(q--){
solve(x,y);
}
}
void init(){
for(int i=1;i<=m;i++){
g[i].used=false;
}
for(int i=1;i<=n;i++)father[i]=i;
sort(g+1,g+m+1,cmp);
kruskal();
}
int main(){
init();
work();
return 0;
}


posted @ 2018-08-21 00:08  符拉迪沃斯托克  阅读(108)  评论(0编辑  收藏  举报
Live2D