博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 362D Fools and Foolproof Roads 构造题
阅读量:7234 次
发布时间:2019-06-29

本文共 1685 字,大约阅读时间需要 5 分钟。

题目链接:

题意:

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

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

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

set走起。。

#include
#include
#include
#include
#include
#include
using namespace std;#define N 123456#define ll __int64ll 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
myset;set
::iterator pp;ll siz[N];vector
L,R;struct node{ ll fa, val; bool operator<(const node&x)const{ if(x.val==val)return x.fa
val; } node(ll x=0,ll y = 0):fa(x),val(y){}};set
hehe;set
::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()
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<
<<" "<
<

转载地址:http://kplfm.baihongyu.com/

你可能感兴趣的文章
iOS-高性能
查看>>
无所遁形
查看>>
动态规划(2)——01背包
查看>>
使用递归遍历并转换树形数据(以 TypeScript 为例)
查看>>
css制作动画
查看>>
大型分布式网站的思考(一):大型网站发展历程
查看>>
一些ES6新姿势
查看>>
Serverless 风格微服务的持续交付(上):架构案例
查看>>
SpringCloud(第 047 篇)注解式Async配置异步任务
查看>>
移动端调试篇
查看>>
时间的符号
查看>>
Debian8 + Flask + Nginx + uWSGI + uWSGI Emperor 基本配置文件注意事项
查看>>
iOS必读 - 收藏集 - 掘金
查看>>
对javascript事件的深度理解
查看>>
《javascript高级程序设计》笔记:Number类型
查看>>
Vue全家桶仿闲鱼移动端App
查看>>
Redis 有序集合
查看>>
mobile调试方法
查看>>
elasticsearch 爬坑记
查看>>
Fundebug能够捕获这些BUG
查看>>