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 ...
随机推荐
- poj1420 拓扑序
题意:给出一个表格,一部分单元格是给定的数字,而另一部分单元格则是一个式子,表示是其他一些单元格的和,让你输出最后计算出的所有格子的数. 因为有些格子需要其他格子先计算出来,所以计算顺序是按照拓扑序的 ...
- (转) ICML2016 TUTORIAL参会分享
ICML2016 TUTORIAL参会分享 本文转自: https://mp.weixin.qq.com/s?__biz=MzI3MDE4NTk4MQ==&mid=2658399541& ...
- HashMap的原理与实 无锁队列的实现Java HashMap的死循环 red black tree
http://www.cnblogs.com/fornever/archive/2011/12/02/2270692.html https://zh.wikipedia.org/wiki/%E7%BA ...
- JSBinding + SharpKit / 安装SharpKit以及添加SharpKit工程
本文说明如何往 sln 中添加 SharpKit 工程,以及配置. SharpKit 工程用于将 C# 源代码编译成 JS 代码. QQ群 189738580 1. 安装SharpKit 到 sha ...
- UMDH
1. 什么是UMDH UMDH=User Mode dump heap.把进程的堆dump下来.包括堆栈信息.UMDH是WinDbg附带的一个工具. 2. 下载安装 WinDbg.完事后在WinDbg ...
- /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h:No such file or directory的解决办法
在编译32位HDecode时出现如题所示的错误,原因时没有安装32位glibc库导致的: ubuntu: sudo apt-get install libc6-dev-i386 CentOS:yum ...
- lua堆栈操作常用函数学习二
/* ** basic stack manipulation */ LUA_API int <strong> (lua_gettop) (lua_State *L); </str ...
- rac 11g_生产库日志组损坏处理
原创作品,出自 "深蓝的blog" 博客,转载时请务必注明出处,否则有权追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ar ...
- 8000401a 错误 ,检索 COM 类工厂中 CLSID 为 的组件时失败,原因是出现以下错误: 8000401a。
"/"应用程序中的服务器错误. -------------------------------------------------------------------------- ...
- SignalR的安装
介绍 SignalR 是 ASP.NET 团队正在开发的一个 Microsoft .NET Framework 库和 jQuery 插件,可能包括在以后版本的 ASP.NET 平台中. 它提供了一些前 ...