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 52 13 14 01 15 0

Sample Output

2 41 43 43 13 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 100005struct 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 = 2,r = 2;
vector<node> p;
int ans1[maxn],ans2[maxn];
int vis[maxn];
int main()
{int n,m;cin>>n>>m;for(int i=1;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 = 2;for(int i=0;i<p.size();i++){if(p[i].y==1){ans1[p[i].z]=1,ans2[p[i].z]=rr;vis[rr]=1;rr++;}else if(p[i].y==0){if(l==r){r++;l=2;if(vis[r]==0)return puts("-1");}ans1[p[i].z]=l++,ans2[p[i].z]=r;}}for(int i=1;i<=m;i++)printf("%d %d\n",ans1[i],ans2[i]);
}

转载于:https://www.cnblogs.com/qscqesze/p/5036508.html

Codeforces Round #335 (Div. 2) D. Lazy Student 构造相关推荐

  1. Codeforces Round #742 (Div. 2) F. One-Four Overload 构造 + 二分图染色

    传送门 文章目录 题意: 思路: 题意: 给你一个n∗mn*mn∗m的矩形,包含...和XXX,你有两种颜色,你需要给...染色使得每个XXX上下左右相邻的...其两种颜色个数相同,输出一种合法方案. ...

  2. Codeforces Round #740 (Div. 2) E. Bottom-Tier Reversals 构造

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为奇数nnn的排列aaa,每次可以选择长度为奇数的前缀,并将[1,len][1,len][1,len]翻转,你需要用不超过5n2\frac{5n} ...

  3. Codeforces Round #335 (Div. 2)

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

  4. Codeforces Round #335 (Div. 1)--C. Freelancer's Dreams 线性规划对偶问题+三分

    题意:p, q,都是整数. sigma(Ai * ki)>= p, sigma(Bi * ki) >= q; ans = sigma(ki).输出ans的最小值 约束条件2个,但是变量k有 ...

  5. Codeforces Round #301 (Div. 2) B. School Marks 构造/贪心

    B. School Marks Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/probl ...

  6. Codeforces Round #700 (Div. 1) C. Continuous City 构造 + 二进制

    传送门 文章目录 题意: 思路: 题意: 构造一个图,使其从111到nnn的路径的长度与[L,R][L,R][L,R]中某个值一一对应,不能有两条路径长度一样,且每个值都必须出现一次,每两个点之间只能 ...

  7. Codeforces Round #715 (Div. 2) D. Binary Literature 构造

    传送门 文章目录 题意: 思路: 题意: 给你个nnn和三个长度为n∗2n*2n∗2的串,让你构造一个长度≤n∗3\le n*3≤n∗3的串,使其子序列包含至少两个给定串. 思路: 先考虑如果没有长度 ...

  8. Codeforces Round #712 (Div. 2) D. 3-Coloring 交互 构造

    传送门 文章目录 题意: 思路: 题意: 给一个n∗nn*nn∗n的格子染色,一共可以染三种颜色,每次都会给一种颜色,代表当前这种颜色不可以使用,染色要求相邻的颜色不能相同,让你给出一种染色方案. 思 ...

  9. Codeforces Round #723 (Div. 2) D. Kill Anton 线段树 + 暴力

    传送门 文章目录 题意: 思路: 题意: 给你一个只有ANTOANTOANTO四个字母的字符串,你每次可以交换相邻两个,花费为111,让后让你打乱字符串,使得将打乱的字符串还原为原来的字符串的花费最小 ...

最新文章

  1. NVIDIA FFmpeg 转码技术分析
  2. javascript object 转换int_关于javascript 中类型转换那些事你知道吗?
  3. dim private public static_static方法 (静态方法)
  4. 【Matlab】找到矩阵中每个连通域的最小值
  5. C语言实现词典编排算法(附完整源码)
  6. html从入门到精通胡菘,网页设计与制作32课时完整教学大纲
  7. vba 提取 json某个值_利用VBA字典,提取两列数据的重复值
  8. Flask Oauth
  9. Hobject 与 OpenCV IplImage 和 Mat 的相互转换
  10. jquery跨域调用webService
  11. 270 扩展固态硬盘_希捷硬盘白送3年原厂数据恢复!成功率高达90
  12. RedHat7安装及小红帽硬盘分区建议
  13. xp升级windows7_试验机之液压万能试验机升级改造全国招商-机械设备-招商
  14. AlphaZero问世:8小时完爆围棋、国际象棋、日本将棋(转)
  15. 量子计算机和量子纠缠的关系,科普:什么是量子纠缠和量子计算?
  16. AI如何识别西瓜和冬瓜?
  17. Cocos Creator 3.61所有工具软件的使用
  18. 数据仓库工程师面试题
  19. VC++6.0 总是1error 问题的解决
  20. 【Leetcode】444. Sequence Reconstruction

热门文章

  1. 使用CSS切换不同背景的字体颜色
  2. JS中 forEach 方法添加属性遇到 “obj“ is not extensible 的解决方法 (不可扩展、密封、冻结的对象添加属性)
  3. excel能设置从属值集吗_Excel中的从属下拉列表
  4. 项目:低配版Everything
  5. 【教学类-35-01】(256*256*256)RGB色卡图片
  6. 别人家的工资!在BAT做三年 普通员工能拿多少?
  7. 搭建适合自己的DDR3仿真平台
  8. 专访融文中国区负责人李建兴:洞悉舆情环境,打造一流品牌
  9. 对于Uncaught TypeError: Cannot set property ‘innerHTML‘ of null 的理解
  10. 什么是管道?(详解进程间是如何通过管道通信的)