传送门
文章目录
- 题意:
- 思路:
题意:
思路:
两点间最长边最小值一定是最小生成树上两点间的最大值,这个比较容易证,就不多说了。
知道这个结论后, 我们直接跑一个KruskalKruskalKruskal建树,让后在树上倍增求两点最小值就好了。
另一个比较好的方法就是建Kruskal重构树,让后最长边即为两点lcalcalca的权值。
Kruskal重构树:
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=100010,M=N*2,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,m,k;
int e[M],ne[M],w[M],h[N],idx;
int p[N],val[N];
int fa[N][20],depth[N],dis[N][20];
struct Edge
{int a,b,c;bool operator < (const Edge &W) const {return c<W.c;}
}edge[N];int find(int x)
{return x==p[x]? x:p[x]=find(p[x]);
}void add(int a,int b)
{e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}void dfs(int u,int f)
{depth[u]=depth[f]+1;fa[u][0]=f;for(int k=1;k<=18;k++) fa[u][k]=fa[fa[u][k-1]][k-1];for(int i=h[u];~i;i=ne[i]){int ver=e[i];if(ver==f) continue;dfs(ver,u);}
}int solve(int a,int b)
{int ans=0;if(depth[a]<depth[b]) swap(a,b);for(int k=18;k>=0;k--)if(depth[fa[a][k]]>=depth[b])a=fa[a][k];if(a==b) return a;for(int k=18;k>=0;k--)if(fa[a][k]!=fa[b][k])a=fa[a][k],b=fa[b][k];return fa[a][0];
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);memset(h,-1,sizeof(h));scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=n*2;i++) p[i]=i;for(int i=1;i<=m;i++){int a,b,c; scanf("%d%d%d",&a,&b,&c);edge[i]={a,b,c};}sort(edge+1,edge+1+m);int tot=n;for(int i=1;i<=m;i++){int a=edge[i].a,b=edge[i].b,c=edge[i].c;int pa=find(a),pb=find(b);if(pa==pb) continue;++tot; p[pa]=tot; p[pb]=tot; val[tot]=c;add(pa,tot); add(tot,pa);add(pb,tot); add(tot,pb);}dfs(tot,0);while(k--){int a,b; scanf("%d%d",&a,&b);printf("%d\n",val[solve(a,b)]);}return 0;
}
/**/
倍增:
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=100010,M=N*2,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,m,k;
int e[M],ne[M],w[M],h[N],idx;
int p[N];
int fa[N][20],depth[N],dis[N][20];
struct Edge
{int a,b,c;bool operator < (const Edge &W) const {return c<W.c;}
}edge[N];int find(int x)
{return x==p[x]? x:p[x]=find(p[x]);
}void add(int a,int b,int c)
{e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}void dfs(int u,int f,int c)
{depth[u]=depth[f]+1;fa[u][0]=f;for(int k=1;k<=18;k++) fa[u][k]=fa[fa[u][k-1]][k-1];dis[u][0]=c;for(int k=1;k<=18;k++) dis[u][k]=max(dis[u][k-1],dis[fa[u][k-1]][k-1]);for(int i=h[u];~i;i=ne[i]){int ver=e[i];if(ver==f) continue;dfs(ver,u,w[i]);}
}int solve(int a,int b)
{int ans=0;if(depth[a]<depth[b]) swap(a,b);for(int k=18;k>=0;k--)if(depth[fa[a][k]]>=depth[b])ans=max(ans,dis[a][k]),a=fa[a][k];if(a==b) return ans;for(int k=18;k>=0;k--)if(fa[a][k]!=fa[b][k]){ans=max(ans,dis[a][k]);ans=max(ans,dis[b][k]);a=fa[a][k];b=fa[b][k];}ans=max(ans,dis[a][0]);ans=max(ans,dis[b][0]);return ans;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);memset(h,-1,sizeof(h));scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=n;i++) p[i]=i;for(int i=1;i<=m;i++){int a,b,c; scanf("%d%d%d",&a,&b,&c);edge[i]={a,b,c};}sort(edge+1,edge+1+m);for(int i=1;i<=m;i++){int a=edge[i].a,b=edge[i].b,c=edge[i].c;int pa=find(a),pb=find(b);if(pa==pb) continue;p[pa]=pb; add(a,b,c); add(b,a,c);}dfs(1,0,0);while(k--){int a,b; scanf("%d%d",&a,&b);printf("%d\n",solve(a,b));}return 0;
}
/**/