QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#546993#2583. 战略游戏wangyangjena100 ✓2299ms134680kbC++143.2kb2024-09-04 16:33:542024-09-04 16:33:54

Judging History

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

  • [2024-09-04 16:33:54]
  • 评测
  • 测评结果:100
  • 用时:2299ms
  • 内存:134680kb
  • [2024-09-04 16:33:54]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
// #pragma GCC optimize(2)
using namespace std;
const int N = 1e6 + 10, MOD = 998244353;

int t;
int n, m, q;
int u, v, k, s[N];
vector <int> e[N], ee[N], g[N];

int dfn[N], low[N], cdfn, cnt;
vector <int> st;

void Tarjan(int x, int y){
    dfn[x] = low[x] = ++cdfn;
    st.push_back(x);

    for(auto i : e[x]){
        if(!dfn[i]){
            Tarjan(i, x);
            low[x] = min(low[x], low[i]);

            if(low[i] == dfn[x]){
                ++cnt;

                int tp = st.back();
                do{
                    tp = st.back();
                    st.pop_back();

                    ee[cnt].push_back(tp);
                    ee[tp].push_back(cnt);
                    // cout << cnt << " " << tp << "\n";
                }while(tp != i);

                ee[cnt].push_back(x);
                ee[x].push_back(cnt);
                // cout << cnt << " " << x << "\n";
            }
        }else{
            low[x] = min(low[x], dfn[i]);
        }
    }
}

int si[N], son[N], top[N], fa[N], dep[N];

void Dfs1(int x, int y){
    si[x] = 1;
    fa[x] = y;
    dep[x] = dep[y] + 1;

    for(auto i : ee[x]){
        if(i == y) continue;
        Dfs1(i, x);

        si[x] += si[i];
        if(si[i] > si[son[x]]) son[x] = i;
    }
}

void Dfs2(int x, int tp){
    top[x] = tp;

    if(son[x]) Dfs2(son[x], tp);
    for(auto i : ee[x]){
        if(top[i]) continue;
        Dfs2(i, i);
    }
}

int Lca(int x, int y){
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]){
            swap(x, y);
        }
        x = fa[top[x]];
    }

    if(dep[x] < dep[y]) return x;
    else return y;
}

bool cmp(int x, int y){
    return dfn[x] < dfn[y];
}

int dis[N];
bool tag[N];

void Get_dis(int x, int y){
    dfn[x] = ++cdfn;
    dis[x] = dis[y] + (1 <= x && x <= n);
    for(auto i : ee[x]){
        if(i == y) continue;
        Get_dis(i, x);
    }
}

signed main(){
    // freopen("1.in", "r", stdin);

    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 1; i <= m; i++){
            cin >> u >> v;
            e[u].push_back(v);
            e[v].push_back(u);
        }
        cnt = n;

        Tarjan(1, 0);
        Dfs1(1, 0);
        Dfs2(1, 1);

        cdfn = 0;
        Get_dis(1, 0);

        cin >> q;
        for(int i = 1; i <= q; i++){
            cin >> k;
            for(int j = 1; j <= k; j++){
                cin >> s[j];
                tag[s[j]] = 1;
            }
            sort(s + 1, s + 1 + k, cmp);

            int ans = -2 * k;
            for(int j = 1; j <= k; j++){
                int lca = Lca(s[j], s[j % k + 1]);
                ans += dis[s[j]] + dis[s[j % k + 1]] - 2 * dis[lca];
            }
            if(Lca(s[1], s[k]) <= n) ans += 2;
            cout << ans / 2 << "\n";

            for(int j = 1; j <= k; j++) tag[s[j]] = 0;
        }

        for(int i = 1; i <= cnt; i++){
            e[i].clear();
            ee[i].clear();
            dis[i] = top[i] = fa[i] = son[i] = si[i] = 0;
            dfn[i] = low[i] = 0;
        }
        cdfn = 0;
    }
}

詳細信息


Pretests


Final Tests

Test #1:

score: 30
Accepted
time: 1485ms
memory: 134680kb

input:

10
94814 186251
42720 65317
33135 40188
90524 91057
15157 59320
56261 84256
49874 66210
18118 38018
34500 43670
14570 37867
53678 90002
13331 40076
27677 48562
36311 65435
29685 49754
22695 61837
59763 84023
2954 70002
18405 72897
7652 64325
69023 79028
27045 81899
26653 32017
1461 81255
6241 78409
...

output:

10842
1733
17003
6950
3081
12247
9063
5817
5912
4840
6920
32177
14220
2021
54095
13636
35977
54537
47758
8286
51352
47579
86029
46711
27894
32639
51745
10230
8399
11304
45143
52011
58472
439
2726
41590
8326

result:

ok 37 lines

Test #2:

score: 45
Accepted
time: 2299ms
memory: 128432kb

input:

10
98254 198477
14613 72237
63083 73551
14567 32237
32006 56998
14940 52592
52150 61985
3972 84462
19680 42800
9518 55226
52089 66431
42873 68721
4698 93512
53178 79801
11807 42183
45906 77619
45069 47574
16109 83638
19343 49330
30148 51345
73355 83865
37818 69769
54127 92904
32072 92335
7056 71015
...

output:

32214
45632
10703
9929
5065
5453
21134
1147
10520
8383
14579
4868
24196
19083
19547
52667
5751
8082
35026
36318
21654
4430
26616
29113
41250
5023
31323
34532
21542
18072
33179
30000
22532
43537
9529
14566
14622
20347
15861
5081
12382
16383
17131
7830
3422
7814
29927
2417
24145
8548
1450
33208
6269
1...

result:

ok 1000000 lines

Test #3:

score: 25
Accepted
time: 1873ms
memory: 131812kb

input:

10
46832 199154
1937 21636
422 25158
5067 12645
42564 46740
4459 16622
3322 11404
10269 16390
28904 37319
24540 25151
21880 37822
12789 18753
7634 16767
25495 38865
36915 43797
26907 45920
31649 35332
18827 38743
16391 32795
5736 42007
3936 31488
1622 13275
2420 19285
19601 41219
4797 20725
12352 45...

output:

3339
8701
1771
7277
10508
5825
5044
14372
2864
991
606
14039
9919
8375
5429
5441
3941
3677
1350
596
4748
970
1669
9439
9298
5551
3286
118
1563
7465
204
2500
780
82
7560
959
797
1640
6131
3500
5826
914
1371
2232
3856
4591
14439
13172
2933
1900
5192
3717
1155
11892
5731
13205
344
1801
2285
506
225
170...

result:

ok 341666 lines