Fools and Foolproof Roads

并查集瞎搞搞就行, 有点小坑点。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m, p, q, num, fa[N], U = -, V = -;
LL cost[N];
vector<PII> vc; int getRoot(int x) {
return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
} int main() {
scanf("%d%d%d%d", &n, &m, &p, &q);
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i <= m; i++) {
int u, v, w; scanf("%d%d%d", &u, &v, &w);
U = u; V = v;
int x = getRoot(u);
int y = getRoot(v);
if(x == y) {
cost[x] += w;
} else {
fa[x] = y;
cost[y] += cost[x] + w;
}
}
priority_queue<PLI, vector<PLI>, greater<PLI> > que;
for(int i = ; i <= n; i++)
if(getRoot(i) == i) que.push(mk(cost[i], i)), num++;
int need = num - q;
if(need < || need > p) {
puts("NO");
return ;
}
while(need--) {
p--;
int u = que.top().se; que.pop();
int v = que.top().se; que.pop();
int x = getRoot(u), y = getRoot(v);
vc.push_back(mk(u, v));
U = u, V = v;
LL ret = min(1000000000ll, cost[x] + cost[y] + );
cost[y] += cost[x] + ret;
que.push(mk(cost[y], y));
}
if(p && U == -) {
puts("NO");
return ;
}
while(p--) {
vc.push_back(mk(U, V));
}
puts("YES");
for(auto& t : vc) printf("%d %d\n", t.fi, t.se);
return ;
} /*
*/

Codeforces 362D Fools and Foolproof Roads的更多相关文章

  1. Codeforces 362D Fools and Foolproof Roads 构造题

    题目链接:点击打开链接 题意: 给定n个点 m条边的无向图 须要在图里添加p条边 使得图最后连通分量数为q 问是否可行,不可行输出NO 可行输出YES,并输出加入的p条边. set走起.. #incl ...

  2. Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列

    D. Fools and Foolproof Roads   You must have heard all about the Foolland on your Geography lessons. ...

  3. Codeforces 191C Fools and Roads(树链拆分)

    题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...

  4. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  5. Codeforces Gym 100338C C - Important Roads tarjan

    C - Important RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  6. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  7. Codeforces #369 div2 D.Directed Roads

    D. Directed Roads time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

  8. CodeForces #369 div2 D Directed Roads DFS

    题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...

  9. codeforces 427 div.2 F. Roads in the Kingdom

    F. Roads in the Kingdom time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. OpenStack实践系列⑤网络服务Neutron

    OpenStack实践系列⑤网络服务Neutron 3.8 Neturn 服务部署 注册neutron服务 [root@node1 ~]# source admin-openrc.sh [root@n ...

  2. mybatis:自动分页插件

    项目地址:https://github.com/pagehelper/pagehelper-spring-boot 简单使用: 1.在pom文件中添加 <dependency> <g ...

  3. est是基于less的样式工具库

    网址:http://ecomfe.github.io/est/ 此处HTML中如果直接引用less就会报错! est 如果直接引用less和js 是会报错的 样式规范:https://www.zybu ...

  4. STM32应用实例十一:基于SPI和AD7192的数据采集

    在开发臭氧发生器的时,我们需要一个高分辨率的AD采集,于是选择了AD7192,选择这款ADC的原因比较简单.首先它是24位的符合我们的精度要求:其次它自带时钟,便于节省空间:第三他又4路单端或2路差分 ...

  5. LoadRunner学习笔记

    什么是性能测试: 简单说,功能测试是软件是否能用,性能测试是看软件好不好用: 性能测试的含义,大体来讲就是通过自动化的手段,模拟生产运行的业务压力或者相应的场景,来测试协同系统是否满足生产需要. 性能 ...

  6. kafka架构浅显理解

    Kafka的概念: 1. AMQP协议 Advanced Message Queuing Protocol (高级消息队列协议) The Advanced Message Queuing Protoc ...

  7. LeetCode(72):编辑距离

    Hard! 题目描述: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换 ...

  8. 【python】正则替换

    正则替换可以使用函数 例如:替换字符串中所有#1.2.3.4#格式中的数字为0 import re def replace(x): def _replace(matched): m = matched ...

  9. NMT 机器翻译

    本文近期学习NMT相关知识,学习大佬资料,汇总便于后期复习用,有问题,欢迎斧正. 目录 RNN Seq2Seq Attention Seq2Seq + Attention Transformer Tr ...

  10. laravel zh-CN

    位置: 调用: