编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#4866 #1027. 艾泽拉斯的灾难 Time Limit Exceeded 0 20138 ms 11584 K C++ 17 / 2.1 K TosakaUCW 2024-02-20 20:47:42
显示原始代码
#include <bits/stdc++.h>
// #define int long long
#define pb push_back

using std::cin, std::cout, std::string;
int read(int x = 0, int f = 0, char ch = getchar()) {
    while (ch < 48 or 57 < ch) f = ch == 45, ch = getchar();
    while (48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar();
    return f ? -x : x;
}
const int N = 1e5 + 5;
const int INF = 1 << 30;
// const long long INF = 1LL << 60;
int n;
int fa[N], dep[N], siz[N], son[N], top[N], dp[N], fp[N];
std::vector<int> g[N];
void dfs1(int u, int fa) {
    siz[u] = 1, ::fa[u] = fa, dep[u] = dep[fa] + 1, son[u] = 0;
    for (auto v : g[u])
        if (v ^ fa)
            dfs1(v, u), siz[u] += siz[v], son[u] = siz[v] > siz[son[u]] ? v : son[u];
}
void dfs2(int u) {
    top[u] = u == son[fa[u]] ? top[fa[u]] : u;
    if (son[u])
        dfs2(son[u]);
    for (auto v : g[u])
        if (v ^ son[u] and v ^ fa[u])
            dfs2(v), dp[u] += dp[v] + siz[v];
}
void dfs3(int u, int sum) {
    for (auto v : g[u])
        if (v ^ fa[u])
            fp[v] = fp[u] + sum + siz[u] - siz[v], dfs3(v, sum + siz[u] - siz[v]);
}
int LCA(int x, int y) {
    for (; top[x] ^ top[y]; x = fa[top[x]])
        if (dep[top[x]] < dep[top[y]])
            std::swap(x, y);
    return dep[x] < dep[y] ? x : y;
}
void solve() {
    n = read();
    for (int u, v, i = 1; i < n; i++) u = read(), v = read(), g[u].pb(v), g[v].pb(u);
    dfs1(1, 0), dfs2(1), dfs3(1, 0);
    int ans = 0, res = 0;
    for (int m = read(); m--;) {
        int x = (read() ^ res + res) % n + 1, y = (read() ^ res + res) % n + 1;
        int lca = LCA(x, y);
        if (dep[x] < dep[y])
            std::swap(x, y);
        int dis = 2 * dep[lca] - dep[x] - dep[y];
        int middle = dis / 2;
        int newx = x;
        for (; middle > dep[top[newx]] - dep[newx]; middle -= dep[top[newx]] - dep[newx], x = fa[top[x]])
            ;
        for (; middle--; newx = fa[newx])
            ;
        res = dp[x] + dp[y] + dp[1] - dp[lca] + fp[x] - fp[newx] + fp[newx] - fp[lca] + fp[y] - fp[lca];
        ans = ans ^ (res * res);
    }
    cout << ans;
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("B.in", "r", stdin);
#endif
    solve();
    return 0;
}
子任务 #1
Time Limit Exceeded
得分:0
测试点 #1
Time Limit Exceeded
得分:0
用时:2009 ms
内存:1808 KiB

输入文件(1.in

5000
1 2
1 3
1 4
2 5
5 6
5 7
5 8
7 9
4 10
8 11
11 12
12 13
10 14
13 15
7 16
10 17
9 18
1 19
19 20
20
<45608 bytes omitted>

答案文件(1.out

137
测试点 #2
Time Limit Exceeded
得分:0
用时:2001 ms
内存:1692 KiB

输入文件(2.in

5000
1 2
2 3
3 4
2 5
5 6
5 7
5 8
5 9
2 10
1 11
8 12
10 13
9 14
1 15
1 16
11 17
16 18
3 19
3 20
9 21

<45718 bytes omitted>

答案文件(2.out

761
测试点 #3
Time Limit Exceeded
得分:0
用时:2011 ms
内存:1764 KiB

输入文件(3.in

5000
1 2
1 3
3 4
4 5
2 6
2 7
5 8
4 9
3 10
8 11
3 12
1 13
4 14
5 15
1 16
13 17
16 18
14 19
9 20
15 21
<45570 bytes omitted>

答案文件(3.out

349
测试点 #4
Time Limit Exceeded
得分:0
用时:2018 ms
内存:6160 KiB

输入文件(4.in

100000
1 2
2 3
1 4
1 5
3 6
6 7
7 8
7 9
6 10
6 11
10 12
8 13
11 14
9 15
7 16
7 17
10 18
4 19
8 20
18 
<1149345 bytes omitted>

答案文件(4.out

876
测试点 #5
Time Limit Exceeded
得分:0
用时:2004 ms
内存:11584 KiB

输入文件(5.in

100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19
<1177707 bytes omitted>

答案文件(5.out

4049854893
测试点 #6
Time Limit Exceeded
得分:0
用时:2020 ms
内存:8212 KiB

输入文件(6.in

100000
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1
<1051036 bytes omitted>

答案文件(6.out

260293561
测试点 #7
Time Limit Exceeded
得分:0
用时:2009 ms
内存:8176 KiB

输入文件(7.in

100000
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1
<1050922 bytes omitted>

答案文件(7.out

211943169
测试点 #8
Time Limit Exceeded
得分:0
用时:2046 ms
内存:8144 KiB

输入文件(8.in

100000
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1
<1051051 bytes omitted>

答案文件(8.out

1337740188
测试点 #9
Time Limit Exceeded
得分:0
用时:2005 ms
内存:8144 KiB

输入文件(9.in

100000
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1
<1050939 bytes omitted>

答案文件(9.out

335272065
测试点 #10
Time Limit Exceeded
得分:0
用时:2015 ms
内存:8184 KiB

输入文件(10.in

100000
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1
<1050947 bytes omitted>

答案文件(10.out

743458136