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 ...
随机推荐
- ASP.NET实现文件下载
转:http://blog.csdn.net/codeshark/article/details/2473664 方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流, ...
- Linux sed命令删除指定行
一.删除包含匹配字符串的行## 删除包含baidu.com的所有行sed -i '/baidu.com/d' domain.file 二.删除匹配行及后所有行## 删除匹配20160229的行及后面所 ...
- CompareValidator ASP控件
定义和用法 CompareValidator 控件用于将由用户输入到输入控件的值与输入到其他输入控件的值或常数值进行比较. 注释:如果输入控件为空,则不会调用任何验证函数,并且验证将成功.使用 Req ...
- [转]MySQL导入和导出SQL脚本
首先,使用mysqldump命令的前提是,在Cmd中进入mysql安装目录下的bin目录下,才可以使用该命令.我的mysql安装在E:盘,所以,首先进入bin目录下:E:/Program Files/ ...
- UVA 10131 - Is Bigger Smarter? (动态规划)
Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. ...
- 理解javascript之 对象
大纲: 1.介绍attribute property的异同,翻译自http://javascript.info/tutorial/attributes-and-custom-properties#pr ...
- bash shell学习-shell script基础 (笔记)
A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版) Linux ...
- ActionResult派生类
类名 抽象类 父类 功能 ContentResult 根据内容的类型和编码,数据内容. EmptyResult 空方法. FileResult abstract 写入文件内容,具体的写入方式在派生类中 ...
- PHP分页详细讲解
网上有好多PHP分页的类,但我们要弄明白PHP分页原理才可以学到知识,今天我就带你学制作PHP分页. 1.前言分页显示是一种非常常见的浏览和显示大量数据的方法,属于web编程中最常处理的事件之 ...
- Python学习笔记四--字典与集合
字典是Python中唯一的映射类型.所谓映射即指该数据类型包含哈希值(key)和与之对应的值(value)的序列.字典是可变类型.字典中的数据是无序排列的. 4.1.1字典的创建及赋值 dict1={ ...