Codeforces Round #335 (Div. 2) D. Lazy Student Greedy

Source: Internet
Author: User

D. Lazy Student

Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph-something that would 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 definit Ion

The minimum spanning tree T of graph G is such a tree that it contains all the vertices of the O Riginal graph G, and the sum of the weights of its edges are 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's included in the found Tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher are getting very angry and demands to See the original graph. Help Vladislav come to a graph so, the information about the minimum spanning tree remains correct.

Input

The first line of the input contains the integers n and m ()-the number of vertices and the Nu Mber of edges in the graph.

Each of the Next  m  lines describes an edge of the graph and consists of I Ntegers  a J  and  b J   (1≤ A J ≤109, b J = {0, 1}). The first of these numbers is the weight of the edge and the second number are equal To 1  If this edge is included in the minimum spanning tree found by Vladislav, Or 0 if it was not.

It is guaranteed this exactly n -1 number {bJ} is equal to one and exactly m - n + 1 of them is equal to zero.

Output

If Vladislav has made a mistake and such graph doesn ' t exist, print -1.

Otherwise PrintmLines. On the J-th Line print a pair of vertices (uJ, vJ) (1≤ u< /c10>J, vjn, uJv J), that should is connected by the J-th Edge. The edges is numbered in the same order as in the input. The graph, determined by these edges, must is connected, contain no loops or multiple edges and its edges with b J = 1 must define the minimum spanning tree. In case there is multiple possible solutions, print any of them.

Sample Test (s) input
4 5
2 1
3 1
4 0
1 1
5 0
Output
2 4
1 4
3 4
3 1
3 2
input
7 ·
1 0
2 1
3 1
Output
-1

Test instructions

Give you n point, M edge tree, topic make a minimum spanning tree, tell you which is the minimum spanning tree edge and its weight, let you construct a tree to meet the conditions

Exercises

Greedy, we are in accordance with the weight from small to large order, so that the minimum spanning tree edge set to 1-x on it.

Other non-minimum spanning tree edges are X-y, increment y to tail, gathered up less than y X is the best

//Meek///#include<bits/stdc++.h>#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<cstring>#include<algorithm>#include<queue>#include<map>#include<Set>#include<stack>#include<sstream>#include<vector>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_back#defineFi first#defineSe Second#defineMP Make_pairinline ll read () {ll x=0, f=1; CharCh=GetChar ();  while(ch<'0'|| Ch>'9')    {        if(ch=='-') f=-1; CH=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); }    returnx*F;}//****************************************Const intn=250000+ -;Constll inf = 1ll<< A;Const intMod=1000000007;structSS {intW,d,id;} A[n];intCMP (ss S1,ss S2) {if(S1.W==S2.W)returnS1.d>s2.d; returns1.w<S2.W;}intN,m,vis[n];vector< pair<int,pair<int,int> > >ans;intMain () {intflag=0; scanf ("%d%d",&n,&m);  for(intI=1; i<=m; i++) {scanf ("%d%d",&a[i].w,&A[I].D); A[i].id=i; } sort (A+1, a+m+1, CMP); intaim=n-1; intans1=1, ans2=2, l=2, r=2; vis[1]=vis[2]=1;  for(intI=1; i<=m;i++) {        if(a[i].d==0) {          if(!Vis[r]) {Flag=1; }           if(l==R) {R++; L=2; }if(!Vis[r]) {Flag=1;           } ANS.PB (MP (A[I].ID,MP (l,r))); L++; }        Else{ANS.PB (MP (A[I].ID,MP (ANS1,ANS2))); VIS[ANS2]=1; Ans2+=1; }    }    if(flag) {cout<<-1<<Endl; return 0;    } sort (Ans.begin (), Ans.end ());  for(intI=0; I<ans.size (); i++) {cout<<ans[i].se.fi<<" "<<ans[i].se.se<<Endl; }    return 0;}
Code

Codeforces Round #335 (Div. 2) D. Lazy Student Greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.