1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=2000+5,maxp=2e5+5;
const int INF=0x3f3f3f3f;
template<typename Tp>
class graph{
public:
struct edge{
int to;edge *next;Tp len;
edge(int to=0,edge *next=0,Tp len=0):to(to),next(next),len(len){}
};
int dep[maxp],fa[maxp][25],n,edgtot;
edge edges[maxp*2],*now[maxp];
Tp f[maxp][25];
void insert(int u,int v,Tp w){
edges[++edgtot]=edge(v,now[u],w),now[u]=edges+edgtot;
}
void dfs(int u){
dep[u]=dep[fa[u][0]]+1;
int k=ceil(log2(dep[u]));
for(int i=1;i<=k;i++){
fa[u][i]=fa[fa[u][i-1]][i-1];
f[u][i]=max(f[u][i-1],f[fa[u][i-1]][i-1]);
}
for(edge *e=now[u];e;e=e->next){
int v=e->to;
if(v!=fa[u][0]){fa[v][0]=u,f[v][0]=e->len;dfs(v);}
}
}
Tp lca(int u,int v){
if(dep[u]<dep[v])swap(u,v);
int s=ceil(log2(n)),k=dep[u]-dep[v];
Tp ans=0;
for(int i=0;i<=s;i++)if(k&1<<i)
ans=max(ans,f[u][i]),u=fa[u][i];
if(u==v)return ans;
s=ceil(log2(dep[u]));
for(int i=s;i>=0;i--)if(fa[u][i]!=fa[v][i]){
ans=max(ans,max(f[u][i],f[v][i]));
u=fa[u][i],v=fa[v][i];
}
return max(ans,max(f[u][0],f[v][0]));
}
};
graph<int> g;
struct node{
int x,y;
node(int x=0,int y=0):x(x),y(y){}
};
struct edge{
int a,b,len;
edge(int a=0,int b=0,int len=0):a(a),b(b),len(len){}
bool operator<(edge rhs)const{return len>rhs.len;}
};
const int dx[]={-1,1,0,0},dy[]={0,大专栏
|