Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造
题目链接:
http://codeforces.com/contest/606/problem/D
D. Lazy Student
time limit per test2 secondsmemory 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 贪心+构造的更多相关文章
- Codeforces Round #335 (Div. 2) D. Lazy Student 贪心
D. Lazy Student Student Vladislav came to his programming exam completely unprepared as usual. He ...
- 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 ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- 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 ...
- 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 ...
- 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 ...
- 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/ ...
- Codeforces Round #335 (Div. 2)
水 A - Magic Spheres 这题也卡了很久很久,关键是“至少”,所以只要判断多出来的是否比需要的多就行了. #include <bits/stdc++.h> using nam ...
- 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. ...
随机推荐
- SessionStroage和locationStorage的思考
从理论上讲 LocalStroage 内存更大,存储时间更为持久,作用域更大.那么SessionStroage有存在的必要吗?有什么样的应该场景是必须用seessionStroage 而不能使用Loc ...
- 偏前端-纯css,手写轮播-(焦点切换 和 自动轮播 只可选择一种,两者不可共存)
现在我们一般都是在网上找个轮播插件,各种功能应有尽有,是吧!!~大家似乎已经生疏了手写是什么感觉.万一哪天想不起来,人家要手写,就尴尬了!~~跟我一起复习一下吧 不多说:效果图看一下: 高度不能是固定 ...
- MyEclipse部署项目时点finish点不动finish按钮灰色的
在MyEclipse中项目的propertes中输入tomcat搜索,jdk选择你本机安装的jdk
- MongoDB基础教程
MongoDB 一.下载MongoDB数据库 1.进入MongoDB官网进行下载,网址:https://www.mongodb.com/. 2.下载完成后可进行安装,安装后,并有了MongoDB服务. ...
- Sql主从同步服务
主服务器A:192.168.1.102从服务器B:192.168.1.103 先关掉主服务器phpstudy,把数据库备份到从服务器 1.授权用户:在A服务器新建一个从账号锁定IP GRANT REP ...
- php 二位数组排序
$member_ship_level 是一个二维数组 $res = array_column($member_ship_level,'integral'); array_multisort($res, ...
- Hive配置项的含义详解
关于MetaStore:metastore是个独立的关系数据库,用来持久化schema和系统元数据. hive.metastore.local:控制hive是否连接一个远程metastore服务器还是 ...
- 20155202 《Java程序设计》实验三(敏捷开发与XP实践)实验报告
20155202 <Java程序设计>实验三(敏捷开发与XP实践)实验报告 代码托管 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<L ...
- 20155206 《Java程序设计》实验四实验报告
20155206 <Java程序设计>实验三实验报告 实验内容 Android程序设计 实验步骤 part1: Android Stuidio的安装测试: 参考<Java和Andro ...
- 20155301 2016-2017-2 《Java程序设计》第8周学习总结
20155301 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 1.java.util.logging包提供了日志功能相关类与接口.使用日志的起点是logg ...