Codeforces Round #335 (Div. 2) D. Lazy Student 构造
D. Lazy Student
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/606/problem/D
Description
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.
Input
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
Output
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.
Sample Input
4 5
2 1
3 1
4 0
1 1
5 0
Sample Output
2 4
1 4
3 4
3 1
3 2
HINT
题意
有一个n点m边的图,没有自环,没有重边
然后给你这m条边的大小,以及哪些边属于一颗最小生成树里面的
现在让你构造一个图,使得,满足对应的边,确实属于一颗最小生成树里面。
如果不能构造,输出-1.
题解:
我们首先把所有边都读入,然后按照边权从小到大排序,边权一样,树边优先。
然后依次插入。
如果插入的是树边,就直接插入就好了。
如果插入的是图边,那么图边一定比他所在环的所有树边的边权都大。
把树边插成一个菊花图/链图都可以
然后图边就直接插入就好了,插入之后,控制l++,因为r不变的话,图边的边权要求也是不会变的。
如果l == r的话,看是否r+1这个点被树边插过,如果没有就return -1
否则就r++,l = 1就行了
一直循环就好了
代码:
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 100005 struct node
{
int x,y,z;
};
bool cmp(node A,node B)
{
if(A.x==B.x)
return A.y>B.y;
return A.x<B.x;
}
int l = ,r = ;
vector<node> p;
int ans1[maxn],ans2[maxn];
int vis[maxn];
int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<=m;i++)
{
node k;
scanf("%d%d",&k.x,&k.y);
k.z = i;
p.push_back(k);
}
sort(p.begin(),p.end(),cmp);
int rr = ;
for(int i=;i<p.size();i++)
{
if(p[i].y==)
{
ans1[p[i].z]=,ans2[p[i].z]=rr;
vis[rr]=;rr++;
}
else if(p[i].y==)
{
if(l==r)
{
r++;l=;
if(vis[r]==)return puts("-1");
}
ans1[p[i].z]=l++,ans2[p[i].z]=r;
}
}
for(int i=;i<=m;i++)
printf("%d %d\n",ans1[i],ans2[i]);
}
Codeforces Round #335 (Div. 2) D. Lazy Student 构造的更多相关文章
- 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 ...
- Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造
Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 ht ...
- 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. ...
随机推荐
- 12、NFC技术:读写NFC标签中的Uri数据
功能实现,如下代码所示: 读写NFC标签的Uri 主Activity import cn.read.write.uri.library.UriRecord; import android.app.Ac ...
- AFNetWorking 使用记录
1.从一个URL GET数据 方法1: NSURL * url = [NSURL URLWithString:@"http://www.weather.com.cn/data/sk/101 ...
- C++模板详解
参考:C++ 模板详解(一) 模板:对类型进行参数化的工具:通常有两种形式: 函数模板:仅参数类型不同: 类模板: 仅数据成员和成员函数类型不同. 目的:让程序员编写与类型无关的代码. 注意:模板 ...
- 移动开发必备!15款jQuery Mobile插件
移动互联网的发展,来自PC端的网页并不能完全自适应移动端页面需求,使得响应式设计体验产生并成为潮流,也正是这样一种需求,促成了jQuery Mobile的流行.jQuery Mobile这样一款基于j ...
- flappy pig小游戏源码分析(1)——主程序初探
闲逛github发现一个javascript原生实现的小游戏,源码写的很清晰,适合想提高水平的同学观摩学习.读通源码后,我决定写一系列的博客来分析源码,从整体架构到具体实现细节来帮助一些想提高水平的朋 ...
- public static void main(String arg[])
该语句定义了main方法. main方法是程序执行的入口,所有的java程序都必须具备一个main()方法,而且必须按照如上的格式来定义. 不具有main方法的类可以编译,但不能执行.因为它没有m ...
- 使用iphone5修剪视频的方法
iphone5有很高的视频拍摄质量,这人很多业余爱好者也加入了视频短片的拍摄当中.在拍摄视频的过程中,为了能够捕捉到精彩瞬间,常常会提前拍摄,并延迟结束拍摄,这样就给视频增加了很多无意义的片段.这时, ...
- 基于mapreduce的大规模连通图寻找算法
基于mapreduce的大规模连通图寻找算法 当我们想要知道哪些账号是一个人的时候往往可以通过业务得到两个账号之间有联系,但是这种联系如何传播呢? 问题 已知每个账号之间的联系 如: A B B C ...
- geeksforgeeks@ Minimum Points To Reach Destination (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=91 Minimum Points To Reach Destination Gi ...
- php--opp--2.什么是类,什么是对象,类和对象这间的关系
类的概念:类是具有相同属性和服务的一组对象的集合.它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和服务两个主要部分.在面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属 ...