题目链接:点击打开链接

题意:

给定n个点 m条边的无向图 须要在图里添加p条边 使得图最后连通分量数为q

问是否可行,不可行输出NO

可行输出YES,并输出加入的p条边。

set走起。。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define N 123456 #define ll __int64
ll n,m,p,q;
struct Edge{
ll from, to, dis;
}edge[N*2];
ll edgenum;
void add(ll u,ll v,ll dis){
Edge E={u,v,dis};
edge[edgenum++] = E;
}
ll f[N];
ll find(ll x){return x==f[x]?x:f[x]=find(f[x]);}
void Union(ll x,ll y){
ll fx = find(x), fy = find(y);
if(fx==fy)return ;
if(fx<fy)swap(fx,fy);
f[fx]=fy;
}
set<ll>myset;
set<ll>::iterator pp;
ll siz[N];
vector<int>L,R;
struct node{
ll fa, val;
bool operator<(const node&x)const{
if(x.val==val)return x.fa<fa;
return x.val>val;
}
node(ll x=0,ll y = 0):fa(x),val(y){}
};
set<node>hehe;
set<node>::iterator dd;
void init(){
hehe.clear();
L.clear(); R.clear();
memset(siz, 0, sizeof siz);
myset.clear();
for(ll i = 1; i <= n; i++)f[i]=i;
edgenum = 0;
} void go(){
dd = hehe.begin();
node x = *dd;
hehe.erase(dd);
dd = hehe.begin();
node y = *dd;
hehe.erase(dd);
Union(x.fa,y.fa);
ll now = min((ll)1000000000, x.val+y.val+1);
node z = node(find(x.fa),x.val+y.val+now);
hehe.insert(z);
L.push_back(x.fa); R.push_back(y.fa);
add(x.fa,y.fa,1);
}
int main(){
ll i, j, u, v, d;
while(~scanf("%I64d %I64d %I64d %I64d",&n,&m,&p,&q)){
init();
while(m--){
scanf("%I64d %I64d %I64d",&u,&v,&d);
add(u,v,d);
Union(u,v);
}
for(i = 1; i <= n; i++)find(i);
for(i = 1; i <= n; i++)myset.insert(f[i]);
for(i = 0; i < edgenum; i++){
siz[f[edge[i].from]]+=edge[i].dis;
}
if(myset.size()<q){puts("NO");continue;}
if(myset.size()==q)
{
if(p && edgenum==0)puts("NO");
else
{
puts("YES");
while(p--){
cout<<edge[0].from<<" "<<edge[0].to<<endl;
}
}
continue;
}
ll ned = myset.size()-q;
if(ned>p){puts("NO");continue;}
p-=ned; for(pp=myset.begin(); pp!=myset.end(); pp++)hehe.insert(node(*pp,siz[*pp]));
while(ned--)go(); puts("YES");
for(i = 0; i < L.size(); i++)cout<<L[i]<<" "<<R[i]<<endl;
while(p--)
cout<<edge[0].from<<" "<<edge[0].to<<endl;
}
return 0;
}

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

  1. Codeforces 362D Fools and Foolproof Roads

    Fools and Foolproof Roads 并查集瞎搞搞就行, 有点小坑点. #include<bits/stdc++.h> #define LL long long #defin ...

  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 1491G - Switch and Flip(构造题)

    Codeforces 题目传送门 & 洛谷题目传送门 obviously,难度高一点的构造题对我来说都是不可做题 首先考虑将排列拆成一个个置换环,也就是 \(\forall i\) 连边 \( ...

  4. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

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

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

  6. B - Save the problem! CodeForces - 867B 构造题

    B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...

  7. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  8. CodeForces 297C Splitting the Uniqueness (脑补构造题)

    题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...

  9. Codeforces 989 P循环节01构造 ABCD连通块构造 思维对云遮月参考系坐标轴转换

    A 直接判存不存在连续的三个包含A,B,C就行 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a ...

随机推荐

  1. Objective-C学习篇01—类的声明与实现

    Objective-C,是美国人布莱德·确斯(Brad Cox)于 1980 年年初发明的一 种程序设计语言,其与同时代的 C++ 一样,都是在 C 的基础上加入面向对象特性扩充而成的.Objecti ...

  2. Swift - 21 - 字典实战和UIKit初探

    //: Playground - noun: a place where people can play import UIKit // 数据源 let colors = [ "Air Fo ...

  3. hibernate_validator_04

    对象图--个人觉得就是关联验证 ean Validation API不仅能够用来校验单个的实例对象,还能够用来校验完整的对象图.要使用这个功能,只需要在一个有关联关系的字段或者属性上标注 @Valid ...

  4. IE layout详解

    引言: Internet Explorer 中有很多奇怪的渲染问题可以给他一个”layout”得到解决,John Gallant 和 Holly Bergevin把他归类为“dimensional b ...

  5. jQuery 使用 jQuery UI 部件工厂编写带状态的插件(翻译)

    首先,我们要创建一个progress bar,它只允许我们简单的设置进度值.正如我们接下来将要看到的,我们需要通过调用 jQuery.widget 及其两个参数来实现这一操作,这两个参数分别是:将要创 ...

  6. javascript权威指南学习笔记1

    打开这本书,进入到javascript的世界.以前都是看各种视频,感觉什么收获也没有,反而弄得脑袋混乱,希望能够按照这本书的节奏掌握javascript这门语言,为我的前端学习打下基础. 学习前准备: ...

  7. 通过 Xftp5 管理 centOS 7 文件

    一. 在安装好了centOS 7 的服务上,打开终端 运行 ip -s addr 命令 获取服务的IP地址 [root@localhost ~]# ip -s addr1: lo: <LOOPB ...

  8. jquery easy ui 学习 (1)Basic Window

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. C语言对数组取地址

    #include <stdio.h> main() { ] = {,,,,}; printf("a=%p\n" , a); printf("a=%p\n&qu ...

  10. Aptana Studio 快捷键

    窗口 Ctrl+ Shift +L 调出快捷键提示 Ctrl+ W 关闭当前标签窗口 Ctrl+ Shift + W 关闭当前标签窗口 Ctrl+ F6 (或者是Aptana的Ctrl+Tab )下一 ...