QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#840989#9924. ReconstructionHuTaoWA 241ms114940kbC++144.4kb2025-01-03 10:59:502025-01-03 10:59:52

Judging History

你现在查看的是最新测评结果

  • [2025-01-03 10:59:52]
  • 评测
  • 测评结果:WA
  • 用时:241ms
  • 内存:114940kb
  • [2025-01-03 10:59:50]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 5, M = 2e6 + 5, K = 19;

int n;
int la[N], h[N], ne[M], en[M], idx;
int fa[N];
int in[N], out[N], dfst;
int s[N];
char res[N];
int dep[N], ff[N][K], mn[N][K], mx[N][K];

inline void Add(int la[], int a, int b)
{
    ne[ ++ idx] = la[a];
    la[a] = idx;
    en[idx] = b;
}

inline void DFS1(int u, int f) // 对原树找父亲
{
    fa[u] = f;
    for(int i = la[u]; i; i = ne[i])
    {
        int v = en[i];
        if(v != f) DFS1(v, u);
    }
}
inline void DFS2(int u, int f) // 对点分树求 DFS 序
{
    in[u] = ++ dfst;
    for(int i = h[u]; i; i = ne[i])
    {
        int v = en[i];
        if(v != f) DFS2(v, u);
    }
    out[u] = dfst;
}

inline void Work0()
{
    dfst = 0, DFS1(1, 0), DFS2(1, 0);
    // for(int i = 1; i <= n; i ++ ) printf("%d ", in[i]);
    // puts("");
    for(int u = 1; u <= n; u ++ )
    {
        for(int i = h[u]; i; i = ne[i])
        {
            int v = en[i];
            if(in[v] > in[u])
            {
                if(u != fa[v] && v != fa[u])
                {
                    int x = max(in[fa[u]], in[fa[v]]);
                    // printf("#%d %d %d\n", u, v, x);
                    if(in[v] <= x && x <= out[v])
                    {
                        s[in[v]] ++ , s[out[v] + 1] -- ;
                    }
                    else
                    {
                        s[1] ++ , s[in[v]] -- , s[out[v] + 1] ++ ;
                    }
                }
            }
        }
    }
    for(int i = 1; i <= n; i ++ ) s[i] += s[i - 1];
    for(int i = 1; i <= n; i ++ ) res[i] = (!s[in[i]]) | 48;
}

inline void DFS4(int u, int f) // 对原树进行预处理
{
    ff[u][0] = f, dep[u] = dep[f] + 1;
    mn[u][0] = mx[u][0] = in[u];
    for(int i = 1; i < K; i ++ )
    {
        ff[u][i] = ff[ff[u][i - 1]][i - 1];
        mn[u][i] = min(mn[u][i - 1], mn[ff[u][i - 1]][i - 1]);
        mx[u][i] = min(mx[u][i - 1], mx[ff[u][i - 1]][i - 1]);
    }
    for(int i = la[u]; i; i = ne[i])
    {
        int v = en[i];
        if(v != f) DFS4(v, u);
    }
}
inline int LCA(int u, int v)
{
    if(dep[u] < dep[v]) swap(u, v);
    for(int i = K - 1; i >= 0; i -- )
        if(dep[ff[u][i]] >= dep[v])
            u = ff[u][i];
    if(u == v) return u;
    for(int i = K - 1; i <= 0; i -- )
        if(ff[u][i] != ff[v][i])
            u = ff[u][i], v = ff[v][i];
    return ff[u][0];
}
inline void FindKth(int u, int k, int &mmn, int &mmx)
{
    for(int i = 0; i < K; i ++ )
        if(k >> i & 1)
        {
            mmn = min(mmn, mn[u][i]);
            mmx = max(mmx, mx[u][i]);
            u = ff[u][i];
        }
}
inline void Find(int u, int v, int &mmn, int &mmx)
{
    int w = LCA(u, v);
    if(w == v)
    {
        FindKth(u, dep[u] - dep[v], mmn, mmx);
    }
    else
    {
        FindKth(u, dep[u] - dep[w] + 1, mmn, mmx);
        FindKth(ff[v][0], dep[v] - dep[w], mmn, mmx);
    }
}

inline bool Work1(int rt)
{
    dfst = 0, DFS2(rt, 0);
    for(int u = 1; u <= n; u ++ )
    {
        for(int i = h[u]; i; i = ne[i])
        {
            int v = en[i];
            if(in[v] > in[u])
            {
                if(u != fa[v] && v != fa[u])
                {
                    int mn = n, mx = 1;
                    Find(v, u, mn, mx);
                    // printf("!%d %d %d %d\n", u, v, mn, mx);
                    if(mn < in[v] || mx > out[v]) return 0;
                }
            }
        }
    }
    return 1;
}

inline void Solve()
{
    scanf("%d", &n);
    for(int i = 0; i <= n + 1; i ++ )
    {
        la[i] = h[i] = s[i] = res[i] = 0;
        for(int j = 0; j < K; j ++ )
        {
            mn[i][j] = n;
            mx[i][j] = 1;
        }
    }
    idx = 0;
    for(int i = 1; i < n; i ++ )
    {
        int a ,b;
        scanf("%d%d", &a, &b);
        Add(la, a, b), Add(la, b, a);
    }
    for(int i = 1; i < n; i ++ )
    {
        int a ,b;
        scanf("%d%d", &a, &b);
        Add(h, a, b), Add(h, b, a);
    }
    Work0();
    int rt = 1;
    while(rt <= n && res[rt] == '0') rt ++ ;
    // printf("#%d\n", rt);
    if(rt <= n && !Work1(rt))
    {
        for(int i = 1; i <= n; i ++ ) res[i] = '0';
    }
    puts(res + 1);
}

int main()
{
    int T;
    T = 1;
    while(T -- ) Solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 22192kb

input:

3
1 2
2 3
2 1
1 3

output:

001

result:

ok single line: '001'

Test #2:

score: 0
Accepted
time: 0ms
memory: 20208kb

input:

6
1 3
3 4
3 6
4 5
5 2
1 3
1 4
4 5
5 2
3 6

output:

010110

result:

ok single line: '010110'

Test #3:

score: 0
Accepted
time: 0ms
memory: 20208kb

input:

1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 22132kb

input:

2
1 2
1 2

output:

11

result:

ok single line: '11'

Test #5:

score: -100
Wrong Answer
time: 241ms
memory: 114940kb

input:

500000
321614 78209
64619 204431
81539 336200
128603 201377
132521 391792
41587 137224
174146 381680
341915 451206
493371 256005
259794 168656
161914 462290
105839 333679
377214 267008
283062 457340
219692 196741
123276 321510
473789 410796
498171 203543
178249 456921
255509 449152
294196 457566
277...

output:

000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

result:

wrong answer 1st lines differ - expected: '000000000000000000000000000000...0000000000000000000000000000000', found: '000000000000000000000000000000...0000000000000000000000000000000'