#include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<cstring>
#include<vector>
#include<bitset>
#include<algorithm>
using namespace std;
struct data
{
int x,y,z;
};
data tr[50005];
struct node
{
int l,w;
};
node tt;
vector <node> te[10005];
int n,m,a,b,c,q,k=1;
int fa[10005],dep[10005],dis[10005][25],f[10005][25];
bool vis[10005];
bool comp(data a,data b)
{
if (a.z>=b.z) return true;
return false;
}
int find (int x){
if (fa[x]==x) return x;
else {
fa[x]=find(fa[x]);
return fa[x];
}
}
void kruskal(){
for (int i=1;i<=m;i++)
{
int r1=find(tr[i].x);
int r2=find(tr[i].y);
if (r2!=r1){
fa[r2]=r1;//here
tt.l=tr[i].y;tt.w=tr[i].z;
te[tr[i].x].push_back(tt);
tt.l=tr[i].x;tt.w=tr[i].z;
te[tr[i].y].push_back(tt);
}
}
}
void dfs(int x)
{
vis[x]=true;
for (int i=0;i<te[x].size();i++)
if (!vis[te[x][i].l])
{
dep[te[x][i].l]=dep[x]+1;
dis[te[x][i].l][0]=te[x][i].w;
f[te[x][i].l][0]=x;
dfs(te[x][i].l);
}
}
void init()
{
for (int j=1;(1<<j)<=n;j++)
for(int i=1;i<=n;i++)
{
f[i][j]=f[f[i][j-1]][j-1];
dis[i][j]=min(dis[f[i][j-1]][j-1],dis[i][j-1]);
}
}
int lca(int x,int y)
{
int sm=10000000;
if (dep[x]>dep[y]) swap(x,y);
int h=dep[y]-dep[x];
for (int i=0;(1<<i)<=h;i++)
if ((1<<i)&h)
{
sm=min(sm,dis[y][i]);
y=f[y][i];
}
if (x!=y)
{
for (int i=(int)log2(n);i>=0;i--)
if (f[x][i]!=f[y][i])
{
sm=min(sm,min(dis[x][i],dis[y][i]));
x=f[x][i];y=f[y][i];
}
sm=min(sm,min(dis[x][0],dis[y][0]));
x=f[x][0];
}
return sm;
}
int main()
{
freopen ("truck.in","r",stdin);
//freopen ("truck.out","w",stdout);
scanf ("%d%d",&n,&m);
for (int i=1;i<=n;i++) fa[i]=i;
for (int i=1;i<=m;i++)
{
scanf ("%d%d%d",&a,&b,&c);
tr[i].x=a;
tr[i].y=b;
tr[i].z=c;
}
sort(tr+1,tr+m+1,comp);
kruskal();
dep[1]=1;
dfs(1);
init();
scanf ("%d",&q);
for (int i=1;i<=q;i++)
{
scanf ("%d%d",&a,&b);
if (find(a)!=find(b)) printf ("-1\n");
else
{
int s=lca(a,b);
printf ("%d\n",s);
}
}
return 0;
}相关推荐
制作Ubuntu22.04启动盘
现在越来越多程序代码都需要运行在linux环境下,运行这些代码的前提是安装一个稳定的linux操作系统。本文选择Ubuntu 22.04去制作系统启动盘。本文主要对ubuntu22.04的启动盘制作过程进行了详细的说明,希望能帮助到有需要的朋友。
NOIP2013 Day1 T3 货车运输
估计30分实际只有25分的SPFA求瓶颈路暴力#include<cstdio> #include<cstring> #include<queue> #include<iostream> using namespace std; const int inf = 100001; const int maxn = 10010; const int maxm = 50050; int n, m, q, to
VSCode+Pyside6开发环境配置全攻略:从安装到实战UI设计
本文提供了一份详细的VSCode与Pyside6集成开发环境配置指南。从创建Python虚拟环境、安装必备VSCode插件,到深度配置PYQT Integration插件以打通Qt Designer与代码编译,最后通过一个待办事项应用实战演示了从UI设计到业务逻辑绑定的完整工作流,旨在帮助开发者建立高效、可维护的GUI开发环境。
NOIP2013T3 小朋友的数字
实例分析 5 981-409 -401 97 -96 -301 10 711907 -565 310 643 -111 -180 -426 -758 -901 253 #include<cstdio> #include<algorithm> #define LL long long #define N 1000005 #def...
Win10下ISE14.7安装避坑全记录:从License加载失败到ChipScope连不上,我踩过的雷都在这了
本文详细记录了在Win10系统下安装ISE14.7时遇到的常见问题及解决方案,包括License加载失败、IMPACT闪退、ChipScope连接故障等。通过实际案例和步骤解析,帮助用户避开安装陷阱,确保FPGA开发工具顺利运行。特别针对license问题提供了兼容性设置和文件修改的实用技巧。
【agent实战教程】OpenClaw多个agent一文通
一个合格的独立agent,必须具备6个完全独立的专属组件独立组件路径规则核心要求不独立的严重后果唯一agentId配置文件中全局唯一,不能重复,建议用英文+数字,不要有特殊字符agent配置冲突,消息派单错误,会话串线独立工作区(workspace)建议每个agent一个独立文件夹,不要共用人设、记忆、规则混在一起,agent人设崩塌,对话串线独立状态目录(agentDir)固定系统自动生成,绝对禁止多个agent复用同一个agentDir。
NOIP2013 DAY1 T3
题目链接Kruskal+LCA求解 Kruskal求最大生成树 LCA查找两点间最小权值 上代码 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define MIN(a,b) a<b?a:b using namespace std;int n,m,fa[500000],khead[500000],hea
RT-DETR算法优化改进:Backbone改进 | HGBlock完美结合PPHGNetV2 GhostConv
GhostConv助力RT-DETR ,HGBlock与PPHGNetV2 GhostConv完美结合
【NOIP2013 Day1T3】货车运输
这道题目质量很高啊…… 很容易想到一个Floyd的暴力算法:O(N3)O(N^3)O(N3) 求出每个点之间的最长路,超时炸空间。 我们发现这道题的”最短路“有些特殊:不是求和而是求出路径上的权值最小的一条边的权值。 我们发现,图中的很多边是一定不会经过的,举例来说: 在这个图中,显然 2→42\rightarrow42→4 这条边我们无论如何是不会经过的。因为如果我们想要从点 222 走到点 444,2→1→42\rightarrow 1\rightarrow 42→1→4 明显是更好的选择。同样,如
材料仿真软件:LAMMPS_(8).温度和压力控制
在 LAMMPS 中,温度和压力的控制是通过多种恒温器和恒压器实现的。Nose-Hoover 恒温器和恒压器因其准确性和广泛适用性而被经常使用,而 Berendsen 恒温器和恒压器则因其计算效率高而适用于初步仿真或粗粒化模型。温度重标和压力重标方法虽然简单,但在某些情况下可以快速调整系统的温度和压力。通过选择合适的控制方法,可以根据具体的需求和系统特性来设计和运行分子动力学仿真,从而获得准确和可靠的结果。在实际应用中,建议先进行初步的仿真以确定合适的控制参数,然后再进行细致的仿真以验证结果的稳定性。
YOLO26科研升级:BiFPN双向特征金字塔,让多尺度特征融合精度再突破
在目录下,新建AddModules文件夹,将bifpn.py放入其中。
【NOIP2013】Day1T3 货车运输
货车运输 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。 Input Format 第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道路。 接下来 m 行每行 3 个整数 x、y、z,每两
VN5620以太网测试——环境搭建篇
CANoe(CAN open environment)VN5620 :是一个紧凑而强大的接口,用于以太网网络的分析、仿真、测试和验证。VN5620 接口支持各种可能的应用。它适用于与其他总线系统、网络参与(例如在模拟和生成帧、测试中的负载和错误)的同步以太网监控。在2021年Vector公司的以太网接口卡VN5610和VN5640设备已经陆续停产,相应被VN5620和VN5650代替。
NOIp 2013 Day1T3 货车运输
Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。 Input: 输入文件名为 truck.in。 输入文件第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道 路。...
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
win10 跳过U8环境检测的IE WebControls.rar
win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。
Noip2013 Day1 T3 货车运输(树上倍增)
有生之年系列,我竟然改出来了一道第三题
TMS_WebGMaps_1.9.8.0_Full_Source
TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder
货车运输 NOIP 2013 day1 t3
这个题目想A掉两个多月了,今天终于AC了,好开心 ^_^ 题意是 有一个有n个点m条边的图,每次询问两个点路径上最小边权的最大值。 根据最大生成树的性质,先kruskal求出最大生成树,有g存储新的生成树。 然后倍增相当于在树上求出两两点的路径上最小边权的最大值。 之后通过dfs解决f[i][0],w[i][0],dep[i]f[i][0],w[i][0],dep[i]f[i][0],w[...
文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况
主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。
【NOIP】2013提高组Day1T3 货车运输
此题写法蛮多的,倍增和启发式合并的写法想必大家已经了解了,本人稍微提一下好了。 而至于按秩合并和整体二分的写法,本人就稍微详细地讲解一下。 解法一:生成树+倍增 用克鲁斯卡尔算法构建出若干棵最大生成树,那么每个询问就转化成了求x到y路径上的最小边权,用倍增维护即可。 代码: #include<bits/stdc++.h> #define MAXN 100010 using n...
Noip 2013 Day1 T3 货车运输 启发式并查集树
题目大意:给一个图,求最大生成树,询问生成树上两个结点路径上最小的边权,若两点不可达输出-1。 这道题是很明显的LCA问题可以倍增写(详见http://blog.csdn.net/nkwbtb/article/details/21880941), 但是我今天看了看题解,惊奇地发现了一种极其美妙的做法。 就是启发式并查集树。 名字听起来非常高大上,实际上很简单,启发式并查集,就是维护每个集合
货车运输 noip2013day1t3
题目描述Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。 输入描述Input Description 第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道路。...
Luogu P1967 货车运输(NOIP2013Day1T3)
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。 输入输出格式 输入格式: 输入文件名为 truck.in。 输入文件第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道 路。 接下...
Noip 提高组 2013 Day1 T3 货车运输 Kruskal+倍增
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。 输入输出格式 输入格式: 输入文件名为 truck.in。 输入文件第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m
洛谷1967货车运输 即 NOIP2013 DAY1 T3
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。 输入输出格式 输入格式: 输入文件名为 truck.in。 输入文件第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m...
【NOIP2013提高组】DAY1T3—货车运输(Kruscal重构树)
描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物? 输入 输入文件第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道 路。 接下来 m 行每行 3 个整数 x、 y、 z,每两个整数之间用一个空格隔开,表示...
1万+




被折叠的 条评论
为什么被折叠?



