Ural 1471 Tree

如何修复Windows卡在“正在准备Windows”的问题?这里有详细步骤 如何修复Windows卡在“正在准备Windows”的问题?这里有详细步骤。 阅读详情

询问一棵树的任意两个节点的最短距离。

裸的lca。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<cstring>
using namespace std;
const int maxn=500001;
struct Node{int val,to,next;}node[2*maxn];
int head[maxn],tot;
int fa[maxn];
int ancestor[maxn];
int vis[maxn];
int ans[maxn];
int dis[maxn];
vector<pair<int,int> > que[maxn];
void addedge(int u,int v,int w)
{
    node[tot].val=w,node[tot].to=v,node[tot].next=head[u],head[u]=tot++;
    node[tot].val=w,node[tot].to=u,node[tot].next=head[v],head[v]=tot++;
}
int find(int x)
{
    return x==fa[x]?fa[x]:fa[x]=find(fa[x]);
}
int merge(int a,int b)
{
    int pa=find(a);
    int fb=find(b);
    if(pa!=fb)
    {
        fa[fb]=pa;
        return 1;
    }
    return 0;
}
void dfs(int u,int f)// distance
{
    int v;
    for(int i=head[u];i!=-1;i=node[i].next)
    {
        v=node[i].to;
        if(v!=f)
        {
            dis[v]=dis[u]+node[i].val;
            dfs(v,u);
        }
    }
}
void tarjan(int u,int f)
{
    int v,w;
    ancestor[find(u)]=u;
    for(int i=head[u];i!=-1;i=node[i].next)
    {
        v=node[i].to;
        if(v!=f)
        {
            tarjan(v,u);
            merge(u,v);
            ancestor[find(u)]=u;
        }
    }
    vis[u]=1;
    for(unsigned int i=0;i<que[u].size();i++)
    {
        v=que[u][i].first;
        w=que[u][i].second;
        if(vis[v])
        {
            int tmp=ancestor[find(v)];
            ans[w]=dis[u]+dis[v]-2*dis[tmp];
        }
    }
}
int main()
{
    //freopen("in","r",stdin);
    int n,u,v,w;
    tot=0;
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    for(int i=0;i<=n;i++)
    fa[i]=i;
    for(int i=0;i<n-1;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v,w);
    }
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        scanf("%d%d",&u,&v);
        que[u].push_back(make_pair(v,i));
        que[v].push_back(make_pair(u,i));
    }
    dfs(0,-1);
    memset(vis,0,sizeof(vis));
    tarjan(0,-1);
    for(int i=1;i<=q;i++)
    printf("%d\n",ans[i]);

    return 0;
}


Windows11开机必看:3种方法跳过联网激活(含最新OOBE命令) 本文详细介绍了Windows11新机开箱时绕过联网激活的三种实用方法,包括任务管理器终止进程法、命令行强制终止法和最新的OOBE\BYPASSNRO命令法。特别推荐使用微软官方预留的OOBE命令,操作简单且无副作用,帮助用户快速完成系统设置。 阅读详情

相关推荐

Flink CDC 1.18.1 Oracle 数据同步到postgresql

8、修改Flink的配置文件,主要是各种服务的绑定地址,默认为localhost,统统改为0.0.0.0,如:rest.address: 0.0.0.0 #localhost。将flink-sql-connector-oracle-cdc-3.0.1.jar复制到flink-1.18.1/lib目录中。6、下载 flink-connector-jdbc-3.1.1-1.17.jar,postgresql-42.7.3.jar。7、安装postgresql就不说了,相信你已经有了数据库了。

ynzhangyao的专栏 1805

Ural 1471. Tree

LCA问题,这篇文章讲得不错: http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor 注意事项在代码中 #include #include #include #include using namespace std; #define N 110000 #define MAX

DBL 605

告别Selenium!纯Python+Requests搞定微信视频号登录Cookie(附完整代码与避坑指南)

本文详细介绍了如何利用纯Python和Requests库实现微信视频号的自动化登录,替代传统的Selenium方案。通过解析HTTP请求交互流程,提供完整的代码实现和避坑指南,显著提升爬虫效率和隐蔽性,适用于轻量化爬虫开发。

weixin_30642561的博客 317

ural 1471 Tree题解

本题采用LCA离线算法来做(Tarjan + 并查集) 题目连接:http://acm.timus.ru/problem.aspx?space=1&num=1471 注意本题可能会Crash。采用Color[] = 0 1 2 的解法,而不用Parent[]限制一棵树。 某种情况下也可以用 #pragma comment(linker, "/STACK:1000000000")

967

ubuntu通过qemu安装银河麒麟V10_Arm_v8 (kylin_v10_飞腾/鲲鹏版)

