Codeforces Round #383 (Div. 2), problem: (E) Arpa’s overnight party and Mehrdad’s silent entering Solution in C/C++

#include <stdio.h>
#include <string.h>

int n,a[200010],b[200010],c[200010],p[200010];
void dfs(int k,int col);

int main(void)
{
    //freopen("party.in","r",stdin);
    //freopen("party.out","w",stdout);
    scanf("%d",&n);
    int i;
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        p[a[i]]=b[i];  p[b[i]]=a[i];
    }
    memset(c,0,sizeof(c));
    for(i=1;i<=n*2;i++)
        if(!c[i]) dfs(i,0);
    for(i=1;i<=n;i++)
        printf("%d %d\n",c[a[i]],c[b[i]]);
    return 0;
}

void dfs(int k,int col)
{
    c[k]=col+1;
    if(!c[((k-1)^1)+1])
        dfs(((k-1)^1)+1,col^1);
    else if(!c[p[k]]) dfs(p[k],col^1);
    else return;
}
#include <stdio.h>
#include <string.h>

int n,a[200010],b[200010],c[200010],p[200010];
void dfs(int k,int col);

int main(void)
{
    //freopen("party.in","r",stdin);
    //freopen("party.out","w",stdout);
    scanf("%d",&n);
    int i;
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        p[a[i]]=b[i];  p[b[i]]=a[i];
    }
    memset(c,0,sizeof(c));
    for(i=1;i<=n*2;i++)
        if(!c[i]) dfs(i,0);
    for(i=1;i<=n;i++)
        printf("%d %d\n",c[a[i]],c[b[i]]);
    return 0;
}

void dfs(int k,int col)
{
    c[k]=col+1;
    if(!c[((k-1)^1)+1])
        dfs(((k-1)^1)+1,col^1);
    else if(!c[p[k]]) dfs(p[k],col^1);
    else return;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Indian Army Organises Inter-Government Degree College Skit Competition in Kupwara

In a vibrant showcase of youthful talent, creativity, and social awareness, the Indian Army successfully organized an Inter-Government Degree College Skit Competition at the...

Best Work-From-Home & Productivity Tech Gifts (2026)

The Best Work-From-Home Tech Gifts for 2026 The home office has become a permanent fixture of modern professional life. Whether your recipient has been working...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!