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.
思路:
一开始我考虑失误了,-1的情况考虑错了,构造也错误了
正确思路应该是:
我们先将最小生成树上的边和不在最小生成树上的边进行排序,前n-1条边为最小生成树上的边,
然后我们先建一颗树,1—2连最小生成树的一条边,2—3连一条,3—4,,,这样依次,最后形成一条1到n的一条链
然后我们考虑把剩下的边添加到建好的最小生成树上,
我们考虑一下情况最小生成树是无法成功构成的:
我们的最小生成树是(u,v,w):(1,2,2),(2,3,3),(3,4,4),(4,5,5),,,
当不在最小生产树上的一条边权值为 1,此时最小生成树将会不再是题目所提供的最小生成树
我们需要保证每次添加进去的边防止这样的情况发生,
设为边(x,y,w),设生成树为T,x与T所连的点为z,若x—z的权值大于w,则我们可以删去x—z这条边,将x—y这条边作为生成树的一条边
1 #include <bits/stdc++.h> 2 #define fi first 3 #define se second 4 #define mm(a,b) memset(a,b,sizeof(a)) 5 #define PI acos(-1) 6 #define int long long 7 #define no cout<<"NO\n"; 8 #define yes cout<<"YES\n"; 9 using namespace std; 10 typedef long long LL; 11 typedef pair<int,int> PII; 12 const int N = 1e6+10; 13 const int mod=998244353; 14 int n,m,a[N],b[N]; 15 struct XY 16 { 17 int id,w,u,v,b; 18 } xy[N]; 19 bool cmp1(XY a,XY b) 20 { 21 if(a.b==b.b) 22 return a.w<b.w; 23 return a.b>b.b; 24 } 25 bool cmp2(XY a,XY b) 26 { 27 return a.id<b.id; 28 } 29 signed main() 30 { 31 ios::sync_with_stdio(false); 32 cin.tie(0),cout.tie(0); 33 int T=1; 34 //cin>>T; 35 while(T--) 36 { 37 int n,m; 38 cin>>n>>m; 39 for(int i=1; i<=m; ++i) 40 { 41 cin>>xy[i].w>>xy[i].b; 42 xy[i].id=i; 43 } 44 sort(xy+1,xy+m+1,cmp1); 45 for(int i=1; i<n; ++i) 46 { 47 xy[i].u=i; 48 xy[i].v=i+1; 49 } 50 int res=3,t=0; 51 for(int i=n; i<=m; ++i) 52 { 53 t++; 54 if(t+2>res) 55 { 56 res++; 57 t=1; 58 } 59 if(xy[i].w<xy[res-1].w) 60 { 61 cout<<-1<<"\n"; 62 return 0; 63 } 64 xy[i].u=res; 65 xy[i].v=t; 66 } 67 sort(xy+1,xy+m+1,cmp2); 68 for(int i=1; i<=m; ++i) 69 { 70 cout<<xy[i].u<<" "<<xy[i].v<<"\n"; 71 } 72 } 73 return 0; 74 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】