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. 关于socket.io获取客户端真实IP地址

    1 前言 由于使用了CDN加速,导致了socket.handshake.address拿到值都是服务器的,而没有使用CDN加速时,可以拿到客户端真实IP. 2 代码 if(socket.handsha ...

  2. Windws Server 2008 R2 WEB环境配置之IIS7/IIS7.5+FastCGI+PHP 5.6.4+MYSQL+phpMyAdmin

    本篇为WEB环境配置的汇总篇,其中PHP以FASTCGI方式来运行,这种方式性能更高.经过配置后,我们的服务器将同时可以运行PHP和.NET的程序,属称全能服务器.所有配置可以根据自身实际需要进行增减 ...

  3. linux指定只显示(只打印)文件中的某几行(中间几行)

    [一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1000 [二]显示1000行到3000行 cat ...

  4. Android 常用正则表达式

    前言 闲扯一下,已经有好久没更新博客了,记得上一篇博客的更新时间为 2017-05-12 15:20.截止到今天,超过一百天没更新了. 这篇博客的内容大多数是从别的博客摘抄过来的,写这篇博客的目的主要 ...

  5. hash·余数hash和一致性hash

    网站的伸缩性架构中,分布式的设计是现在的基本应用. 在memcached的分布式架构中,key-value缓存的命中通常采用分布式的算法 一.余数Hash     简单的路由算法可以使用余数Hash: ...

  6. JS和Jquery获取和修改label的值的示例代码

    abel标签在JS和Jquery中使用不能像其他标签一样用value获取它的值,下面有个不错的示例,希望大家可以学习下 来源:  <  JS和Jquery获取和修改label的值的示例代码  & ...

  7. vuejs中使用echarts

    <style scoped> .content { /*自行添加样式即可*/ } #main { /*需要制定具体高度,以px为单位*/ height: 400px; } </sty ...

  8. mysql 安装问题一:由于找不到MSVCR120.dll,无法继续执行代码.重新安装程序可能会解决此问题。

    这种错误是由于未安装  vcredist  引起的 下载  vcredist  地址:https://www.microsoft.com/zh-CN/download/details.aspx?id= ...

  9. Layers Of Caffe

    本文试图描述构建一个网络结构的layers,可以用prototxt脚本直接写,也可以用python接口实现. 最简单的神经网络包含但不限于以下四部分: 数据层(Data): Data.ImageDat ...

  10. java测试

    //信1705-1 20173527 刘津鑫package money;import java.io.IOException;import java.io.Serializable;import ja ...