一、windows下安装vmware虚拟机 二、ubuntu下载地址 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/20.04/ubuntu-20.04.3-live-server-amd64.iso 三、vmware中新建虚拟机,并使用ubuntu-20.04.3-live-server-amd64.iso作为启用盘 。。。在虚拟机中安装ubuntu(略) 四、配置ssh(如果ssh登录不上时配置) 修改openssh的配置 vi /etc/s

u010533742的博客 8714

从零开始玩转边缘 AI:Jetson Nano 深度学习环境搭建与 YOLOv5 部署指南

Jetson Nano是NVIDIA推出的嵌入式AI开发板,凭借其强大的GPU计算能力和完善的软件生态系统,成为边缘AI部署的理想平台。本教程源于作者团队参加2024中国机器人大赛暨RoboCup机器人世界杯中国赛机器人先进视觉3D识别赛道的实战经验,是一份经过验证的环境部署指南。

LXY_LOVE36的博客 240

URAL 1471 Tree

URAL_1471     这个题目可以用树链剖分做,另外推荐一个讲树链剖分通俗易懂的博客:http://blog.sina.com.cn/s/blog_6974c8b20100zc61.html。 #include&lt;stdio.h&gt; #include&lt;string.h&gt; #define MAXD 50010 #define MAXM 100010 int ...

weixin_34233618的博客 132

06 - 深度学习处理器原理

深度学习处理器的重要性在于它们可以加速深度学习任务的执行,从而推动了人工智能的发展。传统的通用处理器虽然可以执行深度学习任务,但它们不够高效,需要消耗大量的能量和时间。深度学习处理器通过硬件加速和特定设计来优化深度学习任务的执行,从而可以提高执行效率、降低能耗和加速训练和推断过程。 深度学习处理器的发展也推动了深度学习算法的创新,因为算法的发展需要硬件的支持和驱动。同时,深度学习处理器的出现也促进了更广泛的应用场景,包括自动驾驶、人脸识别、语音识别、图像识别等领域。因此,深度学习处理器的重要性不仅在于加速

m0_51339444的博客 757

Ural1471-Distance in the Tree

给定一棵树,求两个节点之间的距离。 以0为根节点,先dfs求出遍历的节点顺序和对应的节点深度,记录每个节点在欧拉序列中第一次出现的下标,以及每个节点到根节点的距离。 预处理RMQ能够快速得到在欧拉序列中的某个区间中离根最近的点(深度最浅),然后通过寻找两个节点u,v在欧拉序列中首次出现的位置区间内深度最浅的对应节点就是LCA(u,v),则u,v之间的距离为dis[u]-dis[t]+dis[v]

Collider 578

华为OD机试真题 新系统2026-04-01 Python&JS 实现【空间占用计算】

员工A的磁盘空间经常被耗尽,他需要找到占用空间最大的目录或文件,然后决定如何清理文件释放空间。给定某一目录,请编写程序帮助他统计该目录内一级子目录和文件的占用空间,并返回目标目录一级子项(文件或子目录)中占用空间最大的项。

MISAYAONE的博客 1372

视觉Transformer实战 | Transformer详解与实现

本文系统介绍了 Transformer 架构的核心原理与实现。相比传统 RNN 和 CNN,Transformer 凭借自注意力机制有效捕捉序列中的长距离依赖关系,支持并行训练,显著提升效率。其核心创新在于通过查询、键、值向量计算注意力权重,理解单词间的语义关联。架构包含编码器-解码器结构:编码器通过多头自注意力和前馈网络提取输入特征;解码器利用掩码自注意力和交叉注意力生成输出。本文使用 PyTorch 实现了缩放点积注意力、位置编码、层归一化等关键模块,并构建了完整的 Transformer 模型。

盼小辉丶的博客 4万+

ural 1471 Tree(最近公共祖先)

裸LCA,用的DFS+并查集,这题弄了我一下午。。。。 #include #include #include #include #include #include #include #include #include using namespace std; const int maxn=50010; const int maxm=75010; struct E { int u,v,w,n

730

各大厂薪资对比爬虫:Python采集OfferShow爆料数据

print(f"⚠️ API返回错误码: {data.get('code')}, msg: {data.get('msg')}")ax.set_title('各大厂薪资对比 (基于OfferShow爆料数据)', fontsize=16, fontweight='bold')stats.columns = ['人数', '平均年薪', '年薪中位数', '年薪标准差', '平均月薪', '平均月数']print(analyzer.get_position_stats('字节跳动').head(10))

2201_76125261的博客 614

ural1471 Distance in the Tree

Distance in the Tree Time limit: 1.0 secondMemory limit: 64 MB A weighted tree is given. You must find the distance between two given nodes. Input The first line contains the number of n...

