Sponsors

Codeforces Round #381 (Div. 1), problem: (D) Recover a functional graph Solution in C/C++

Hi guys , I just solved the fourth problem of round 381 , Hope you like it , feel free to comment any better solution .

 

 

#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<vector>
#define N 310
#define pb push_back
using namespace std;
struct node{int p,c,id;};
vector<node> A[N],B[N],C[N],D;
int n,mx[N],f[N][N],need[N],can[N][N],has[N],a[N],num,ans[N];
bool v[N],b[N];
int read()
{
int t=0;char c=getchar();
while(c!=’?’ && (c<‘0′ || c>’9′)) c=getchar();
if(c==’?’) return -1;
while(c>=’0′ && c<=’9′) {t=t*10+c-‘0’;c=getchar();}
return t;
}
void print()
{
for(int i=1;i<=n;i++) printf(“%d “,ans[i]);
}
int solve(int o)
{
for(int i=1;i<=n;i++) ans[i]=0;
int cnt=0,siz;
for(int i=1;i<=n;i++)
for(int j=0;j<=mx[i];j++)
f[i][j]=0;
for(int i=1;i<=n;i++)
{
if(v[i]==0) continue;
int cir=0;
siz=A[i].size();
for(int j=0;j<siz;j++)
{
int p=A[i][j].p;
if(f[i][p]==0) f[i][p]=A[i][j].id;
if(p==0) cir++;
}
if(cir==0) need[i]=i;
else if(cir%i==0) need[i]=0;
else need[i]=i-cir%i;
can[i][0]=min(need[i],(int)(C[0].size()));
for(int j=1;j<=mx[i];j++) if(f[i][j]==0) need[i]++;
for(int j=1;j<=mx[i];j++)
{
can[i][j]=can[i][j-1];
if(f[i][j]==0 && C[j].size()) can[i][j]++;
}
}
for(int i=1;i<=n;i++) has[i]=B[i].size();
for(int i=1;i<=n;i++) b[i]=0;
for(int i=n;i>=1;i–)
{
siz=C[i].size();int now=0;
for(int j=1;j<=n;j++)
{
if(v[j]==0 || mx[j]<i || f[j][i]) continue;
if(now==siz) break;
if(has[j]>=need[j]) continue;
if(has[j]+can[j][i]<=need[j]) {f[j][i]=C[i][now].id;now++;need[j]–;}
}
for(int j=1;j<=n;j++)
{
if(v[j]==0 || mx[j]<i || f[j][i]) continue;
if(now==siz) break;
if(has[j]>=need[j]) continue;
f[j][i]=C[i][now].id;now++;
need[j]–;
}
for(int j=1;j<=n;j++)
{
if(v[j]==0 || mx[j]<i || f[j][i]) continue;
if(has[j]>0) {int k=B[j].size()-has[j];f[j][i]=B[j][k].id;has[j]–;}
else if(cnt<D.size()) {f[j][i]=D[cnt].id;cnt++;}
else return 0;
need[j]–;
}
}
int now=0;
for(int i=1;i<=n;i++)
{
if(v[i]==0) continue;
siz=A[i].size();num=0;
for(int j=0;j<siz;j++) if(A[i][j].p==0) a[++num]=A[i][j].id;
while(now<C[0].size() && has[i]<need[i])
{
a[++num]=C[0][now].id;now++;
need[i]–;
}
if(has[i]+D.size()-cnt<need[i]) return 0;
while(need[i]>0 && has[i]>0)
{
int k=B[i].size()-has[i];has[i]–;
a[++num]=B[i][k].id;
need[i]–;
}
while(need[i]>0)
{
a[++num]=D[cnt].id;cnt++;
need[i]–;
}
for(int j=1;j<=num;j++)
{
if(f[i][0]==0) f[i][0]=a[j];
if(j%i) ans[a[j]]=a[j+1];
else ans[a[j]]=a[j-i+1];
}
for(int j=1;j<=mx[i];j++) ans[f[i][j]]=f[i][j-1];
siz=A[i].size();
for(int j=0;j<siz;j++)
if(ans[A[i][j].id]==0) ans[A[i][j].id]=f[i][A[i][j].p-1];
siz=B[i].size();
for(int j=0;j<siz;j++)
if(ans[B[i][j].id]==0) ans[B[i][j].id]=f[i][0];
}

for(int i=1;i<=n;i++)
{
siz=C[i].size();
for(int j=0;j<siz;j++)
if(ans[C[i][j].id]==0) ans[C[i][j].id]=f[o][i-1];
}

num=0;
siz=C[0].size();
for(int i=0;i<siz;i++) if(ans[C[0][i].id]==0) a[++num]=C[0][i].id;
siz=D.size();
for(int i=cnt;i<siz;i++) a[++num]=D[i].id;
for(int i=1;i<num;i++) ans[a[i]]=a[i+1];
ans[a[num]]=a[1];

return 1;
}
int main()
{
scanf(“%d”,&n);
int maxd=0;
bool bo=0;
for(int i=1;i<=n;i++)
{
int p,c;p=read();c=read();
maxd=max(maxd,p);
node t=(node){p,c,i};
if(c!=-1 && p!=-1) A[c].pb(t);
else if(c!=-1 && p==-1) B[c].pb(t);
else if(c==-1 && p!=-1) C[p].pb(t);
else D.pb(t);
if(c!=-1) v[c]=bo=1,mx[c]=max(mx[c],p);
}
if(bo==0) v[1]=1;
for(int i=1;i<=n;i++)
{
if(v[i]==0) continue;
int t=mx[i];
mx[i]=max(mx[i],maxd);
if(solve(i)) {print();return 0;}
mx[i]=t;
}
printf(“-1\n”);
return 0;
}

