题目链接:点击打开链接

题意:

给定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. solr可用于集群的搜索 【转】

    一. SOLR搭建企业搜索平台 运行环境: 运行容器:Tomcat6.0.20 Solr版本:apache-solr-1.4.0 分词器:mmseg4j-1.6.2  词库:sogou-dic 准备工 ...

  2. MySQL 插入数据

    MySQL 插入数据 MySQL 表中使用 INSERT INTO SQL语句来插入数据. 你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 语法 以下 ...

  3. 解决tomcat占用8080端口问题

    在dos下,输入  netstat   -ano|findstr  8080 //说明:查看占用8080端口的进程 显示占用端口的进程 askkill  /pid  44464  /f  //说明,运 ...

  4. java记事本

    新知识点 1.撤销 textArea添加一个实现监听接口的类(添加了之后可以一直监视着添加的删除的情况,以便来撤销 textArea.getDocument().addUndoableEditList ...

  5. I/O复用-select模型

    IO复用: I/O复用使得程序可以同时监听多个文件描述符,这对提高程序的性能至关重要.例如TCP服务器要同时处理监听socket和连接socket,客户端要同时处理用户输入和网络连接. Linux下实 ...

  6. python正则表达式入门

    基本概念 使用正则表达式需要import re 表达式前加r避免转义 \d代表数字,\s代表空白字符,\w代表字母+数字. .代表任意单个字符 {m,n}代表前面字符至少出现m次,最多出现n次. (x ...

  7. 如果使用的是orm,是否还需要关系索引

    简而言之:是的,仍然需要理解索引,即使是使用对象关系映射(ORM)工具. ORM工具能够产生符合逻辑的,合法的查询(多数的时候),除非只是生成非常基本的查询(例如仅是根据主键查询的),否则它很难生成适 ...

  8. 浅谈intval()函数用法

    <? } } 总结:intval()函数功能1.参数一定是数字否则会报错,2.如果是数字那一定是整数,如果有小点,那会省略掉,3,强调参数可以有“-”值.4.参数第一位不应为0开头,不然会自动转 ...

  9. 使用Code::Blocks配置Python编译环境

    1.先在CodeBlock中新建C或C++工程. CodeBlock新建工程步骤:File——New——Project——Console applications——C或C++都可——Project所 ...

  10. SCJP_104——题目分析(1)

    1.1) public class ReturnIt{2) returnType methodA(byte x, double y){3) return (short)x/y*2;4) }5) }wh ...