[Tkey] Transport Nekomusume II
考虑定义一条有向边 \(u\rightarrow v\) 的意义为 \(u\) 把窝让给了 \(v\),那么每个点一定入度为 \(1\),所有的边会形成一个外向基环树森林。
贪心地把猫娘按照权值从大到小排序,每个猫娘看成一条无向边,那么可行的方案一定会形成一个基环树森林。
用并查集维护所有窝组成的基环树,用标记来记录一个并查集代表的集合为基环树还是树,然后考虑选择一条边的方法:
如果合并的两个点属于同一个并查集:
如果这个并查集是一棵树,那么可以选,并标记为基环树;
否则不选。
如果合并的两个点不属于一个并查集:
如果合并两棵树,那么可以选,标记为树;
如果合并一棵树,一棵基环树,那么可以选,标记为基环树;
否则不选。
#include<bits/stdc++.h>
using namespace std;
const int N=200001;
struct Edge{
int u,v,w;
bool operator <(const Edge &a)const{return w>a.w;}
}e[N];
int fa[N],d[N];
int n,m,sum;
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
cin>>m>>n;
for(int i=1;i<=n;++i){
fa[i]=i;
d[i]=1;
}
for(int i=1;i<=m;++i){
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
}
sort(e+1,e+m+1);
for(int i=1;i<=m;++i){
int x=find(e[i].u),y=find(e[i].v);
if(x!=y&&(d[x]||d[y])) fa[x]=y,sum+=e[i].w,d[y]=d[x]&d[y];
else if(x==y&&d[x]) d[x]=0,sum+=e[i].w;
}
printf("%d\n",sum);
}
[Tkey] Transport Nekomusume II的更多相关文章
- ZOJ 1655 FZU 1125 Transport Goods
迪杰斯特拉最短路径. 1.every city must wait till all the goods arrive, and then transport the arriving goods t ...
- 2020: [Usaco2010 Jan]Buying Feed, II
2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 220 Solved: 162[ ...
- 微软职位内部推荐-SW Engineer II for Azure Network
微软近期Open的职位: Software Engineer II The world is moving to cloud computing. Microsoft is betting Windo ...
- POJ-2336 Ferry Loading II(简单DP)
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...
- HNU 13411 Reverse a Road II(最大流+BFS)经典
Reverse a Road II Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB Total submit ...
- poj 2336 Ferry Loading II ( 【贪心】 )
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3704 Accepted: 1884 ...
- poj-2336 Ferry Loading II(dp)
题目链接: Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3946 Accepted: ...
- TOJ 2419: Ferry Loading II
2419: Ferry Loading II Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Subm ...
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...
- USACO Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II 洛谷传送门 JDOJ 2671: USACO 2010 Jan Silver 2.Buying Feed, II ...
随机推荐
- 云端IDE如何重定义开发体验
豆包 MarsCode 是一个集成了AI功能的编程助手和云端IDE,旨在提高开发效率和质量.它支持多种编程语言和IDE,提供智能代码补全.代码解释.单元测试生成和问题修复等功能,同时具备AI对话视图和 ...
- php 选择驱动写法
在 ThinkPHP 5.1 中,若要根据配置文件 sms.conf 中的设置在不同的短信渠道之间进行切换,可以采用以下步骤: 第一步:定义接口 首先,创建一个接口,这个接口将由所有短信渠道类实现.这 ...
- Fiddler使用界面介绍-底部状态栏
底部状态栏 1.Capturing抓包状态 Capturing:Fiddler正在抓包 空白:Fiddler停止抓包 2.All Processes抓取进程类型 All Processes:抓取所有进 ...
- 【Web】实现页面自动刷新的功能
技术发现自: https://www.bilibili.com/video/BV14v411b7JS?p=8 摘要自CSDN帖子: https://blog.csdn.net/senbar/artic ...
- 【Layui】05 进度条 Progress
文档地址: https://www.layui.com/doc/element/progress.html 演示案例: <div class="layui-progress" ...
- 全球2023年自然科学指数(Nature Index),各单位排名表
地址: https://www.nature.com/nature-index/annual-tables/2023/institution/all/all/global 自然科学指数(Nature ...
- Ubuntu/Linux系统中的multi-user.target
相关: https://www.cnblogs.com/devilmaycry812839668/p/17999041 multi-user.target 是 Linux 系统中 systemd 的一 ...
- 中国特供阉割版4090D建议安装最新驱动,据说不然的话会报error:4090和4090D对比
资料来源: https://www.bilibili.com/video/BV1oa4y127fG/?spm_id_from=333.999.0.0&vd_source=f1d0f27367a ...
- Ubuntu18.04终端alacritty安装及配置
想在Ubuntu上安装alacritty终端,发现不能直接安装,在网上找到教程: Linux上安装使用最快的 GPU 加速的终端仿真器Alacritty 根据教程成功在Ubuntu18.04桌面系统上 ...
- 链接池偶尔报错:HikariPool-1 - Connection is not available, request timed out after 39985ms.
1.背景 线上服务器偶尔报错如下: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.e ...