cf 605B B. Lazy Student 构造 好题
题意:
一个n个节点的图,有m条边,已知这个图的一个mst
现在如果我们知道这个图的m条边,和知道mst的n-1条边是哪些,问能不能构造出一个满足条件的图
思路:排序+构造
数组deg[i]表示节点i此时还可以1~i-1中的多少条边相连
由于:deg[i]=i-1时,最低可以和1连接,=i-2时,最低可以和2连接
所以如果我们知道i和deg[i]的话,我们就知道这一次i要和哪一个节点连接,
就可以我构造出来了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdlib> #define fir first
#define sec second using namespace std; const int MAXN = + ; struct Edge
{
int is,w,id;
};
Edge edge[MAXN];
int deg[MAXN];
pair<int,int>res[MAXN]; bool cmp(Edge x,Edge y)
{
if(x.w == y.w)
return x.is > y.is;
return x.w < y.w;
} inline int lb(int x)
{
return x&(-x);
} void update(int x,int add,int n)
{
while(x <= n){
deg[x] += add;
x += lb(x);
}
} int query(int x)
{
int ret = ;
while(x > ){
ret += deg[x];
x -= lb(x);
}
return ret;
} pair<int,int> find_edge(int num,int n)
{
int l=,r=num;
while(r-l > ){
int mid=(l+r)>>;
if(query(mid) <= )
l = mid;
else
r = mid;
}
int tmp;
if(query(r) == )
tmp = r;
else
tmp = l;
tmp++;
int tmp_deg = query(tmp);
pair<int,int> ret = make_pair(tmp-tmp_deg,tmp);
update(tmp,-,n);
return ret;
} void solve(int n,int m)
{
memset(deg,,sizeof deg);
int num = ;
int sum = ;
bool flag = true;
sort(edge+,edge+m+,cmp); for(int i=;i<=m;i++){
if(edge[i].is){
num++;
update(num,num-,n);
//for(int j=1;j<=n;j++){
// printf("deg[%d] = %d\n",j,query(j) - query(j-1));
//}
sum += num - ;
res[edge[i].id] = make_pair(,num);
}
else{
if(sum <= ){
flag = false;
break;
}
else{
sum--;
res[edge[i].id] = find_edge(num,n);
}
}
}
if(!flag)
puts("-1");
else{
for(int i=;i<=m;i++){
printf("%d %d\n",res[i].fir,res[i].sec);
}
}
return ;
} int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d %d",&edge[i].w,&edge[i].is);
edge[i].id = i;
} solve(n,m); return ;
}
cf 605B B. Lazy Student 构造 好题的更多相关文章
- Codeforces Round #335 (Div. 2) D. Lazy Student 构造
D. Lazy Student Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/606/probl ...
- 605B. Lazy Student(codeforces Round 335)
B. Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- CF#335 Lazy Student
Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造
题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...
- Codeforces Round #335 (Div. 2) D. Lazy Student 贪心
D. Lazy Student Student Vladislav came to his programming exam completely unprepared as usual. He ...
- cf 443 D. Teams Formation](细节模拟题)
cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...
- 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...
- CF C. Vladik and fractions——构造题
题目 构造一组 $x, y, z$,使得对于给定的 $n$,满足 $\frac{1}{x} + \frac{1}{y} + \frac{1}{z} = \frac{2}{n}$. 分析: 样例二已 ...
- CodeForces 605B Lazy Student
构造.对边的权值排序,权值一样的话,在MST中的边排到前面,否则权值小的排在前面. 然后边一条一条扫过去,如果是1 ,那么连一个点到集合中,如果是0,集合内的边相连. #include<cstd ...
随机推荐
- kafka的推和拉的问题
之前学习过这一问题,但是面试又被问道了.再次记录下 推还是拉? Kafka最初考虑的问题是,customer应该从brokes拉取消息还是brokers将消息推送到consumer,也就是pull还p ...
- hiho一下116周 网络流
网络流二·最大流最小割定理 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi:在上一周的Hiho一下中我们初步讲解了网络流的概念以及常规解法,小Ho你还记得内容么? ...
- 控制文本和外观------CSS Binding(CSS类名绑定)
<style> .myColor{color:red;font-size:13px} .yourColor{color:green;font-size:34px}</style> ...
- MySQL 用户与授权管理详解
大纲 一.前言 二.创建用户并授权 三.GRANT语句的种类 四.撤权并删除用户 一.前言 做为Mysql数据库管理员管理用户账户,是一件很重要的事,指出哪个用户可以连接服务器,从哪里连接,连接后能做 ...
- weblogic管理2 - 创建并启动一个managed server
创建一个managed server. 1. 进入网页console管理页面,如:http://10.100.25.14:7001/console , 先点击->服务器 (红色标记框) ...
- newman 3.0改变
https://github.com/postmanlabs/newman/blob/develop/MIGRATION.md
- android绘画折线图一
最近需要实现用android来画折线图,所以百度了一下,发现确实很多,也很乱,现在整理两种方法(第二种方法在[android绘画折线图二]中实现),仅供大家参考,一起学习研究. 第一种使用ChartF ...
- eclipse color themes 让eclipse编码好看点
http://eclipsecolorthemes.org/ 就是这个,很好用的! 安装后需要重启Eclipse.重启后打开Window->Preferences->General-> ...
- MySQL在一台db服务器上面如何启动多个实例 (转)
安装过程省略过,源码安装请参考http://write.blog.csdn.net/postlist/1609043/all 整理自己的文档,发现以前做的例子,share下,欢迎大家提出改进意见. 一 ...
- Android中Service的使用详解和注意点(LocalService)
Android中Service的使用详解和注意点(LocalService) 原文地址 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalServ ...