hdu 1232 畅通工程

HDU 1232 畅通工程 并查集入门经典 OJ题解 HDU1232 畅通工程一、背景简介二、题目描述Problem DescriptionInputOutputSample InputSample Output三、题解思路四、AC代码完整代码其他参考 一、背景简介 此题为并查集的经典例题,入门水准。如果想学习这个数据结构,一定要好好学习这个基础,模版可以背下来。题目来源为HDU 1232,具体链接为:http://acm.hdu.edu.cn/showproblem.php?pid=1232。 二、题目描述 Problem Description 某省调查城 阅读详情

(1)类型:并查集。
(2)题意:中文题。
(3)时间复杂度:并查集简单题,时间复杂度够小但当复杂一点的并查集的题目,会卡c++的输入流输出流的时间,所以当遇到复杂的并查集题目,输入输出尽量用scanf();
(4)代码:

#include<iostream>
using namespace std;
const int MAX=1e3+6;
int n,m,ans,par[MAX];
int GetPar(int a){return par[a]==-1?a:par[a]=GetPar(par[a]);}
void Merge(int a,int b){
     int p1=GetPar(a),p2=GetPar(b);
     if(p1!=p2)par[p1]=p2;  
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    while(cin>>n>>m,n){ans=0;
        for(int i=1;i<=n;i++)par[i]=-1;
        for(int i=1;i<=m;i++){
            int a,b;cin>>a>>b;
            Merge(a,b);
        }
        for(int i=1;i<=n;i++)
            if(par[i]==-1)ans++;
        cout<<ans-1<<endl;
    }
}
Windows Server下OnlyOffice Workspace社区版安装避坑指南(含局域网邮箱配置) 本文详细介绍了在Windows Server环境下部署OnlyOffice Workspace社区版的完整流程,特别针对企业内网环境中的邮箱配置和端口冲突等常见问题提供了实用解决方案。通过硬件资源配置建议、软件环境检查清单以及内网邮箱系统搭建方案对比,帮助用户高效搭建企业级协作平台,实现团队协作的无缝对接。 阅读详情

相关推荐

CST微波工作室端口设置保姆级教程:从波导端口到离散端口,手把手教你搞定S参数仿真

本文提供CST微波工作室端口设置的详细教程,涵盖波导端口和离散端口的选择与配置,助力工程师精准完成S参数仿真。通过微带线滤波器案例,详解端口尺寸、阻抗设置及常见问题排查,提升仿真效率与准确性。

weixin_42570296的博客 362

素数筛(卡时间复杂度的题目)

