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 ...
随机推荐
- iPad 2升级iOS 9的过程记录
有一台老旧的iPad2,iOS版本还是5.1.1,现在好多软件都无法安装了. 决定升级到最新的操作系统,中间的过程,遇到的问题和解决办法如下: 据说升级到iOS 9以后就不好越狱了,不过我也就是用用一 ...
- 用Spring的mappingDirectoryLocations来配置Hibernate映射文件
在Spring的applicationContext.xml中配置映射文件的方法: <property name="mappingResources"> < ...
- Java设计模式之适配器设计模式
1.适配器模式( Adapter)定义将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原来由于接口不兼容而不能一起工作的 那些类可以一起工作. 现实案例如下: 墙上电源类(22 ...
- cluster,network
How cluster works https://technet.microsoft.com/en-us/library/cc738051(v=ws.10).aspx http://blogs.te ...
- 如何将maven项目导入myeclipse中
在mvn的项目中 pom.xml 文件所在目录, 运行 mvn eclipse:clean eclipse:eclipse ,会自动将mvn工程转成eclipse工程, 然后在eclipse中 &qu ...
- Kettle合并记录步骤
转载: http://blog.itpub.net/post/37422/464323 该步骤用于将两个不同来源的数据合并,这两个来源的数据分别为旧数据和新数据,该步骤将旧数据和新数据按照指定的关键字 ...
- linux更改启动级别后,无法启动的问题解决
装好之后,配置好IP,启动后也能上网了,然后我修改了系统的启动级别(默认为3,我改为了5),意思是让他能够启动桌面. 我是这么设置的: 1.vi命令打开/etc/inittab文件,可以看到如下描述 ...
- Web版报表的架构和设计思路
通常,我们设计一个报表,会面临设计报表,构造数据,展示报表的一个过程 设计报表也许大家都会用过,比如水晶报表,jreport一堆一堆的. 不过我觉得最好用的应该还是office做成的报表,谁都会,而且 ...
- 111个知名Java项目集锦,包括url和描述
转:http://www.cnblogs.com/wangs/p/3282183.html 项目名称 项目描述 ASM Java bytecode manipulation framework A ...
- 【extjs】 ext5 Ext.grid.Panel 分页,搜索
带有分页,搜索的grid. <%@page language="java" contentType="text/html; charset=UTF-8" ...