QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#883732#9983. Color-Balanced TreeKafuuChinocppWA 3ms29124kbC++142.5kb2025-02-05 18:31:592025-02-05 18:31:59

Judging History

This is the latest submission verdict.

  • [2025-02-05 18:31:59]
  • Judged
  • Verdict: WA
  • Time: 3ms
  • Memory: 29124kb
  • [2025-02-05 18:31:59]
  • Submitted

answer

#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int max1 = 2e5;

int T, n, color[max1 + 5];
vector <int> edge[max1 + 5], son[max1 + 5];
vector <bool> piL[max1 + 5], piR[max1 + 5];
int f[max1 + 5];
bool g[max1 + 5];

void Dfs ( int now, int fa )
{
    son[now].clear();
    son[now].push_back(0);
    for ( auto v : edge[now] )
    {
        if ( v == fa )
            continue;
        
        Dfs(v, now);
        son[now].push_back(v);
    }

    int siz = son[now].size() - 1;
    piL[now].resize(siz + 2); piL[now][0] = 1;
    for ( int i = 1; i <= siz; i ++ )
        piL[now][i] = piL[now][i - 1] && f[son[now][i]];
    piR[now].resize(siz + 2); piR[now][siz + 1] = 1;
    for ( int i = siz; i >= 1; i -- )
        piR[now][i] = piR[now][i + 1] && f[son[now][i]];
    
    f[now] = 0;
    for ( int i = 1; i <= siz; i ++ )
        if ( piL[now][i - 1] && piR[now][i + 1] && g[son[now][i]] )
            { f[now] = son[now][i]; break; }
    
    g[now] = true;
    for ( int i = 1; i <= siz; i ++ )
        if ( !f[son[now][i]] )
            { g[now] = false; break; }
    return;
}

void Get_Color ( int now, bool flag )
{
    int siz = son[now].size() - 1;
    if ( flag )
    {
        color[now] = 0;
        for ( int i = 1; i <= siz; i ++ )
        {
            if ( son[now][i] == f[now] )
                continue;
            
            Get_Color(son[now][i], 1);
        }
        Get_Color(f[now], 0);
    }
    else
    {
        color[now] = 1;
        for ( int i = 1; i <= siz; i ++ )
            Get_Color(son[now][i], 1);
    }
    return;
}

void Work ()
{
    scanf("%d", &n); n <<= 1;
    for ( int i = 1; i <= n; i ++ )
        edge[i].clear();
    for ( int i = 2, u, v; i <= n; i ++ )
    {
        scanf("%d%d", &u, &v);
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    if ( n <= 6 )
    {
        for ( int i = 1; i <= (n >> 1); i ++ )
            printf("0 ");
        for ( int i = 1; i <= (n >> 1); i ++ )
            printf("1 ");
        printf("\n");
    }
    else
    {
        Dfs(1, 0);
        if ( f[1] )
        {
            Get_Color(1, 1);
            for ( int i = 1; i <= n; i ++ )
                printf("%d ", color[i]);
            printf("\n");
        }
        else
            printf("-1\n");
    }
    return;
}

int main ()
{
    scanf("%d", &T);
    while ( T -- )
        Work();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 29124kb

input:

4
3
1 2
2 3
2 4
4 5
4 6
3
1 2
1 3
1 4
1 5
1 6
4
1 2
2 3
3 4
4 5
5 6
6 7
7 8
5
1 2
1 4
4 3
4 5
5 6
5 8
8 7
8 9
9 10

output:

0 0 0 1 1 1 
0 0 0 1 1 1 
0 1 0 1 0 1 0 1 
0 1 1 0 0 1 1 0 0 1 

result:

wrong answer diff > 3 (test case 4)