QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#309497#8131. Filesystemucup-team1516#WA 1ms3788kbC++174.4kb2024-01-20 17:48:062024-01-20 17:48:06

Judging History

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

  • [2024-01-20 17:48:06]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3788kb
  • [2024-01-20 17:48:06]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

template <typename T> struct dinic {
    struct edge {
        int to;
        T c, f;
    };
    T eps;
    const T inf = numeric_limits<T>::max();
    int n, m = 0;
    vector<edge> e;
    vector<vector<int>> g;
    vector<int> level, ptr;
    dinic(int n) : n(n), g(n), level(n), ptr(n) { eps = (T)1 / (T)1e9; }
    void add_edge(int s, int t, T c) {
        e.push_back({t, c, 0});
        e.push_back({s, 0, 0});
        g[s].push_back(m++);
        g[t].push_back(m++);
    }
    bool bfs(int s, int t) {
        fill(level.begin(), level.end(), -1);
        level[s] = 0;
        for (queue<int> q({s}); q.size(); q.pop()) {
            int s = q.front();
            for (int i : g[s]) {
                int t = e[i].to;
                if (level[t] == -1 and (e[i].c - e[i].f) > eps) {
                    level[t] = level[s] + 1;
                    q.push(t);
                }
            }
        }
        return (level[t] != -1);
    }
    T dfs(int s, int t, T psh) {
        if (!(psh > eps) or s == t) return psh;
        for (int &i = ptr[s]; i < (int)g[s].size(); ++i) {
            auto &eg = e[g[s][i]];
            if (level[eg.to] != level[s] + 1 or !(eg.c - eg.f > eps)) continue;
            T f = dfs(eg.to, t, min(psh, eg.c - eg.f));
            if (f > eps) {
                eg.f += f;
                e[g[s][i] ^ 1].f -= f;
                return f;
            }
        }
        return 0;
    }
    T max_flow(int s, int t) {
        T f = 0;
        while (bfs(s, t)) {
            fill(ptr.begin(), ptr.end(), 0);
            while (1) {
                T c = dfs(s, t, inf);
                if (c > eps) {
                    f += c;
                } else {
                    break;
                }
            }
        }
        return f;
    }
    // ABC239-G
    vector<bool> min_cut(int s) {
        vector<bool> visited(n);
        queue<int> q;
        q.push(s);
        while (q.size()) {
            int p = q.front();
            q.pop();
            visited[p] = true;
            for (auto idx : g[p]) {
                auto eg = e[idx];
                if (eg.c - eg.f > eps and !visited[eg.to]) {
                    visited[eg.to] = true;
                    q.push(eg.to);
                }
            }
        }
        return visited;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while (q--) {
        int n, k; cin >> n >> k;
        vector<bool> used(k);
        for (int i = 0; i < k; ++i) {
            int x; cin >> x; x -= 1;
            used[x] = true;
        }
        vector<int> p(n);
        for (int i = 0; i < n; ++i) {
            cin >> p[i]; p[i] -= 1;
        }

        vector<pair<int,int>> v, vv;
        int res = 0;
        for (int i = 0; i+1 < n; ++i) {
            if (used[p[i]] and used[p[i+1]]) {
                v.push_back({p[i], p[i+1]});
                res += 1;
            }
            if (used[i] and used[i+1]) {
                vv.push_back({i, i+1});
                res += 1;
            }
        }
        int S = n+n+v.size()+vv.size(), T = S+1;
        dinic<int> g(T+1);
        for (int i = 0; i < n; ++i) {
            if (used[i]) {
                g.add_edge(i, i+n, 1e9);
                g.add_edge(i+n, i, 1e9);
                g.add_edge(i, T, 1);
                g.add_edge(S, i+n, 1);
            }
        }
        for (int i = 0; i < v.size(); ++i) {
            auto u = v[i];
            g.add_edge(u.first+n, 2*n+i, 1e9);
            g.add_edge(u.second+n, 2*n+i, 1e9);
            g.add_edge(2*n+i, T, 1);
        }
        for (int i = 0; i < vv.size(); ++i) {
            auto u = vv[i];
            g.add_edge(2*n+v.size()+i, u.first, 1e9);
            g.add_edge(2*n+v.size()+i, u.second, 1e9);
            g.add_edge(S, 2*n+v.size()+i, 1);
        }
        cout << g.max_flow(S,T)-res << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3672kb

input:

2
12 8
2 5 8 3 4 10 12 1
10 5 8 6 4 7 2 3 11 12 1 9
8 4
1 3 5 7
1 4 5 8 7 6 3 2

output:

3
4

result:

ok 2 number(s): "3 4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3532kb

input:

100
10 3
10 5 2
2 4 9 8 7 3 1 5 10 6
10 3
8 1 10
8 4 3 7 1 2 9 5 6 10
10 3
6 5 2
8 7 6 4 2 10 1 3 5 9
10 3
5 8 4
10 4 5 7 8 9 1 2 3 6
10 3
8 4 10
10 6 9 2 8 7 1 4 3 5
10 3
9 8 1
8 5 6 10 2 4 1 7 9 3
10 3
5 4 1
7 5 8 4 3 6 9 10 2 1
10 3
2 4 3
6 7 3 9 1 2 5 8 4 10
10 3
9 5 3
6 10 7 4 9 3 1 8 5 2
10 3
...

output:

2
3
2
2
3
2
2
1
2
2
3
2
2
2
2
2
2
2
3
2
2
2
2
2
2
2
2
3
1
3
1
2
2
2
2
2
2
2
3
2
2
3
3
3
2
2
2
3
2
2
2
2
2
2
3
2
2
2
2
2
3
2
2
1
2
1
2
2
2
1
3
2
3
1
2
2
3
2
2
3
3
2
3
2
2
2
2
3
2
2
2
2
3
1
3
3
2
2
2
2

result:

ok 100 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

100
10 5
2 6 1 9 3
10 8 2 6 9 7 5 1 3 4
10 5
7 10 1 3 6
5 7 3 9 4 1 8 2 10 6
10 5
8 3 6 2 9
4 2 9 3 8 1 6 10 7 5
10 5
5 6 7 4 3
3 7 6 2 1 5 9 8 10 4
10 5
1 6 8 10 5
8 6 3 10 1 4 7 9 5 2
10 5
6 5 1 3 9
8 10 5 3 6 1 9 2 7 4
10 5
8 6 2 7 1
3 6 5 10 9 1 2 7 4 8
10 5
6 10 8 4 2
3 9 7 5 4 1 10 6 2 8
10 5
...

output:

2
3
2
1
3
1
2
2
2
3
3
2
2
2
2
4
3
2
3
2
2
3
3
2
3
3
2
1
1
2
2
2
3
3
2
3
3
3
3
1
4
3
3
3
2
2
2
3
4
3
2
2
2
2
2
3
2
2
2
2
2
1
2
2
3
2
2
2
3
3
3
3
3
4
2
3
2
3
2
3
2
2
2
2
2
2
2
2
2
2
3
3
3
2
2
2
2
3
3
2

result:

ok 100 numbers

Test #4:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

100
10 8
3 4 9 1 8 7 2 10
9 1 7 10 5 6 2 4 8 3
10 8
9 1 8 4 2 10 3 7
4 9 8 3 7 10 2 1 5 6
10 8
4 8 3 6 9 10 7 1
10 3 5 1 6 4 8 9 2 7
10 8
2 4 5 9 7 3 10 8
10 7 2 9 3 4 8 6 1 5
10 8
3 1 8 5 9 2 10 6
10 9 2 7 1 8 3 5 4 6
10 8
10 9 3 5 6 4 7 2
1 10 2 4 7 5 8 3 9 6
10 8
7 1 8 9 2 10 5 4
2 4 10 3 6 1 8 7...

output:

2
1
3
2
3
2
2
2
2
3
2
1
1
2
2
2
2
2
2
3
3
3
2
2
2
3
2
2
2
2
3
2
2
3
2
2
2
2
2
2
2
3
2
2
1
2
1
3
2
2
2
2
2
2
3
2
3
2
3
2
2
2
1
2
1
2
2
2
1
2
2
2
2
3
1
2
3
2
3
2
3
2
2
3
3
2
3
2
2
2
2
2
2
3
2
2
2
3
2
2

result:

ok 100 numbers

Test #5:

score: 0
Accepted
time: 1ms
memory: 3556kb

input:

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

output:

2
5
4
3
4
3
4
4
4
3
5
3
3
4
4
3
3
3
4
3
2
3
3
4
4
3
4
3
5
4
4
4
3
5
4
3
2
3
3
3
3
4
3
4
4
4
4
4
4
4

result:

ok 50 numbers

Test #6:

score: 0
Accepted
time: 1ms
memory: 3568kb

input:

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

output:

5
4
5
4
4
4
6
5
5
4
5
3
5
5
4
3
5
6
4
4
5
4
4
5
4
4
5
6
5
5
4
6
4
3
5
4
4
5
4
5
4
4
4
6
3
4
5
4
4
7

result:

ok 50 numbers

Test #7:

score: 0
Accepted
time: 1ms
memory: 3680kb

input:

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

output:

4
5
4
4
5
4
5
5
4
3
5
5
5
4
5
4
5
5
4
5
4
2
4
3
3
4
5
3
5
4
4
5
5
5
3
5
5
5
5
4
5
4
5
6
4
4
5
5
4
6

result:

ok 50 numbers

Test #8:

score: -100
Wrong Answer
time: 1ms
memory: 3788kb

input:

1
1000 4
781 123 667 259
407 249 35 994 450 359 628 437 912 247 376 28 238 684 285 264 329 325 936 294 291 817 203 704 735 844 830 383 542 421 468 371 349 441 347 360 475 273 655 915 368 943 74 92 288 232 73 977 568 909 679 456 213 240 951 201 858 802 481 741 908 414 22 497 933 332 295 798 744 612 2...

output:

66

result:

wrong answer 1st numbers differ - expected: '4', found: '66'