(1)类型:数学中典型的素数题目。 (2)题目: /* Description 现在给你一个正整数N,要你快速的找出在2…..N这些数里面所有的素数。Input 给出一个正整数数N(N<=2000000) 但N为0时结束程序。 测试数据不超过100组Output 将2~N范围内所有的素数输出。两个数之间用空格隔开Sample Input 5 10 11 0 Sample O

Light2Chasers的博客 868

7-15 估值一亿的AI核心代码

函数封装,含一个字符串中的所有的某个字串全部替换成另一个的函数,还有判断是否独立的函数以上图片来自新浪微博。!

weixin_73529240的博客 601

迷宫的最短路径

description: 给定一个大小为N*M的迷宫。迷宫由通道和墙壁组成,每一步可以向邻接的上下左右四格的通道移动。请求出从起点到终点所需的最小步数。请注意,本题假定从起点一定可以移动到终点。 样例输入输出: 输入: 输出: 22 AC code: #include&amp;lt;iostream&amp;gt; #include&amp;lt;cstring&amp;gt; #include&amp;lt;qu...

Light2Chasers的博客 567

开源网格划分软件-Gmsh二次开发教程 (4)网格生成与控制​

​本章将深入讲解Gmsh的网格生成技术,包括全局/局部网格控制、边界层生成、自适应细化等,并提供完整的代码示例。

ZQGG的博客 1473

hdu1232畅通工程

HDU1232畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? Input 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < ...

小麦面包 480

AEB(自动紧急制动)

AEB系统对静止物体的误判率受多种因素影响,包括传感器精度、算法复杂性以及环境条件等。通过多传感器融合、算法优化、硬件升级以及环境适应性增强等技术手段,可以有效减少误判问题。美国和欧洲的AEB法规均强调了技术性能和安全性,但具体要求有所不同。美国的NHTSA规定更注重高速和复杂场景下的紧急制动能力,而欧盟的ECE R152法规则更侧重于基础的AEB功能覆盖范围。

百态·数智思维 | 聚焦大数据、量化与未来AI 2550

《HDU 1232 - 畅通工程》的题解

《D - 畅通工程》的题解 题目的描述 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? Input 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M...

weixin_46163391的博客 516

CenterPoint 工程复现

CenterPoint 工程复现,本文中使用版本:BEVerse工程下的mmdet3d (`MMDet3d v0.17.3`)版本,MMDetection3D中其他版本的使用`大同小异`。

Darchan的博客 5663

ABAP Platform 2025 到来之后,我们该怎样重新理解 SAP S/4HANA 2025 的开发边界

2025年10月,SAP正式发布S/4HANA Cloud Private Edition 2025及S/4HANA 2025,其技术底座ABAP Platform 2025全面支持SAP HANA数据库。新版平台强调Clean Core理念升级为四级成熟度模型,推动ABAP Cloud成为扩展开发首选方案。AI能力正式融入ABAP开发全流程,包括代码生成、迁移辅助等场景。RAP框架显著增强,支持跨业务对象协作、树形视图等复杂业务场景。ADT开发工具持续进化,新增内存分析、单元测试等专业功能,形成完整开发工

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 306

hdu1232-畅通工程

畅通工程 我都不知道该怎么形容我最近的状态了,刷水题狂卡,这个题目也是一样,输入n,居然忘了排除n=0 // File Name: hdu1232.cpp // Author: rudolf // Created Time: 2013年04月25日 星期四 19时42分58秒 #include #include #include #include #include #includ

bo-jwolf的专栏 1044

冷门但实用:Python爬取微信公众号历史文章,突破微信反爬限制(2025有效)

2025年微信公众号爬取的核心是「冷门方法+指纹伪装」,而非常规接口破解,itchat-uos+Playwright组合是突破设备指纹的关键;登录态持久化、动态Token实时获取、频率严格控制是避免被封禁的三大核心;文章内容解析需解决2025新增的编码混淆问题,chardet+BeautifulSoup是冷门但有效的方案;合规是前提,爬取频率、数据用途需严格遵守平台/法律规则。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 3777

2020寒假集训专题三 HDU - 1232 畅通工程

HDU - 1232 畅通工程题解 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 专题链接:https://vjudge.net/contest/351234#problem/H 思路 题意要求出保证每个城镇“通路”的前提下的最小待建路数,我们只要将原本就存在的路尽可能地纳入我们的最终企划中,再用城镇数-1再减去纳入规划中的原有路数即可。...

bllovepigpig的博客 152

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

HDU1232 畅通工程【并查集】

问题链接:HDU1232 畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?     Input 测试输入包含若干测试用例。...

aaaaawyf的博客 254

深度学习工具包 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")

【并查集】HDU 1232 畅通工程

HDU 1232 畅通工程 #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <cstring> #include <string> #include <v...

L_X_ 134

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

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

hdu 1232 畅通工程

hdu  1232  畅通工程                题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 并查集 题目分析:重点在这个水爆了的算法 code: #include #include int father[1010]; int _find(int a) { while(father[a]!=-1)a=father[a

slicer的技术交流栈 759

hdu 1232 畅通工程【并查集】【模板题】

hdu 1232 畅通工程  #include&lt;iostream&gt; #include&lt;string.h&gt; using namespace std; int pre[1005]; int find (int x) { int r=x; while(pre[r]!=r) r=pre[r]; int i=x; int j; while(i!=r) { ...

不甘 289

hdu 1232 畅通工程(并查集)

hdu 1232 畅通工程 http://acm.hdu.edu.cn/showproblem.php?pid=1232 ProblemDescription 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需...

diansitao5461的博客 77

HDU-1232 畅通工程

HDU-1232 畅通工程 链接添加链接描述 分析: 1.运用了并查集的思想,将互相连通的城镇看成一个集合,建设a与b之间的一条道路就是把a所在的集合与b所在的集合进行合并。 2.使用find函数进行路径压缩,使得代码更加高效。 3.将所有集合都看成一个点,则易得使n个点(城镇集合)全部连通所需要最少的边(道路)为n-1条. 代码 #include<bits/stdc++.h> using namespace std; int fa[1010]; //fa[r]代表r的父节点 int find

qq_55788088的博客 184

hdu1232详解 畅通工程

题目链接:hdu1232畅通工程这一题是并查集的题,关键就是找出根结点的数目,下面附上详细的代码和注释讲解:(注释中  祖先=根节点)#include&lt;bits/stdc++.h&gt; int m,n,pre[1010],rank[1010];//pre数组存关系(pre[a]==b表示a的父亲是b),rank数组存树上有多少节点(rank[i]==j表示根节点为i的树上有j个节点) v...

Ruhe_king的博客 1357

畅通工程(HDU 1232)经典并查集

#include <bits/stdc++.h> using namespace std; const int N = 1005; int table[N]; int findt(int x){ if(table[x]==-1){ return x; } table[x]=findt(table[x]); return table[x]; } void merge(int a, int b){ int ta=findt(a); int tb=findt(b); if(t

征服所有不服的博客 162

HDU 1232 畅通工程 [并查集]

Description给N个城市,再给出M条关系,表示两者是否连通 问 再修几条路,使他们全部连通Algorithm并查集 并一下,看最后有几个集合,那个-1就是ansCodeimport java.util.Scanner;class UnionFind { int n; int[] par; int[] rank; UnionFind(int nn) { n = n

more time 535

【并查集】HDU 1232 畅通工程

题目链接-&gt;http://acm.hdu.edu.cn/showproblem.php?pid=1232 HDU 1232   畅通工程                                                                                  畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...

荷叶田田 428

HDU-1232.畅通工程(并查集)

HDU-1232.畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? Input 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( &lt...

紫藤&code的博客 182
上一篇: poj 2386 lake counting
下一篇: poj 1979 Red and Black
图南1204
博客等级 码龄9年 21粉丝 118原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值