QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#473251#8131. Filesystemucup-team1198#WA 0ms3656kbC++203.5kb2024-07-11 23:37:152024-07-11 23:37:16

Judging History

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

  • [2024-07-11 23:37:16]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3656kb
  • [2024-07-11 23:37:15]
  • 提交

answer

#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

const int INF = 1e9;

struct Edge {
  int from, to;
  int flow;
  int cap;
  Edge(int from, int to, int flow, int cap): from(from), to(to), flow(flow), cap(cap) {}
};

struct DinicFlow {
  vector<Edge> edges;
  vector<vector<int>> G;
  vector<int> last_edge;
  vector<int> dist;
  int N;
  int s, t;

  DinicFlow(int N, int s, int t): edges(), G(N), last_edge(N, 0), dist(N, 0), N(N), s(s), t(t) {}

  void add_undir_edge(int from, int to, int cap) {
    G[from].emplace_back(edges.size());
    edges.emplace_back(from, to, 0, cap);
    G[to].emplace_back(edges.size());
    edges.emplace_back(to, from, 0, cap);
  }

  void add_dir_edge(int from, int to, int cap) {
    G[from].emplace_back(edges.size());
    edges.emplace_back(from, to, 0, cap);
    G[to].emplace_back(edges.size());
    edges.emplace_back(to, from, 0, 0);
  }

  int dfs(int v, int mf) {
    if (v == t)
      return mf;
    int res = 0;
    for (; last_edge[v] < (int) G[v].size(); ++last_edge[v]) {
      int i = G[v][last_edge[v]];
      if (dist[edges[i].to] != dist[v] + 1)
        continue;
      if (edges[i].cap <= edges[i].flow)
        continue;
      int cur = dfs(edges[i].to, min(mf - res, edges[i].cap - edges[i].flow));
      if (cur == 0)
        continue;
      res += cur;
      edges[i].flow += cur;
      edges[i ^ 1].flow -= cur;
      if (edges[i].cap > edges[i].flow || res == mf)
        return res;
    }
    return res;
  }

  int flow() {
    while (true) {
      dist.assign(N, N);
      deque<int> Q;
      dist[s] = 0;
      Q.emplace_back(s);
      while (!Q.empty()) {
        int v = Q.front();
        Q.pop_front();
        for (int i : G[v]) {
          if (edges[i].cap > edges[i].flow && dist[edges[i].to] > dist[v] + 1) {
            dist[edges[i].to] = dist[v] + 1;
            Q.emplace_back(edges[i].to);
          }
        }
      }
      if (dist[t] == N)
        break;
      fill(last_edge.begin(), last_edge.end(), 0);
      while (dfs(s, INF));
    }
    int res = 0;
    for (int i : G[s])
      res += edges[i].flow;
    return res;
  }
};

void solve() {
  int n, k;
  cin >> n >> k;
  vector<bool> is_good(n, false);
  for (int i = 0; i < k; ++i) {
    int x;
    cin >> x;
    is_good[x - 1] = true;
  }
  vector<int> p(n);
  vector<int> q(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    --p[i];
    q[p[i]] = i;
  }
  int kek = 0;
  DinicFlow dinic(n + 2, n, n + 1);
  for (int i = 0; i + 1 < n; ++i) {
    if (is_good[i] && is_good[i + 1]) {
      ++kek;
      dinic.add_dir_edge(n, i, 1);
      dinic.add_dir_edge(n, i + 1, 1);
      dinic.add_undir_edge(i, i + 1, 1);
    }
  }
  for (int j = 0; j + 1 < n; ++j) {
    int u = q[j];
    int v = q[j + 1];
    if (is_good[u] && is_good[v]) {
      ++kek;
      dinic.add_dir_edge(u, n + 1, 1);
      dinic.add_dir_edge(v, n + 1, 1);
      dinic.add_undir_edge(u, v, 1);
    }
  }
  int tmp = dinic.flow();
  cout << tmp / 2 + k - kek << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3656kb

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: -100
Wrong Answer
time: 0ms
memory: 3572kb

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
2
1
2
1
3
3
2
2
2
3
2
2
2
2
3
3
3
2
1
2
3
2
2
3
1
3
1
2
2
2
2
2
2
2
3
2
3
3
3
3
2
2
2
3
2
2
2
2
2
3
3
2
2
2
2
2
2
2
2
1
2
1
2
3
2
2
2
2
3
1
2
2
3
3
2
1
2
2
3
2
3
2
2
2
2
2
2
2
1
2
2
1
2
2
2
2

result:

wrong answer 5th numbers differ - expected: '3', found: '2'