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 ...
随机推荐
- Sublime Text 3安装Package Control快速建立html5和xhtml文档
Sublime Text 3安装Package Control快速建立html5和xhtml文档 先关闭Sublime text 3:第1步:下载sublime_package_control-mas ...
- MariaDB主从备份记录
一.预期效果: 环境: centos 6.5 mariadb 10.0.14 (mysql -V) 主服务器:192.168.5.206 从服务器:192.168.5.207 主服务器数据 ...
- Js -----后台json数据,前端生成下载text文件
需要引入 <script src="/assets/libs/single_file/jquery.min.js"></script> <script ...
- MS SQL Server 建库建表
CREATE DATABASE Test use Test --创建用户类型表CREATE TABLE UserType ( ID INT NOT NULL identity(1,1) primary ...
- 【原创】大数据基础之Kerberos(2)hive impala hdfs访问
1 hive # kadmin.local -q 'ktadd -k /tmp/hive3.keytab -norandkey hive/server03@TEST.COM'# kinit -kt / ...
- JGroups
JGroups Developer(s) Bela Ban Stable release 4.0.10.Final / February 1, 2018 Written in Java Operati ...
- hive学习05 参数设置
001参数设置 hive执行命令的本质是mapreduce,当然也可以作为关系型数据库进行查询 --设置一个job有多少个reducer处理,依据多少的是文件的大小,默认1G set hive.exe ...
- 使用应用链接来连接 Jira 和 Confluence 6
请参考 Linking to Another Application 页面中的内容来设置如何让 Confluence 连接到你的 Jira 应用,这个过程只需要一次就可以了. 如果你计划使用 Jira ...
- UINavigationController 、navigationBar 、 UINavigationItem、UIBarButtonItem之间的关系
- LeetCode(115):不同的子序列
Hard! 题目描述: 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字 ...