QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#153231#6847. Hide-And-Seek Gamejrjyy#AC ✓74ms3720kbC++202.7kb2023-08-29 18:24:442023-08-29 18:24:46

Judging History

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

  • [2023-08-29 18:24:46]
  • 评测
  • 测评结果:AC
  • 用时:74ms
  • 内存:3720kb
  • [2023-08-29 18:24:44]
  • 提交

answer

/* a.cpp */
#include <bits/stdc++.h>

using i64 = long long;

int exgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int g = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
}

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<std::vector<int>> adj(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;
        adj[u].push_back(v), adj[v].push_back(u);
    }

    std::vector<int> dep(n), fa(n, -1);
    auto dfs = [&](auto self, int u) -> void {
        for (auto v : adj[u]) if (v != fa[u]) {
            dep[v] = dep[u] + 1;
            fa[v] = u;
            self(self, v);
        }
    };
    dfs(dfs, 0);

    auto apply = [&](auto &f, int s, int t) {
        std::fill(f.begin(), f.end(), std::array{-1, -1});
        int x = s, y = t, l = 0;
        while (x != y) {
            if (dep[x] >= dep[y]) {
                x = fa[x];
            } else {
                y = fa[y];
            }
            l += 1;
        }

        int p = 0, q = 0;
        while (s != t) {
            if (dep[s] >= dep[t]) {
                f[s] = {p, 2 * l - p};
                s = fa[s];
                p += 1;
            } else {
                f[t] = {l - q, l + q};
                t = fa[t];
                q += 1;
            }
        }
        f[s] = {p, 2 * l - p};

        return 2 * l;
    };

    std::vector<std::array<int, 2>> f(n), g(n);
    while (m--) {
        int sa, ta, sb, tb;
        std::cin >> sa >> ta >> sb >> tb;
        --sa, --ta, --sb, --tb;

        int la = apply(f, sa, ta), lb = apply(g, sb, tb);
        int x, y, z = exgcd(la, lb, x, y);

        std::pair<int, int> ans{1e9, -2};
        for (int i = 0; i < n; ++i) {
            if (f[i][0] == -1 || g[i][0] == -1) {
                continue;
            }
            for (auto a : f[i]) {
                for (auto b : g[i]) {
                    a %= la, b %= lb;
                    if ((a - b) % z) {
                        continue;
                    }
                    int res = (b - a) / z * x % (lb / z) * la + a;
                    res %= lb / z * la;
                    if (res < 0) {
                        res += lb / z * la;
                    }
                    ans = std::min(ans, {res, i});
                }
            }
        }

        std::cout << ans.second + 1 << '\n';
    }
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 74ms
memory: 3720kb

input:

500
6 20
1 2
1 3
3 4
2 5
3 6
1 6 6 5
2 1 6 2
5 1 2 6
2 6 1 2
6 3 6 4
4 1 1 3
5 6 3 4
2 1 3 5
2 4 1 2
6 2 1 2
6 2 1 4
1 6 1 6
2 5 2 6
2 5 6 2
5 2 1 3
5 6 6 4
4 1 4 5
5 4 4 1
3 6 1 2
6 1 4 3
71 30
1 2
2 3
2 4
4 5
2 6
6 7
4 8
2 9
1 10
9 11
3 12
5 13
5 14
10 15
6 16
13 17
17 18
4 19
5 20
17 21
20 22
4 2...

output:

3
-1
-1
-1
6
3
-1
1
-1
1
3
1
2
-1
-1
3
4
1
-1
3
-1
11
2
-1
5
-1
17
-1
-1
5
-1
-1
-1
-1
2
-1
5
53
5
7
-1
-1
2
4
-1
-1
-1
-1
-1
-1
-1
5
-1
1
5
1
-1
-1
-1
-1
33
5
1
1
-1
1
-1
-1
-1
-1
7
-1
-1
9
5
3
-1
-1
-1
19
-1
-1
6
5
-1
-1
5
-1
-1
-1
-1
1
-1
7
-1
-1
1
-1
-1
-1
8
-1
13
-1
-1
-1
-1
4
5
-1
-1
5
-1
-1
8...

result:

ok 53199 lines