Codeforces 362D Fools and Foolproof Roads 构造题
题目链接:点击打开链接
题意:
给定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 构造题的更多相关文章
- Codeforces 362D Fools and Foolproof Roads
Fools and Foolproof Roads 并查集瞎搞搞就行, 有点小坑点. #include<bits/stdc++.h> #define LL long long #defin ...
- 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 1491G - Switch and Flip(构造题)
Codeforces 题目传送门 & 洛谷题目传送门 obviously,难度高一点的构造题对我来说都是不可做题 首先考虑将排列拆成一个个置换环,也就是 \(\forall i\) 连边 \( ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces 191C Fools and Roads(树链拆分)
题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...
- B - Save the problem! CodeForces - 867B 构造题
B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...
- Codeforces 482 - Diverse Permutation 构造题
这是一道蛮基础的构造题. - k +(k - 1) -(k - 2) 1 + k , 1 , k , 2, ....... ...
- CodeForces 297C Splitting the Uniqueness (脑补构造题)
题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...
- Codeforces 989 P循环节01构造 ABCD连通块构造 思维对云遮月参考系坐标轴转换
A 直接判存不存在连续的三个包含A,B,C就行 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a ...
随机推荐
- Android开发手记(15) 拨打电话和收发短信
1.Intent简介 Android组价之间的通信,由Intent来协助完成.Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到 ...
- SOA与EBS集成_Package 调用维护
介绍SOA Suite与EBS集成成,使用PACKAGE调用集成的组合程序的运维方法: 1.打开组合程序,在oracle adapter上右键,选择edit,在配置向导中跳转到第五步,确认组合程序调用 ...
- DontDestroyOnLoad
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_DontDestroyOnLoad.html public stati ...
- 【USACO 1.3.3】回文串
[題目描述] 据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文.你的工作就是去寻找这些牛制造的奇观(最棒的回文). 在寻找回文时不用理睬那些标点符号.空 ...
- Linux硬盘命名和安装分区
硬盘命名: 硬盘命名基于文件,一般有如下文件方式: /dev/hda1 /dev/sdb3 具体含义如下: /dev:是所有设备文件存放的目录. hd和sd:他们是区别的前两个字母,代表该分区所在的设 ...
- Ecshop 数据库操作方法getRow、getAll、getOne区别
ECShop没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现.这样做的好处是实现非常轻量,大大减小了分发包的文件大小.另外,当网站需要做memcached缓存时,也可以 ...
- apache 修改端口号 修改根目录 建立多个网站
修改apache端口号选择Apache下的httpd.conf,查找:Listen,你会看到 #Listen 12.34.56.78:80Listen 80把80改成90,保存就好了 修改WampSe ...
- iOS开发——OC篇&常用关键字的使用与区别
copy,assign,strong,retain,weak,readonly,readwrite,nonatomic,atomic,unsafe_unretained的使用与区别 最近在学习iOS的 ...
- 实验四:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 如果我写的不好或者有误的地方请留言 ...
- IOS--UITextView 多行文本的输入和显示 的使用方法详细
IOS--UITextView 多行文本的输入和显示 的使用方法详细 // UITextView的常用方法 主要用来输入和显示多行文本信息 UITextView *oneTextView = [[ ...