P1967 [noip2013 improvement group] truck transportation (MST & tree multiplication)

wx6110fa547fd20 2021-08-10 07:01:09 阅读数:76

本文一共[544]字,预计阅读时长:1分钟~
p1967 noip2013 noip improvement group

P1967 [NOIP2013 Improvement group ] Truck transportation (MST& On the tree )

The question

Given n n n A little bit m m m Undirected graph of strip and edge , Each side has its own right , Given q q q A asked , Each inquiry includes a starting point , End , Find the maximum load of the path ( That is, the largest of the minimum values of all paths )

Ideas

M S T MST MST+ A good question on the tree .

Greedy consideration , Obviously, it is better to use the edge with larger weight , So construct a maximum spanning tree , Then for each path s t → e d st\rightarrow ed sted, Obviously, this path in the tree is s t − → l a c ( s t , e d ) → e d st-\rightarrow lac(st,ed)\rightarrow ed stlac(st,ed)ed

 Insert picture description here

Therefore, we can consider tree multiplication to find the minimum value on the path , That is, when doubling the father, it will maintain the minimum value at the same time .

code

// Problem: P1967 [NOIP2013 Improvement group ] Truck transportation 
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1967
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Date: 2021-03-20 17:04:42
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5,M=2e4+5,inf=999999999,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define IOS ios::sync_with_stdio(false),cin.tie(0) 
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
int dep[N],fa[N][20],mx,n,m,ww[N][20];
struct bian{
int u,v,w;
}a[N];
int s[N];
int find(int x){return x==s[x]?x:s[x]=find(s[x]);}
void Init(int n){
for(int i=1;i<=n;i++) s[i]=i;
}
struct edge{
int to,nt,w;
}e[N<<1];
int h[N],cnt,vis[N];
void add(int u,int v,int w){
e[++cnt]={v,h[u],w},h[u]=cnt;
e[++cnt]={u,h[v],w},h[v]=cnt;
}
bool cmp(bian a,bian b){
return a.w>b.w;
}
void dfs(int u){ // Find node depth dep
vis[u]=1;
for(int i=h[u];i;i=e[i].nt){
int v=e[i].to;
if(vis[v]) continue;
fa[v][0]=u;
dep[v]=dep[u]+1;
ww[v][0]=e[i].w;
dfs(v);
}
}
int lca(int u,int v){ // Multiply LCA
if(find(u)!=find(v)) return -1;
int ans=inf;
if(dep[u]<dep[v]) swap(u,v);
int delta=dep[u]-dep[v];
for(int i=0;i<=mx;i++)
if(delta&1<<i) ans=min(ans,ww[u][i]),u=fa[u][i];
if(u==v) return ans;
for(int i=mx;~i;i--)
if(fa[u][i]!=fa[v][i]){
ans=min(ans,min(ww[u][i],ww[v][i]));
u=fa[u][i],v=fa[v][i];
}
ans=min(ans,min(ww[u][0],ww[v][0]));
return ans;
}
int dis(int a,int b){
return dep[a]+dep[b]-(dep[lca(a,b)]<<1);
}
int main(){
scanf("%d%d",&n,&m);
mx=log(n);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
}
mst(ww,0x3f);
sort(a+1,a+m+1,cmp);
Init(n);
for(int i=1;i<=m;i++){
int u=a[i].u,v=a[i].v,w=a[i].w;
int fu=find(u),fv=find(v);
if(fu!=fv){
s[fu]=fv;
add(u,v,w);
}
}
for(int i=1;i<=n;i++)
if(!vis[i]) fa[i][0]=i,ww[i][0]=inf,dfs(i);
for(int i=1; i<=mx; i++)
for(int j=1; j<=n; j++){
fa[j][i]=fa[fa[j][i-1]][i-1];
ww[j][i]=min(ww[j][i-1], ww[fa[j][i-1]][i-1]);
}
int q;scanf("%d",&q);while(q--){
int x,y;scanf("%d%d",&x,&y);
printf("%d\n",lca(x,y));
}
return 0;
}

  • 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.
版权声明:本文为[wx6110fa547fd20]所创,转载请带上原文链接,感谢。 https://car.inotgo.com/2021/08/20210810065944659b.html