---------------------------------------------------editorial---------------------------------------------------------
this problem can easily solve without using dsu
considerations
sort all distance , now one by one start adding these edge in the graph
for simplicity we can consider mst graph as a star graph or a linear graph (here in my code i considere as a star graph )
if any edge is exists in the mst add it with node 1 (since mst is a star graph in my soln )
else
add that edge b/w any two alreaded vertices , bcoze that edge not included in the mst means it is forming a cycle ,
now if all the nodes are connected there is no way to connect that edge with the existing connected edge , means that edge is a part of mst soooooo ans=-1;
note there is one another most important observation is that sort the distance using own comparator such that if there are two edge having same length han edge which included in mst must comes earlier in sorted aray ( think why ?..easy think again)
else continue ,
implementation can be understand with this editorial;
Let’s order edges of ascending length, in case of a tie placing earlier edges we were asked to include to MST. Let’s start adding them to the graph in this order. If we asked to include the current edge to MST, use this edge to llink 1st vertex with the least currently isolated vertex. If we asked NOT to include the current edge to MST, use this edge to link some vertices that are already linked but have no edges between them. To do this it’s convenient to have two pointer on vertices (let’s call them FROM and TO). At the beginning, FROM=2, TO=2. When we are to link two already linked vertices, we add new edge (FROM, TO) and increment FROM. If FROM becomes equal to TO, we can assume we already added all possible edges to TO, so we increment TO and set FROM to 2. This means from this moment we will use non-MST edges to connect TO with all previous vertices starting from 2. If it appears that TO looks at currently isolated vertex, we can assume there are no place for non-MST edge it the graph, so the answer is Impossible. Keep doing in the described way, we’ll be adding MST edges as (1,2), …, (1,n) and non-MST edges as (2,3), (2,4), (3,4), (2,5), (3,5), (4,5), ...
--------------------------------------code------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
vector<pair<pair<int,int>,int> > v,ans;
#define pp pair<pair<int,int>,int>
bool compare(pp p1,pp p2)
{
if(p1.first.first<p2.first.first)return true;
else if(p1.first.first>p2.first.first) return false;
else
{
if(p1.first.second>p2.first.second) return true;
else return false;
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
// if(b==0) b=2;
v.push_back(mp(mp(a,b),i+1));
}
sort(v.begin(),v.end(),compare);
int left=2;
int right=2;
int max_inserted=1;
for(int i=0;i<m;i++)
{
if(v[i].first.second==1)
{
ans.push_back(mp(mp(v[i].second,1),++max_inserted));
}
else
{
if(left<right)
{
ans.push_back(mp(mp(v[i].second,left),right));
left++;
}
else
{
if(right<max_inserted)
{
right++;
left=2;
ans.push_back(mp(mp(v[i].second,left),right));
left++;
}
else
{
cout<<"-1"<<endl;
exit(0);
}
}
}
}
sort(ans.begin(),ans.end());
for(int i=0;i<m;i++)
{
cout<<ans[i].first.second<<" "<<ans[i].second<<endl;
}
}
No comments:
Post a Comment