Codeforces Round #409 (rated, Div. 1, based on VK Cup 2017 Round 2), problem: (E) Verifying Kingdom Solution In C/C++

#define rep(x,a,b) for (int x=int(a);x<=(int)(b);x++)
#define drp(x,a,b) for (int x=int(a);x>=(int)(b);x–)
#define cross(x,a) for (int x=hd[a];~x;x=nx[x])
#define ll long long
#define inf (1<<29)
#define pii pair<int,int>
#define pdd pair<double,double>
#define mk(a,b) make_pair(a,b)
#define fs first
#define sc second
#define pb push_back
#define VI vector<int>
#define VS vector<string>
using namespace std;
inline ll rd(){
ll x=0;int ch=getchar(),f=1;
while (!isdigit(ch)&&(ch!=’-‘)&&(ch!=EOF)) ch=getchar();
if (ch==’-‘){f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-‘0’;ch=getchar();}
return x*f;
inline void rt(ll x){
if (x<0) putchar(‘-‘),x=-x;
if (x>=10) rt(x/10),putchar(x%10+’0′);
else putchar(x+’0′);
const int maxn=2005;
int n,nc;
int hd[maxn],nx[maxn<<1],to[maxn<<1],cnt;
void addedge(int u,int v){
int pa[maxn],ls[maxn],rs[maxn],ez[maxn];
int cen,ms[maxn],size[maxn];
int del[maxn],allsize,nwk;
void getcenter(int u,int fa){
int v=to[i];
if (v==fa||del[v]) continue;
if (u>n&&(!cen||ms[u]<ms[cen])) cen=u;
char ans[5];
void divide(int u){
printf(“%d %d %d\n”,ez[ls[u]],ez[rs[u]],nwk);
if (ans[0]==’X’){
if ((!~pa[u])||del[pa[u]]){
if (~pa[u]){
if (ls[pa[u]]==u) ls[pa[u]]=nc;else rs[pa[u]]=nc;
else allsize=size[pa[u]]<size[u]?size[pa[u]]:allsize-size[u],divide(pa[u]);
else if (ans[0]==’Z’){
if (ls[u]<=n||del[ls[u]]){
else allsize=size[ls[u]]<size[u]?size[ls[u]]:allsize-size[u],divide(ls[u]);
else if (ans[0]==’Y’){
if (rs[u]<=n||del[rs[u]]){
else allsize=size[rs[u]]<size[u]?size[rs[u]]:allsize-size[u],divide(rs[u]);
int main(){
rep(i,1,n) ez[i]=i;
memset(pa,-1,sizeof pa);
memset(hd,-1,sizeof hd);cnt=0;
rep(i,1,v-1) addedge(pa[i],i);
rep(i,n+1,nc) if (~pa[i]) addedge(pa[i],i);
memset(del,0,sizeof del);
rep(i,1,n*2-1) rt(pa[i]),putchar(‘ ‘);putchar(‘\n’);



Top 5 Metaverse Games...

  The metaverse is a virtual world where users can...

Forex Trading- The Best...

  Bringing in cash has for the most part been...

Music + Faith: 3...

Faith and music go hand in hand, with many...

Executive Producer John Lasseter...

In the past week, it was announced that Skydance...

Online Study Tips To...

The pandemic has changed the daily schedule for everyone...

Do you really need...

Changing a PDF format file into a Word file...

Top 5 Metaverse Games of 2023

  The metaverse is a virtual world where users can interact with each other and the environment in real time. It is a place where...

Forex Trading- The Best Way To Make Money Online

  Bringing in cash has for the most part been connected with and restricted to customary 'disconnected' courses. With the Internet progressing and being a...

Music + Faith: 3 Reasons to Book Christian Live Acts

Faith and music go hand in hand, with many saying they believe we connect so much better with religion when we're able to celebrate...

Executive Producer John Lasseter and Skydance Animation Team Up for the Movie Luck

In the past week, it was announced that Skydance animation studios is releasing their first movie since hiring animator, John Lasseter, as the head...

Online Study Tips To Help You Succeed

The pandemic has changed the daily schedule for everyone and how they go on with their lives. With offices and educational institutes now shut...

Do you really need a PDF editing tool?

Changing a PDF format file into a Word file or any other format could be a really frustrating task. While PDFs have become quite...

How to Benefit from the Services of a Tax Attorney

However, if your condition worsens, you may require the services of an attorney. Tax law is a specialized area of the law, and tax...

Tips on Buying Diamond Engagement Rings

When planning an engagement, one always want to get the best for their fiancée; thus, comparing and contrasting different options in the market is...

How To Create A Fun And Healthy Working Environment?

Everyone likes to work in a fun and engaging place that has a positive vibe in the work culture. This kind of atmosphere not...