题目链接

戳我

\(Solution\)

这一道题直接用\(kruskal\)重构树就好了,这里就不详细解释\(kruskal\)重构树了,如果不会直接去网上搜就好了.接下来讲讲详细过程.

  • 首先构建\(kruskal\)重构树.
  • 对于询问直接求\(lca\)就可以了,如果没有\(lca\)输出\(-1\),否则输入\(lca\)上的权值就好了,不是很难.

\(Code\)

#include<bits/stdc++.h>
using namespace std;
const int N=200011;
int read(){
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9') f=(c=='-')?-1:1,c=getchar();
while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();
return f*x;
}
struct node1 {
int to,next;
}e[500001];
struct node{
int x,y,v;
}b[N];
int res,cnt,n,m,t,pre[N],head[N],vis[N];
int f[N][21],dep[N],val[N],bin[101];
void add(int x,int y){ e[++cnt].to=y,e[cnt].next=head[x],head[x]=cnt; }
int find(int x){ return pre[x]==x?x:pre[x]=find(pre[x]); }
bool cmp(const node & a , const node & b ){ return a.v>b.v; }
void dfs(int k){
vis[k]=1;
for(int j=1;j<=19;j++)
f[k][j]=f[f[k][j-1]][j-1];
for(int i=head[k];i;i=e[i].next){
int v=e[i].to;
f[v][0]=k,dep[v]=dep[k]+1;
dfs(v);
}
}
int lca(int x,int y){
if(dep[x]<dep[y])
swap(x,y);
// cout<<dep[x]<<" "<<dep[y];
for(int i=19;i>=0;i--)
if(dep[x]-(1<<i)>=dep[y])
x=f[x][i];
if(x==y)
return y;
for(int i=19;i>=0;i--){
if(f[x][i]==f[y][i]||!f[y][i]||!f[x][i])
continue;
x=f[x][i],y=f[y][i];
}
return f[x][0];
}
void build(){
res=n,sort(b+1,b+1+m,cmp);
for(int i=1;i<=m;i++){
int fx=find(b[i].x),fy=find(b[i].y);
if(fx!=fy)
val[++res]=b[i].v,pre[fx]=res,pre[fy]=res,add(res,fx),add(res,fy);
if(res==n*2-1)
break;
}
}
int main(){
n=read(),m=read(),bin[1]=0;
for(int i=1;i<n*2;i++) pre[i]=i;
for(int i=1;i<=19;i++) bin[i]=bin[i-1]<<1;
for(int i=1;i<=m;i++) b[i].x=read(),b[i].y=read(),b[i].v=read();
build();
for(int i=1;i<=n;i++)
if(!vis[i])
dfs(find(i));
t=read();
while(t--){
int x=read(),y=read();
int p=lca(x,y);
// cout<<find(x)<<" "<<find(y)<<endl;
if(p==0)
cout<<"-1\n";
else
cout<<val[p]<<endl;
}
}

「NOIP 2013」 货车运输的更多相关文章

  1. [Noip 2013 Day1-3] 货车运输 做法总结

    [Noip 2013 Day1-3] 货车运输 做法总结 Online Judge:Luogu-1967 Label:启发式合并,离线,整体二分,按秩合并,倍增,最大生成树 打模拟离线赛时做到,顺便总 ...

  2. NOIP 2013 P1967 货车运输

    倍增求LCA+最大生成树 题目给出的是一张图,在图上有很多算法无法实现,所以要将其转化为树 题中可以发现货车的最后的载重量是由权值最小的一条边决定的,所以我们求最大生成树 求完最大生成树后我们得到一个 ...

  3. 「NOIP 2017」列队

    题目大意:给定一个 $n times m$ 的方阵,初始时第 $i$ 行第 $j$ 列的人的编号为 $(i-1) times m + j$,$q$ 次给出 $x,y$,让第 $x$ 行 $y$ 列的人 ...

  4. 「NOIP 2020」微信步数(计数)

    「NOIP 2020」微信步数(Luogu P7116) 题意: 有一个 \(k\) 维场地,第 \(i\) 维宽为 \(w_i\),即第 \(i\) 维的合法坐标为 \(1, 2, \cdots, ...

  5. 「NOIP2013」「LuoguP1967」货车运输(最大生成树 倍增 LCA

    题目描述 AA国有nn座城市,编号从 11到nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最 ...

  6. 「NOIP2013」货车运输

    传送门 Luogu 解题思路 首先 \(\text{Kruskal}\) 一下,构造出一棵森林. 并查集还要用来判断连通性. 倍增 \(\text{LCA}\) 的时候顺便维护一下路径最小值即可. 细 ...

  7. 「HNOI 2013」游走

    题目链接 戳我 \(Solution\) 首先申明几个变量: f[x]:到点x的概率, vis[x]:x点的度 dp[x][y]:(x,y)这条边的概率 number[x][y]:x这条边的编号 下面 ...

  8. 「HNOI 2013」比赛

    题目链接 戳我 \(Solution\) 这道题观察数据范围发现很小,再看看题目可以发现是搜索. 这题纯搜索会\(T\)所以要加入适当剪枝 如果一个人后面的比赛都赢却依旧到不了目标分数,则直接\(re ...

  9. 「HNOI 2013」消毒

    题目链接 戳我 \(Solution\) 我们首先想一想如果这一题只是二维的该怎么办? 就是一个最小点覆盖问题.这里就不详细解释了,用网络流或匈牙利都无所谓. 但现在是三维的,那么现在该如何处理呢? ...

随机推荐

  1. 通过ajax异步调用返回值

    调用方法的时候传递一个callback方法来获取成功回调的值test(function (data) { }); function test(callback){ $.ajax({ type: &qu ...

  2. C# Attribute的用法

         {                   {                            Attribute[] atts = Attribute.GetCustomAttribut ...

  3. nginx 真实ip

    server { listen       80; server_name  localhost; location /{ root   html; index  index.html index.h ...

  4. Visual Studio Find All no results.

    重装WDK什么的有时候有bug....写下面注册表修复 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Wow6432Node\CLSI ...

  5. inline-block元素出现位置错位的解决方法

    如下代码所示: <div class="container"> <div style="display: inline-block; height: 1 ...

  6. 82. Remove Duplicates from Sorted List II (List)

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  7. zabbix主机自动注册

    一.主机自动注册的流程 zabbix agent指定server active主动自己的信息提供给zabbix_server,zabbix_server根据提供的信息自动添加主机,方便. 二. lin ...

  8. [SoapUI] JsonPath 语法 与 XPath 对比

    XPath JSONPath Description / $ the root object/element . @ the current object/element / . or [] chil ...

  9. [Schema] I have updated my XML Schema for my service but SoapUI still generates/validates according to the old schema.

    SoapUI caches XML schemas when they are first loaded. If you need to force a reload of an interfaces ...

  10. [GO]channel实现同步

    goroutine运行在相同的地址空间,因此访问共享内存必须 做好同步.goroutine奉行通过通信来共享内存,而不是共享内存通信 它跟map一样,使用make来创建,它是一个引用 ,而不是值传递 ...