[FROM WOJ]#2235 Maintain
#2235 Maintain
传送门
SOL
这道题要求动态维护MST
由于数据很小,可以每次跑Kruskal
然而sort会超时
直接插入排序即可
代码:
#include<bits/stdc++.h>
using namespace std;
#define re register
inline int rd(){int re data=0;static char ch=0;ch=getchar();while(!isdigit(ch))ch=getchar();while(isdigit(ch))data=(data<<1)+(data<<3)+(ch^48),ch=getchar();return data;
}
const int N=205,M=10005;
struct edge{int u,v,w;}e[M],tmp;
int fa[N],n,m;
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline int kruskal(int now,int cnt=0,int sum=0,bool add=0){for(int re i=1;i<=n;++i)fa[i]=i;if(now>1){for(int re i=1;i^now;++i)if(e[i].w>tmp.w){for(int re j=now;j>i;--j)e[j]=e[j-1];e[i]=tmp,add=1;break;}if(!add)e[now]=tmp;}else e[now]=tmp;for(int re fu,fv,u,v,w,i=1;i<=now;++i){u=e[i].u,v=e[i].v,w=e[i].w,fu=find(u),fv=find(v);if(fu==fv)continue;fa[fu]=fv,sum+=w,++cnt;if(cnt==n-1)break;}if(cnt!=n-1)return -1;else return sum;
}
signed main(){n=rd(),m=rd();for(int re i=1;i<=m;++i)tmp.u=rd(),tmp.v=rd(),tmp.w=rd(),printf("%d\n",kruskal(i));exit(0);
}
[FROM WOJ]#2235 Maintain相关推荐
- puppet cert maintain
puppet cert maintain posted on 2014-08-06 10:02 秦瑞It行程实录 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblo ...
- SAP QM QS41 试图维护Catalog为3的Code Group, 报错-You need to maintain catalog 3 (Usage Decisions) in Customi
SAP QM QS41 试图维护Catalog为3的Code Group, 报错-You need to maintain catalog 3 (Usage Decisions) in Customi ...
- cluster maintain manager Software群集管理软件
cluster maintain manager Software群集管理软件 1,ocfs2 2,crmsh(cluster management shell,)==crm shell [pacem ...
- android toolchain maintain team
android toolchain maintain team
- [CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数
18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the ...
- Oracle 11.2.0.3 [INS-41112] Specified network interface doesnt maintain connectivity across cluster
安装Oracle 11.2.0.3 的RAC,在Grid 安装时报错: [INS-41112]Specified network interface doesnt maintain connectiv ...
- Failed to maintain projects LRU cache for dir *********
打开JetBrains系软件时(包括Android Studio),右下角有个红色小叹号总报错,强迫症看了很不爽,点开后详细内容: Failed to maintain projects LRU ca ...
- [转]Maintain Control State in ASP.NET 2.0
[转自http://www.vbdotnetheaven.com] Maintain Control State in ASP.NET 2.0 By Scott Lysle September 15 ...
- MAINTAIN翻译软件测试,maintain是什么意思_maintain的翻译_音标_读音_用法_例句_爱词霸在线词典...
全部 四级 六级 高考 考研 Their life was much more comfortable than that of todayIt was helpful to maintaining ...
最新文章
- Photoshop CS6软件安装教程
- 2018/Province_Java_C/1/哪天返回
- 荣耀v10Android9新功能,荣耀10、荣耀V10开启安卓9.0内测 日常领跑行业
- python selenium 环境_配置Python Selenium环境
- Notification Swift 3 0
- SCCM 2012系列2 服务器准备下
- 我敢说,你的登录接口肯定不安全
- EMVI5.3 将数据信息改为投影坐标系
- AC日记——Flag Codeforces 16a
- copy的过去式_[copy是什么意思中文翻译成]copy是什么意思中文翻译
- FPGA那些事(黑金动力社区)-笔记
- jsd2205-csmall-passport(Day13)
- 以太坊性能优化:分片技术、雷电网络、Casper-下一代以太坊共识协议
- Notification 加入本地的声音文件
- remote: Repository not found. fatal: repository ‘xxxxxxx‘ not found
- SQL注入漏洞-GET注入
- matlab使用CVX求解优化问题时,如果变量搜索空间过大,导致求解的数值解相当不准确,通过变量替换,缩小搜索空间
- pandas转json去掉index索引
- VS2017安装Spy++工具
- 安装Windows10系统---制作U盘启动盘的方式安装
热门文章
- 在UCOSIII基础上对STM32F407ZGT6移植LWIP2.1.3
- 用git来下载github项目代码
- [学习笔记] Pell方程
- 趣头条确定发行价为7美元:今日上市
- Ubuntu下Java使用pdfbox将pdf转换为图片的方法及问题
- tp5 php post方式提交无提示,使用TP5框架时,代码提交到GitHub,无法提交thinkphp核心库...
- 纯英文外刊原刊在哪看?怎么看?
- 全球区块链第26周看点 | G20中方发言人:中国在数字货币、加密资产等领域做得较好...
- 4款令人骄傲的国产软件,每回买新电脑必装
- C++中的RTTI(转)