QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#546974#2583. 战略游戏wangyangjena0 1995ms66752kbC++143.5kb2024-09-04 16:23:152024-09-04 16:23:16

Judging History

This is the latest submission verdict.

  • [2024-09-04 16:23:16]
  • Judged
  • Verdict: 0
  • Time: 1995ms
  • Memory: 66752kb
  • [2024-09-04 16:23:15]
  • Submitted

answer

#include <bits/stdc++.h>
#define int long long
// #pragma GCC optimize(2)
using namespace std;
const int N = 2e5 + 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 lim = k;
            for(int j = 2; j <= k; j++){
                int lca = Lca(s[j - 1], s[j]);
                if(lca != s[j - 1] && lca != s[j]) s[++lim] = lca;
            }
            sort(s + 1, s + 1 + lim, cmp);
            lim = unique(s + 1, s + 1 + lim) - s - 1;

            int ans = 0;
            ans += (s[1] == 1 && tag[1] == 0);
            // cout << s[1] << " ";
            for(int j = 2; j <= lim; j++){
                int lca = Lca(s[j - 1], s[j]);
                ans += dis[s[j]] - dis[lca] - tag[s[j]];
                // cout << s[j] << " ";
            }
            cout << ans << "\n";

            for(int j = 1; j <= lim; 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;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 0
Wrong Answer
time: 1153ms
memory: 66752kb

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:

10841
1733
17002
6949
3081
12246
9063
5816
5911
4839
6920
32177
14220
2020
54095
13636
35977
54537
47758
8286
51352
47579
86029
46711
27894
32639
51745
10230
8399
11304
45142
52010
58471
438
2725
41589
8326

result:

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

Test #2:

score: 0
Wrong Answer
time: 1995ms
memory: 61316kb

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
9928
5064
5453
21134
1147
10520
8383
14579
4868
24196
19083
19547
52667
5751
8082
35025
36318
21654
4430
26616
29112
41250
5023
31322
34532
21541
18072
33179
30000
22532
43537
9528
14566
14622
20347
15861
5081
12382
16383
17131
7830
3421
7814
29927
2417
24145
8548
1450
33208
6269
1...

result:

wrong answer 4th lines differ - expected: '9929', found: '9928'

Test #3:

score: 0
Wrong Answer
time: 1786ms
memory: 62612kb

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
8700
1770
7276
10507
5824
5043
14371
2863
990
605
14038
9918
8374
5428
5440
3940
3676
1349
595
4747
969
1668
9438
9297
5550
3285
118
1563
7464
203
2499
779
81
7560
958
796
1640
6131
3499
5825
913
1371
2231
3855
4591
14438
13171
2932
1900
5191
3716
1154
11891
5730
13204
343
1800
2284
505
225
170...

result:

wrong answer 2nd lines differ - expected: '8701', found: '8700'