题意:

一个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 构造 好题的更多相关文章

  1. 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 ...

  2. 605B. Lazy Student(codeforces Round 335)

    B. Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. CF#335 Lazy Student

    Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造

    题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...

  5. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心

    D. Lazy Student   Student Vladislav came to his programming exam completely unprepared as usual. He ...

  6. cf 443 D. Teams Formation](细节模拟题)

    cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...

  7. 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root

    题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...

  8. CF C. Vladik and fractions——构造题

    题目 构造一组 $x, y, z$,使得对于给定的 $n$,满足 $\frac{1}{x}  + \frac{1}{y} + \frac{1}{z} =  \frac{2}{n}$. 分析: 样例二已 ...

  9. CodeForces 605B Lazy Student

    构造.对边的权值排序,权值一样的话,在MST中的边排到前面,否则权值小的排在前面. 然后边一条一条扫过去,如果是1 ,那么连一个点到集合中,如果是0,集合内的边相连. #include<cstd ...

随机推荐

  1. 1-1 Windows应用程序的特点

    主要内容:介绍Windows应用程序的特点,并附加了消息和事件的一些区别 //以后该分类中字体均采用 隶书 4(14pt) 1. 面向对象 <1>针对Windows应用本身,如记事本界面, ...

  2. window7资源管理器一直重启(百度知道找到可用)

    今天我的机器也出现这种问题:我的解决方式是,在开机时选择系统修复选项中的进入命令行方式(尝试过用安全模式,文件被占用,现象一样),然后cd C:\Users\Administrator\AppData ...

  3. 使用CURL下载远程文件保存到服务器

    比如微信公众平台开发,下载用户的头像到服务器上: /** * 使用CURL获取远程文件保存到服务器 *@param $image=$oJSON->headimgurl; 获取到的微信返回的头像U ...

  4. 什么是域名?什么网站名?什么是URL?

    域名,相信大家都不默认,也使用过无数次!比如: google.com.baidu.com.163.com等. 这时候,你可能要奇怪了,为什么小编没有在前面加上www? 因为正常情况下,不应该是www. ...

  5. Tyvj 9.10 总结 (其实只是发一下心情)

    原以为能拿200的,然而看到成绩彻底方了,60滚粗,连100都不到 这种成绩连一等都拿不到,我tm还谈什么省选,更别说NOI了 目前还没有部分分版本的,也没有数据,不知道问题出在哪里 tyvj群里好多 ...

  6. 嵌入式Linux C笔试题积累(转)

    http://blog.csdn.net/h_armony/article/details/6764811 1.   嵌入式系统中断服务子程序(ISR) 中断是嵌入式系统中重要的组成部分,这导致了很 ...

  7. Improve Scalability With New Thread Pool APIs

    Pooled Threads Improve Scalability With New Thread Pool APIs Robert Saccone Portions of this article ...

  8. git 教程 ,常用命令

    Git使用手册 http://www.cnblogs.com/lantingji/p/5942721.html git官网 https://git-scm.com/ Git的奇技淫巧 http://w ...

  9. Logistic回归的牛顿法及DFP、BFGS拟牛顿法求解

    牛顿法 # coding:utf-8 import matplotlib.pyplot as plt import numpy as np def dataN(length):#生成数据 x = np ...

  10. Python_Day_4(内置函数之篇)

    一:内置函数 常用内置函数如下: 1)abs:取一个数字的绝对值 #abs:取绝对值n = abs(-10)print(n) 2)any和all 值为Fslse有:0,None,"" ...