Codeforces 362D 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的更多相关文章
- Codeforces 362D Fools and Foolproof Roads 构造题
题目链接:点击打开链接 题意: 给定n个点 m条边的无向图 须要在图里添加p条边 使得图最后连通分量数为q 问是否可行,不可行输出NO 可行输出YES,并输出加入的p条边. set走起.. #incl ...
- 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. ...
- Codeforces 191C Fools and Roads(树链拆分)
题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Codeforces Gym 100338C C - Important Roads tarjan
C - Important RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- Codeforces #369 div2 D.Directed Roads
D. Directed Roads time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...
- CodeForces #369 div2 D Directed Roads DFS
题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...
- 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 ...
随机推荐
- MariaDB基础详解
数据库结构模型分类 1.层次模型 2.网状模型 3.关系模型 关系模型的组成部分 二维关系 表 row column 索引 index 视图 view (只包含固定字段,不包含其他字段) 关系型数据库 ...
- 【原创】大叔经验分享(41)hdfs开启kerberos之后报错Encryption type AES256 CTS mode with HMAC SHA1-96 is not supported/enabled
hdfs开启kerberos之后,namenode报错,连不上journalnode 2019-03-15 18:54:46,504 WARN org.apache.hadoop.security.U ...
- PHP中empty,isset,is_null的区别
isset 判断变量是否已存在 empty 判断变量是否为空或为0 is_null 判断变量是否为NULL 仅作为记录使用. 参考链接:http://www.jb51.net/article/6903 ...
- vue----less引用
1.安装less. npm install less less-loader --save-dev 2.修改webpack.config.js { test: /\.less$/, use:[ 'st ...
- Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较
.Net 4.5 WebSocket Server Running on Windows 7? Net 4.5 WebSocket Server 可以运行在 Windows 7,但是Net 4.5的 ...
- 《JavaWeb从入门到精通》(明日科技,清华大学出版社)
<JavaWeb从入门到精通>(明日科技,清华大学出版社)
- Android “Command” from work summary
总结一下Android中的命令. 一.adb 与 shell ADB的全称为Android Debug Bridge(调试桥).是一个适用命令行工具,用来与模拟器实例或链接的Android设备进行通信 ...
- embed标签详解
HTML-embed标签详解 Embed(一).基本语法:embed src=url说明:embed可以用来插入各种多媒体,格式可以是 Midi.Wav.AIFF.AU.MP3等等, Net ...
- 《剑指offer》从尾到头打印链表
本题来自<剑指offer> 从尾到头打印链表 题目: 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 思路: 方案一:首先遍历到尾部,然后从尾部进行到头值进行操作,后进先 ...
- laravel 配置设置
public function updateRegisterSetting(Request $request, Configuration $config) { $conf = $request-&g ...