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 ...
随机推荐
- configSections(配置文件)
转载:http://www.cnblogs.com/jhxk/articles/1609182.html 由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, ...
- .NET Framework 中的类型系统的两个基本点
它支持继承原则. 类型可从称为基类型的其他类型派生. 派生类型继承基类型的方法.属性和其他成员(存在一些限制). 之后,基类型可从某些其他类型派生,这种情况下,派生类型继承其层次结构中这两个基类型的成 ...
- EXPDP IMPDP 知识总结
Data Pump Export ATTACH Default: job currently in the user's schema, if there is only one Purpose(目的 ...
- Oracle11G 数据库 expdp、impdp使用示例
expdp 备份数据库.expdp/impdp简单测试 操作系统层面创建目录 [root@Oracle11g ~]# mkdir -p /home/oracle/db_back/ 修改目录的所属用户. ...
- iOS面试题整理(一)
代码规范 这是一个重点考察项,曾经在微博上发过一个风格纠错题: 也曾在面试时让人当场改过,槽点不少,能够有 10 处以上修改的就基本达到标准了(处女座的人在这方面表现都很优秀 一个区分度很大的面试题 ...
- PHP MySQL 预处理语句
PHP MySQL 预处理语句 预处理语句对于防止 MySQL 注入是非常有用的. 预处理语句及绑定参数 预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. 预处理语句的工作原理如下: 预 ...
- js apply
1.作用 函数的apply方法的作用与call方法类似,也是改变this指向,然后再调用该函数.唯一的区别就是,它接收一个数组作为函数执行时的参数 Fn.apply(obj, [arg1, arg2, ...
- css3之box-sizing
css盒子模型中包括几种重要的属性,包括margin.border.padding以及content.但浏览器对其盒子模型的解释则有所不痛,启用标准模式还是IE(怪)模式是与当前页面的文档声明相关的. ...
- (转载)图解Linux系统的系统架构
我以下图为基础,说明Linux的架构(architecture).(该图参考<Advanced Programming in Unix Environment>) 最内层是硬件,最外层是用 ...
- 关于ligerui和其他前端脚本的学习方法(适用于自己)
特别是看别人的源代码(来源于自己看的那个cms系统),比如ligerui,别人用的juery和ligerui结合的很灵活,比如下面一段代码 var itemiframe = "#framec ...