题目链接:
http://codeforces.com/contest/606/problem/D
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 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-----------------------------------------------------------------------