QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#462665#6888. TeyvatFortitudeWA 2011ms226528kbC++234.3kb2024-07-03 23:43:332024-07-03 23:43:33

Judging History

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

  • [2024-07-03 23:43:33]
  • 评测
  • 测评结果:WA
  • 用时:2011ms
  • 内存:226528kb
  • [2024-07-03 23:43:33]
  • 提交

answer

#include <bits/stdc++.h>
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define f first
#define s second
using namespace std;
using ll = long long;
using pii = pair <int, int>;
const int N = 1e6 + 5;
int n, _n, m, q, tin[N], tout[N], fup[N], id[N], up[N][20], sz[N], cnt[N];
bool is_cut[N];
vector <int> g[N];
stack <int> stk;
vector <vector <int> > comps;
void dfs(int v, int p = 0) {
    tin[v] = fup[v] = ++tin[0];
    stk.push(v);
    for (auto to : g[v]) {
        if (to == p)
            continue;
        if (tin[to])
            fup[v] = min(fup[v], tin[to]);
        else {
            dfs(to, v);
            fup[v] = min(fup[v], fup[to]);
            if (fup[to] >= tin[to]) {
                is_cut[v] = (tin[v] > 1 || tin[to] > 2);
                comps.pb({v});
                while (comps.back().back() != to) {
                    comps.back().pb(stk.top());
                    stk.pop();
                }
            }
        }
    }
}
void calc(int v, int p = 0) {
    tin[v] = ++tin[0];
    up[v][0] = p;
    for (int i = 1; i < 20; ++i) 
        up[v][i] = up[up[v][i - 1]][i - 1];
    sz[v] = cnt[v];
    for (auto to : g[v]) {
        if (to != p) {
            calc(to, v);
            sz[v] += sz[to];
        }
    }
    tout[v] = ++tin[0];
}
bool upper(int u, int v) {
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
    if (upper(u, v))
        return u;
    if (upper(v, u))
        return v;
    for (int i = 19; i >= 0; i--) {
        if (up[u][i] && !upper(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}
void solve() {
    cin >> n >> m >> q;
    comps.clear();
    tin[0] = _n = 0;
    for (int i = 1; i <= n; ++i) {
        g[i].clear();
        tin[i] = fup[i] = is_cut[i] = cnt[i] = 0;
    }
    while (!stk.empty()) stk.pop();
    for (int i = 1; i <= m; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1);
    for (int i = 1; i <= 2 * n; ++i) 
        g[i].clear();
    // building the block-cut tree
    for (int i = 1; i <= n; ++i) {
        if (is_cut[i]) {
            id[i] = ++_n;
            cnt[_n] = 1;
        }
    }
    if (comps.empty()) {
        ++_n;
        for (int i = 1; i <= n; ++i) {
            id[i] = 1;
            ++cnt[1];
        }
    }
    for (auto comp : comps) {
        int v = ++_n;
        for (auto x : comp) {
            if (!is_cut[x]) {
                id[x] = v;
                ++cnt[v];
            } else {
                g[id[x]].pb(v);
                g[v].pb(id[x]);
            }
        }
    }
    tin[0] = 0;
    calc(1);
    while (q--) {
        int k;
        cin >> k;
        vector <int> a;
        map <int, int> mp;
        for (int i = 0; i < k; ++i) {
            int x;
            cin >> x;
            a.pb(id[x]);
        }
        if (sz(a) == 1) {
            if (cnt[a[0]] > 1) { // it's not cut
                cout << n << '\n';
            }
            else
                cout << sz[a[0]] * 1ll * (n - sz[a[0]] + 1) << '\n';
            continue;
        }
        if (sz(a) == 2) {
            if (cnt[a[0]] != 1 || cnt[a[1]] != 1) {
                cout << "1\n";
                continue;
            }
        }
        bool ok = 1;
        for (auto x : a) {
            ok &= (cnt[x] == 1);
        }
        if (!ok) {
            cout << "0\n";
            continue;
        }
        sort(all(a), [&] (int x, int y) {
            return tin[x] < tin[y];
        });
        int l = 0;
        while (l + 1 < sz(a) && upper(a[l], a[l + 1]))
            ++l;
        
        int u = l;
        if (u == sz(a) - 1) {
            cout << sz[a[u]] * 1ll * (n - sz[a[0]] + 1) << '\n';
            continue;
        }
        ++l;
        while (l + 1 < sz(a) && upper(a[l], a[l + 1]))
            ++l;
        int v = l;
        
        if (v < sz(a) - 1) {
            cout << "0\n";
            continue;
        }
        cout << sz[a[u]] * 1ll * sz[a[v]] << '\n';
    }
}
int main() {
    ios :: sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2011ms
memory: 226528kb

input:

1000
92 95 16
76 55
19 12
57 7
39 90
38 89
48 60
29 67
35 52
32 83
10 80
64 11
66 63
90 57
17 70
20 42
31 87
91 41
33 72
12 14
9 38
30 82
72 21
9 81
40 9
73 60
71 82
48 69
70 26
72 34
25 62
10 75
83 92
16 18
34 79
15 72
65 13
64 12
37 63
46 16
32 1
23 78
22 18
78 68
49 78
48 13
39 72
39 44
27 25
20 ...

output:

144
23
92
91
602
91
92
92
90
92
90
1
92
90
270
182
18
16
-26
60
1
16
16
1
16
-26
-26
0
18
60
18
2
-26
2
18
34
18
1
1
1
-1170
16
1
18
0
18
60
2
18
-1170
34
18
48
18
1
48
1
-26
18
18
1
16
1
1
-1170
1
-26
1
1
1
18
-1170
-1170
18
16
-1170
-1170
39
-1305
39
15
15
39
-1305
48
15
1
1
39
28
-116
-1305
39
39...

result:

wrong answer 2nd lines differ - expected: '91', found: '23'