QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#593903#6559. A Tree and Two Edgesticking_away#WA 137ms10732kbC++172.3kb2024-09-27 16:47:312024-09-27 16:47:32

Judging History

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

  • [2024-09-27 16:47:32]
  • 评测
  • 测评结果:WA
  • 用时:137ms
  • 内存:10732kb
  • [2024-09-27 16:47:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int MXN = 5e4 + 7;

int n, q, far[MXN][21], dpt[MXN];

vector<int> g[MXN];

int val[2][2], fg;

bool vs[MXN];
void build_tre(int u) {
    dpt[u] = dpt[far[u][0]] + 1;
    for(int k=1; k<21; ++k)
        far[u][k] = far[far[u][k-1]][k-1];
    vs[u] = 1;
    for(int v:g[u]) {
        if(!vs[v]) {
            far[v][0] = u;
            build_tre(v);
        } else if(v>u) {
            if(fg) val[1][0] = u, val[1][1] = v;
            else val[0][0] = u, val[0][1] = v, fg = 1;
        }
    }
}

int get_far(int u, int ds) {
    for(int cnt=0; ds; ds>>=1) {
        if(ds&1) {
            u = far[u][cnt];
            cnt = 0;
        } else ++cnt;
    }
    return u;
}

int get_lca(int l, int r) {
    if(dpt[l]<dpt[r]) swap(l, r);
    l = get_far(l, dpt[l]-dpt[r]);
    if(l==r) return l;
    for(int k=20; k>=0; --k) {
        if(far[l][k]!=far[r][k]) {
            l = far[l][k];
            r = far[r][k];
        }
    }
    return far[l][0];
}

bool adj(int a, int b, int c, int d) {
    int arr[] = {get_lca(a, c), get_lca(a, d), get_lca(b, c), get_lca(b, d)};
    sort(arr, arr+4, [](int l, int r){ return dpt[l]>dpt[r]; });
    return (arr[0]!=arr[1])||(dpt[arr[0]]>=max(dpt[get_lca(a, b)], dpt[get_lca(c, d)]));
}

void solve() {
    for(int i=0, u, v, cnt; i<q; ++i) {
        cin >> u >> v;
        cnt = 1;
        for(int j=0; j<=1; ++j) {
            if(!adj(u, val[j][0], v, val[j][1])||!adj(u, val[j][1], v, val[j][0]))
                ++cnt;
        }
        for(int j=0; j<=1; ++j) {
            for(int k1=0; k1<=1; ++k1) {
                for(int k2=0; k2<=1; ++k2) {
                    if(!adj(u, val[j][k1], val[j][!k1], val[!j][k2])
                        &&!adj(val[j][!k1], val[!j][k2], val[!j][!k2], v)
                        &&!adj(u, val[j][k1], val[!j][!k2], v)) {
                            ++cnt;
                        }
                }
            }
        }
        cout << cnt << '\n';
    }
}

void pre() {
    cin >> n >> q;
    for(int i=0, u, v; i<=n; ++i) {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    far[1][0] = 1;
    build_tre(1);
}

int t_=1;
void init() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    // cin >> t_;
}

int main() {
    init();
    for(int i=1; i<=t_; ++i) {
        pre(); solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 6728kb

input:

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

output:

3
3
3
3
3
4

result:

ok 6 lines

Test #2:

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

input:

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

output:

2
2
4
1

result:

ok 4 lines

Test #3:

score: -100
Wrong Answer
time: 137ms
memory: 10732kb

input:

50000 50000
11561 23122
14261 28523
24407 48814
17947 35894
14803 29607
19557 39115
12415 24830
9583 19167
18397 36794
439 878
18040 36080
17150 34300
7922 15845
18938 37877
18625 37250
6459 12919
9818 19636
3579 7158
21323 42646
23882 47764
13603 27207
8353 16707
15907 31814
20935 41871
11686 23372...

output:

1
1
2
2
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
2
2
1
1
1
1
2
1
1
1
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
2
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
1
1
1
1
1
2
2
1
1
2
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
2
2
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
2
1
1
1
...

result:

wrong answer 1st lines differ - expected: '4', found: '1'