weixin_30614109的博客 157

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

LCA裸题 Distance in the Tree URAL - 1471

A weighted tree is given. You must find the distance between two given nodes. Input The first line contains the number of nodes of the tree n (1 ≤ n ≤ 50000). The nodes are numbered from 0 to n–1....

qq_29904713的博客 353

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

URAL 1471(lca tarjan算法)

题意:给定一棵树,查询时给定两个点,求出两个点的距离。 暴力做肯定超时的。我的做法是采用lca(最近公共祖先)的离线算法,即tarjan算法(据说Tarjan提出了很多算法,可能还有很多tarjan算法),算法里用到了并查集。在输入完所有查询之后,在求出答案。tarjan算法的做法是:一开始vis数组初始化为0,从树根开始递归往下对点进行染色,刚到一个点的时候将vis取为-1,在继续递归

84970000的专栏 775

深度学习工具包 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');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LCA 暂存 1471 URAL

#include #include #include #include using namespace std; #define max1 50010 #define max12 75010 int parent[max1]; ///是没向变为有向的标志 int father[max1];//并查集集合中的代表节点 int ancestor[max1];//LCA中的祖先节点 in

站稳脚跟学做人! 715

Ural 1100. Final Standings

水题,把AC 的题目分类即可。 #include #include #include #include #include #include #include #include #include #include using namespace std; #define eps 1e-7 #define INF 0x3fffffff #define L(u) (u<<1) #de

Kryi的专栏 840

Ural 1028. Stars

看树状数组课件的时候见到过 这个题,给出恒星的坐标,Y轴从小到大有序,那么按y轴的顺序修改树状数组的值,求1-x的区间和就是在恒星左下的恒星数。 #include #include #include #include #include using namespace std; const int maxn=15010; const int maxm=32010; int num[max

Kryi的专栏 822

Ural 1003. Parity

给出在一个区间内1的奇偶性,输出在第一次出现矛盾位置。 个人感觉是一个比较难也比较好的并查集的题目,一开始并没有看出来,囧。 把给出的区间转化为前开后闭区间,查询区间的短点是否为同一个祖先,若是,则判断奇偶性是否正确。 若不在同一个区间把区间短点合并。 注意两点: 1.记录当前区间短点到最远祖先这段区间的奇偶性。 2.合并的时候,是把要合并的区间端点的两个祖先合并。路径压缩的时候修改祖

Kryi的专栏 809

Ural 1306. Sequence Median

求一个数列的中位数,最多有250000个数。 看了discuss才知道用优先队列,先压栈N/2+1个然后边压栈边弹栈。 N为奇数就是栈里的弹一个,N为偶数就从栈里弹两个。 #include #include #include #include using namespace std; priority_queue que; int main() { //freopen("in","r

Kryi的专栏 807

Ural 1126. Magnetic Storms

题好难读,输出长度为k的区间内的峰值。 单调队列的应用。 单调队列的插入就是把一个元素加入,从队尾开始到比第一个他的大的元素位置之后,在之后的元素全部抛弃。 删除就是从对头找到第一个满足可在队列中的元素作为对头。 #include #include #include #include #include #include #include using namespace s

Kryi的专栏 764

Ural 1019. Line Painting

在0到10^9的线段上,一开始颜色全白,然后修改多次子区间颜色或黑或白。求最终状态下的白色最长区间。 主要就是离散化,然后就直接用线段树了。刚刚开始学线段树,代码很挫。 #include #include #include #include #include using namespace std; const int maxn=10010; int col[maxn],x[ma

Kryi的专栏 746

Ural 1037. Memory Management

看到网上有用堆做的,我是用线段树做的,根据noclyt的思想。 把30000个内存建树,记录区间内最早到期的内存时间。 当申请内存时,查询编号最小的空闲内存。 当访问内存时,先查询内存块时候已经到期,否则更新内存块的到期时间。 #include #include #include #include #include #include #include #include #i

Kryi的专栏 717

Ural 1090. In the Army Now

#include #include #include #include #include #include #include using namespace std; const int maxn=10010; int sum; int f[maxn*4]; void Build(int n) { sum=0; n=n<<2; for(int i=0;i<=

Kryi的专栏 696

Ural 1654. Cipher Message

删掉连续的相同两个字母,直到不存在两个连续且相同的字母。 模拟一个栈即可。 #include #include #include #include #include #include #include #include #include #include using namespace std; #define eps 1e-7 #define INF 0x3ffffff

Kryi的专栏 668
上一篇: Codeforces Beta Round #52 (Div. 2) D - Changing a String
下一篇: poj 1523 SPF
kryi
博客等级 码龄15年 5粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值