QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#702603#7898. I Just Want... One More...ucup-team3646RE 0ms3644kbC++202.6kb2024-11-02 16:16:152024-11-02 16:16:15

Judging History

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

  • [2024-11-02 16:16:15]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3644kb
  • [2024-11-02 16:16:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define rep2(i,l,r) for(ll i=ll(l); i<ll(r); i++)

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

mt19937 engine(42);

vector<pair<int, int>> Bipartite_Mathing(int n, int m, vector<pair<int, int>> &edges) {
  shuffle(edges.begin(), edges.end(), engine);
  vi G(edges.size()), L(n, -1), R(m, -1), deg(n + 1), a, p, q(n);
  for (auto &[x, y] : edges) deg[x]++;
  rep(i, n) deg[i + 1] += deg[i];
  for (auto &[x, y] : edges) G[--deg[x]] = y;
  while (true) {
    a.assign(n, -1), p.assign(n, -1);
    int t = 0;
    rep(i, n) {
      if (L[i] == -1) {
        q[t++] = a[i] = p[i] = i;
      }
    }
    bool match = false;
    rep(i, t) {
      int x = q[i];
      if (L[a[x]] != -1) continue;
      rep2(j, deg[x], deg[x + 1]) {
        int y = G[j];
        if (R[y] == -1) {
          while (y != -1) {
            R[y] = x;
            swap(L[x], y);
            x = p[x];
          }
          match = true;
          break;
        }
        if (p[R[y]] == -1) {
          q[t++] = y = R[y];
          p[y] = x;
          a[y] = a[x];
        }
      }
    }
    if (!match) break;
  }
  vector<pair<int, int>> res;
  rep(i, L.size()) {
    if (L[i] != -1) res.push_back({i, L[i]});
  }
  return res;
}

void solve() {
  ll N, M; cin >> N >> M;
  vector< pair<int, int> > edge(M);
  for (ll i = 0; i < M; ++i) {
    ll u, v; cin >> u >> v;
    u--, v--;
    edge[i] = {u, v};
  }

  vector<pair<int, int>> match = Bipartite_Mathing(N, M, edge);
  set< pair<int, int> > st;
  for (auto [u, v] : match) st.insert({u, v});
  
  vector<pair<int, int>> non;
  for (auto [u, v] : edge) {
    if (st.find({u, v}) == st.end()) {
      non.push_back({u, v});
    }
  }


  vector<ll> Sok(N, 0), Sng(N, 0), Tok(N, 0), Tng(N, 0);
  for (auto [u, v] : non) {
    Tng[v] = 1;
    Sng[u] = 1;
  }
  vector<int> isS(N, 0), isT(N, 0);
  for (auto [u, v] : match) {
    if (Tng[v]) Sok[u] = 1;
    if (Sng[u]) Tok[v] = 1;

    isS[u] = isT[v] = 1;
  }

  // for (ll u = 0; u < N; ++u) {
  //   cout << u << " " << Sok[u] << " " << isS[u] << endl;
  // }

  {
    ll s = 0, t = 0;
    for (ll i = 0; i < N; ++i) {
      if (isS[i] == 0 || Sok[i] == 1) {
        s++;
      }
    }
    for (ll i = 0; i < N; ++i) {
      if (isT[i] == 0 || Tok[i] == 1) {
        t++;
      }
    }
    ll res = s * t;
    cout << res << endl;
  }

  return;
}

int main() {
  ll T; cin >> T;
  while (T--) {
    solve();
  }

  return 0;
}

詳細信息

Test #1:

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

input:

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

output:

6
0
4

result:

ok 3 number(s): "6 0 4"

Test #2:

score: -100
Runtime Error

input:

10000
5 4
2 2
3 1
5 3
1 1
1 1
1 1
1 1
1 1
2 2
2 2
1 2
1 1
1 1
1 1
1 1
1 1
1 1
2 3
2 1
1 2
1 1
5 5
1 4
2 2
3 1
1 3
1 2
2 4
2 2
2 1
1 2
1 1
5 1
5 3
3 1
2 2
1 1
1 1
3 2
3 1
2 1
5 2
1 2
2 3
5 3
1 5
4 2
1 2
4 1
1 1
2 3
1 1
2 2
2 1
4 1
1 4
3 1
1 1
1 1
1 1
2 1
2 2
3 3
1 3
2 3
2 2
3 3
1 3
3 3
1 2
3 3
2 2
1 ...

output:

6
0
0
2
0
0
0
1
9
4
25

result: