【Codeforces Round 335 (Div 2) D】【构造 脑洞】Lazy Student 图的最小生成树告诉边权和选取情况让你还原图

来源:互联网 发布:淘宝怎么改差评为好评 编辑:程序博客网 时间:2024/06/05 01:58

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

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 test(s)
input
4 52 13 14 01 15 0
output
2 41 43 43 13 2
input
3 31 02 13 1
output
-1


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=1e5+10,M=0,Z=1e9+7,ms63=1061109567;int n,m;struct A{int v;int u;int o;bool operator < (const A& b)const{if(v!=b.v)return v<b.v;return u>b.u;}}a[N];int b[N],c[N];bool check(){int ed=1;int x=1;int y=3;for(int i=1;i<=m;++i){if(a[i].u==1){b[a[i].o]=ed;c[a[i].o]=++ed;}else{if(y>ed)return 0;b[a[i].o]=x;c[a[i].o]=y;if(--x==0){++y;x=y-2;}}}return 1;}int main(){while(~scanf("%d%d",&n,&m)){for(int i=1;i<=m;++i)scanf("%d%d",&a[i].v,&a[i].u),a[i].o=i;sort(a+1,a+m+1);if(check()){for(int i=1;i<=m;++i)printf("%d %d\n",b[i],c[i]);}else puts("-1");}return 0;}/*【题意】这题也是十分有趣的。给你一个图,图上有n(1e5)个结点,m(1e5范围且n-1<=m<=C(n,2))。保证图联通。我们现在,依次告诉你这m条边的——(长度,是否为最小生成树上选取的边)让你构造一个图,还原这m条边的可能连接关系。或者判定这样的图不存在并输出-1【类型】构造 脑洞 最小生成树【分析】这题还是要借助最小生成树的相关知识的。最小生成树是每次选边权最小的边,然后如果不存在环,就选取。于是,我们要按照(边权>,选取>不选取)的关键字排序。对于当前的边,其边权肯定大于等于之前的边权。如果这条边需要选取,我们可以直接以链式关系选取。如果这条边不需要选取,我们需要构建一个环,使得之前环上的所有边(已选取)都小于等于这个边的权值。如果这条边不选取,而又找不到这个环,那么就false。注意不能有重边和自环。【时间复杂度&&优化】O(nlogn)【数据】4 61 11 12 11 01 01 0*/


1 0