How Dhurandhar’s repurposed qawwali...

The Forgotten Heritage: How Dhurandhar’s Qawwali Connects to Sufi...

Explaining what exotic matter,...

Understanding the Core Science: Exotic Matter, The Bridge, and...

Chinese economy rides the...

The Rise of the Consumer: China’s Economy Finds New...

Google-Backed Fleet Tracking Firm...

Motive Technologies, Google-Backed Fleet Tracker, Files Publicly for IPO The...

FDA greenlights Wegovy in...

The Game Changer: FDA Greenlights Wegovy in Convenient Pill...

The Road Ends at...

The Road Ends at the Mother Church: Finale Preview The...

How Dhurandhar’s repurposed qawwali with Pakistani origins engages with a shared cultural past, one the film ignores

The Forgotten Heritage: How Dhurandhar’s Qawwali Connects to Sufi Masters and a Shared Cultural Past The contemporary cinematic landscape often borrows and repurposes older melodies,...

Explaining what exotic matter, the bridge, and the wall are in Stranger Things season 5

Understanding the Core Science: Exotic Matter, The Bridge, and The Wall in Stranger Things 5 As Stranger Things races toward its final, thrilling conclusion, Season...

Chinese economy rides the crest of new consumer phenomena

The Rise of the Consumer: China’s Economy Finds New Momentum For years, discussions surrounding China’s post-pandemic economic recovery and its path toward growth have been...

Google-Backed Fleet Tracking Firm Motive Files Publicly For IPO

Motive Technologies, Google-Backed Fleet Tracker, Files Publicly for IPO The highly anticipated move by Motive Technologies Inc., a leading name in AI-enabled fleet management software,...

FDA greenlights Wegovy in pill form

The Game Changer: FDA Greenlights Wegovy in Convenient Pill Form The landscape of chronic weight management has just seen a monumental shift. The U.S. Food...

The Road Ends at “Ryman Auditorium, Nashville, TN”: Our Finale Preview

The Road Ends at the Mother Church: Finale Preview The journey has been long, but the road finally reaches its destination tonight. The highly anticipated...

‘This is immoral’: Public Health experts respond to funding cuts to science & medicine

Public Health Experts Decry "Immoral" Funding Cuts to NIH and Science The scientific community is voicing alarm following significant funding cuts targeting key medical research...

Putin sticks to Russian demands on Ukraine, says EU robbery failed

Putin Reaffirms Unyielding Stance on Ukraine Peace Terms In a recent statement that eliminated any immediate hope for a softened diplomatic approach, Russian President Vladimir...

The Hobby Awards: Complete Winners List and Recap

The Votes Are In: A Complete Recap of The Hobby Awards Winners The collectible industry, often referred to simply as "The Hobby," achieved a major...