题目链接:

http://codeforces.com/contest/606/problem/D

D. Lazy Student

time limit per test2 seconds
memory limit per test256 megabytes
#### 问题描述
> Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following definition:
>
> The minimum spanning tree T of graph G is such a tree that it contains all the vertices of the original graph G, and the sum of the weights of its edges is the minimum possible among all such trees.
>
> Vladislav drew a graph with n vertices and m edges containing no loops and multiple edges. He found one of its minimum spanning trees and then wrote for each edge its weight and whether it is included in the found tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher is getting very angry and demands to see the original graph. Help Vladislav come up with a graph so that the information about the minimum spanning tree remains correct.
#### 输入
> The first line of the input contains two integers n and m () — the number of vertices and the number of edges in the graph.
>
> Each of the next m lines describes an edge of the graph and consists of two integers aj and bj (1 ≤ aj ≤ 109, bj = {0, 1}). The first of these numbers is the weight of the edge and the second number is equal to 1 if this edge was included in the minimum spanning tree found by Vladislav, or 0 if it was not.
>
> It is guaranteed that exactly n - 1 number {bj} are equal to one and exactly m - n + 1 of them are equal to zero.
#### 输出
> If Vladislav has made a mistake and such graph doesn't exist, print  - 1.
>
> Otherwise print m lines. On the j-th line print a pair of vertices (uj, vj) (1 ≤ uj, vj ≤ n, uj ≠ vj), that should be connected by the j-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must be connected, contain no loops or multiple edges and its edges with bj = 1 must define the minimum spanning tree. In case there are multiple possible solutions, print any of them.
####样例输入
> 4 5
> 2 1
> 3 1
> 4 0
> 1 1
> 5 0
####样例输出
> 2 4
> 1 4
> 3 4
> 3 1
> 3 2
## 题意
> 给出n个点m条边,边不给起点和终点,只给权值,然后0表示不在最小生成树上,1表示在。并且生成树上的边刚好n-1条。构造一个图满足所有的条件,没有自环和重边,如果不存在,则输出-1,否则输出所有的边的起点和终点。
## 题解
> 首先,我们把最小生成树的高度限制为1,因为这样在后面连非树边的时候形成的环最小(只有三条边),这样能够很好的吧树边的权值独立开,创造更多合法的顶点对。然后,就是将树边,非树边分别按权值排个序,贪心的把非树边插到生成树里面,直到有一条非树边插不进去,则输出-1.否则输出最后的结果。
## 代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=1e5+10; PII ans[maxn]; int n,m; int main() {
scf("%d%d",&n,&m);
VPII a,b;
int id=1;
for(int i=0;i<m;i++){
int x,type;
scf("%d%d",&x,&type);
if(type==0) a.pb(mkp(x,i));
else{
b.pb(mkp(x,i));
ans[i]=mkp(1,++id);
}
}
sort(all(a));
sort(all(b));
int p=0;
int x=0,y=1,su=1;
for(int i=0;i<a.sz();i++){
if(y>=b.sz()||a[i].X<b[y].X){
su=0; break;
}
ans[a[i].Y]=mkp(ans[b[x].Y].Y,ans[b[y].Y].Y);
x++;
if(x==y){ x=0; y++; }
}
if(su==0) prf("-1\n");
else{
for(int i=0;i<m;i++) prf("%d %d\n",ans[i].X,ans[i].Y);
}
return 0;
} //end-----------------------------------------------------------------------

Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造的更多相关文章

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

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

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

  3. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  4. Codeforces Round #335 (Div. 2) B. Testing Robots 水题

    B. Testing Robots Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606 ...

  5. Codeforces Round #335 (Div. 1) C. Freelancer's Dreams 计算几何

    C. Freelancer's Dreams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contes ...

  6. Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 动态规划

    C. Sorting Railway Cars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/conte ...

  7. Codeforces Round #335 (Div. 2) A. Magic Spheres 水题

    A. Magic Spheres Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606/ ...

  8. Codeforces Round #335 (Div. 2)

    水 A - Magic Spheres 这题也卡了很久很久,关键是“至少”,所以只要判断多出来的是否比需要的多就行了. #include <bits/stdc++.h> using nam ...

  9. Codeforces Round #335 (Div. 2) A. Magic Spheres 模拟

    A. Magic Spheres   Carl is a beginner magician. He has a blue, b violet and c orange magic spheres. ...

随机推荐

  1. nginx 源码安装以及后续升级https

    事情的来源是,公司要将网站从http升级到https,由于历史遗留原因,才发现现有的nginx是通过源码安装的,并没有安装ssl模块,需要现安装sll模块,这个nginx是整个公司最前端的一个代理,涉 ...

  2. FROM_UNIXTIME/CONCAT

    将mysql查询结果中时间戳转化为时间格式 FROM_UNIXTIME( c.createtime, '%Y-%m-%d %H:%i:%S' ) 2个字段合并查询 CONCAT(d.`name`, ' ...

  3. PE 学习之路 —— DOS 头、NT 头

    1. 前述 可执行文件的格式是操作系统本身执行机制的反映,理解它有助于对操作系统的深刻理解,掌握可执行文件的数据结构及其一些机理,是研究软件安全的必修课.`PE(Portable Executable ...

  4. python+selenium实现动态爬取及selenuim的常用操作

    应用实例可以参考博客中的12306自动抢票应用 https://www.cnblogs.com/mumengyun/p/10001109.html 动态网页数据抓取 什么是AJAX: AJAX(Asy ...

  5. Bugku一段base64

    本文转自:本文为博主原创文章,如有转载请注明出处,谢谢. https://blog.csdn.net/pdsu161530247/article/details/74640746 链接中高手给出的解题 ...

  6. 002---rest_framework认证组件

    rest_framework认证组件 问题:有些API,需要用户登录才能访问,有些无需登录就能访问. 解决: a. 创建两个模型类:UserInfo---OneToOne---UsetToken b. ...

  7. 脱离matlab运行可执行程序的步骤

    MCR是由matlab的运行环境,占用不到600M的对于用不同matlab版本生成的exe文件,MCR版本也会有不同,因此,在程序打包时,最好将相应版本的MCR一起打包.MCR环境的设置文件存放目录如 ...

  8. vs2013发布网站合并程序是出错(ILmerge.merge:error)

    Vs2013发布网站时,生成错误提示: 合并程序集时出错: ILMerge.Merge: ERROR!!: Duplicate type 'manage_ForcePasswrod' found in ...

  9. 20155232 2016-2017-2 《Java程序设计》第2周学习总结

    20155232 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 类型 基本类型 整数(short.int.long) 字节(byte) -128~127 字 ...

  10. 20155236 《Java程序设计》实验四(Android程序设计)实验报告

    20155236 <Java程序设计>实验四(Android程序设计)实验报告 一.实验内容及步骤 第24章:初识Android 任务一:完成Hello World, 要求修改res目录中 ...