Restore Graph
########################editorial########################
finally if answer exist than for the formation of graph connect node with dist x to node with dist x-1, (but alco check that if node with dist x-1 must connect with maximum k no of node for that do hassing of node connected with all nodes )......
################CODE############
in this problem 1 st need to decide whether it is possinble to form a graph or not .. for that node with dist 0 (starting )can form k no of connections from from node with dist 1 now all node with dist 1 can form maximum k-1 connections with node of dist k-1 , (sice that noode is already connected with node with dist 0 ) and so on ...
finally if answer exist than for the formation of graph connect node with dist x to node with dist x-1, (but alco check that if node with dist x-1 must connect with maximum k no of node for that do hassing of node connected with all nodes )......
################CODE############
#include<iostream>
using namespace std;
long long int has[10001000];
long long int cnt[10001000];
long long int cp[10001000];
#include<bits/stdc++.h>
int main()
{
long long int n,k;
long long int max_level=0;
cin>>n>>k;
vector<pair<long long int,long long int> > v;
long long int start=0;
for(long long int i=0;i<n;i++)
{
long long int zz;
cin>>zz;
v.push_back(make_pair(zz,i+1));
if(zz==0) start=i+1;
has[zz]++;// count of occurance of dist x
if(zz>max_level) max_level=zz;
}
sort(v.begin(),v.end());
if(has[1]>has[0]*k || has[0]>1 || has[0]==0)
{
cout<<"-1"<<endl;
return 0;
}
cp[0]=0;
cp[1]=1;
long long int kk=2;
for(long long int i=2;i<=n;i++)
{
if(has[i]>has[i-1]*(k-1))
{
// cout<<"here"<<endl;
cout<<"-1"<<endl;
return 0;
}
if(v[i].first!=v[i-1].first) cp[kk++]=i;// cp contain the occurance //start of new dist
}
kk=-1;
long long int index=0;
cout<<n-1<<endl;
for(long long int i=1;i<n;i++)
{
long long int no=v[i].second;
long long int dist=v[i].first;
if(v[i].first!=v[i-1].first)
{
kk++;
index=cp[kk];
}
if(cnt[v[index].second]>=k) index++;// if index no node is //connected with k nodes
cout<<v[index].second<<" "<<v[i].second<<endl;
cnt[v[index].second]++;
cnt[v[i].second]++;
}
return 0;
}
No comments:
Post a Comment