QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#761515#5224. 电阻网络hos_lyric#10 53ms3884kbC++147.7kb2024-11-19 00:05:252024-11-19 00:05:25

Judging History

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

  • [2024-11-19 00:05:25]
  • 评测
  • 测评结果:10
  • 用时:53ms
  • 内存:3884kb
  • [2024-11-19 00:05:25]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// gg: bipartite graph between {vertex} and {biconnected component}
//   (|gg| - n) biconnected components
//   isolated point: not regarded as biconnected component (==> isolated in gg)
// f: DFS out-forest
// ess: edges in biconnected component
//   (u, v) with dis[u] <= dis[v]
//   self-loop at isolated point: not included in ess
struct Biconnected {
  int n, m;
  vector<vector<int>> g, f, gg;
  vector<vector<pair<int, int>>> ess;
  vector<int> par, rs;
  int zeit;
  vector<int> dis, fin, low;

  Biconnected() {}
  explicit Biconnected(int n_) : n(n_), m(0), g(n_) {}
  void ae(int u, int v) {
    ++m;
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    g[u].push_back(v);
    if (u != v) g[v].push_back(u);
  }

  int stackVLen, stackELen;
  vector<int> stackV;
  vector<pair<int, int>> stackE;
  vector<int> cntPar;
  void dfs(int u) {
    stackV[stackVLen++] = u;
    dis[u] = low[u] = zeit++;
    for (const int v : g[u]) {
      if (par[u] == v && !cntPar[u]++) continue;
      if (~dis[v]) {
        if (dis[u] >= dis[v]) stackE[stackELen++] = std::make_pair(v, u);
        if (low[u] > dis[v]) low[u] = dis[v];
      } else {
        f[u].push_back(v);
        par[v] = u;
        rs[v] = rs[u];
        const int stackEPos = stackELen;
        stackE[stackELen++] = std::make_pair(u, v);
        dfs(v);
        if (low[u] > low[v]) low[u] = low[v];
        if (dis[u] <= low[v]) {
          const int x = gg.size();
          gg.emplace_back();
          ess.emplace_back();
          for (; ; ) {
            const int w = stackV[--stackVLen];
            gg[w].push_back(x);
            gg[x].push_back(w);
            if (w == v) break;
          }
          gg[u].push_back(x);
          gg[x].push_back(u);
          for (; stackELen > stackEPos; ) ess[x].push_back(stackE[--stackELen]);
        }
      }
    }
    fin[u] = zeit;
  }
  void build() {
    f.assign(n, {});
    gg.assign(n, {});
    ess.assign(n, {});
    par.assign(n, -1);
    rs.assign(n, -1);
    zeit = 0;
    dis.assign(n, -1);
    fin.assign(n, -1);
    low.assign(n, -1);
    stackV.resize(n);
    stackE.resize(m);
    cntPar.assign(n, 0);
    for (int u = 0; u < n; ++u) if (!~dis[u]) {
      stackVLen = stackELen = 0;
      rs[u] = u;
      dfs(u);
    }
  }

  // Returns true iff u is an articulation point
  //   <=> # of connected components increases when u is removed.
  inline bool isArt(int u) const {
    return (gg[u].size() >= 2);
  }

  // Returns w s.t. w is a child of u and a descendant of v in the DFS forest.
  // Returns -1 instead if v is not a proper descendant of u
  //   O(log(deg(u))) time
  int dive(int u, int v) const {
    if (dis[u] < dis[v] && dis[v] < fin[u]) {
      int j0 = 0, j1 = f[u].size();
      for (; j0 + 1 < j1; ) {
        const int j = (j0 + j1) / 2;
        ((dis[f[u][j]] <= dis[v]) ? j0 : j1) = j;
      }
      return f[u][j0];
    } else {
      return -1;
    }
  }
  // Returns true iff there exists a v-w path when u is removed.
  //   O(log(deg(u))) time
  bool isStillReachable(int u, int v, int w) const {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    assert(0 <= w); assert(w < n);
    assert(u != v);
    assert(u != w);
    if (rs[v] != rs[w]) return false;
    if (rs[u] != rs[v]) return true;
    const int vv = dive(u, v);
    const int ww = dive(u, w);
    if (~vv) {
      if (~ww) {
        return (vv == ww || (dis[u] > low[vv] && dis[u] > low[ww]));
      } else {
        return (dis[u] > low[vv]);
      }
    } else {
      if (~ww) {
        return (dis[u] > low[ww]);
      } else {
        return true;
      }
    }
  }
};

////////////////////////////////////////////////////////////////////////////////


int N, M;
vector<int> A, B;

Biconnected C;

namespace brute {
vector<int> on;
bool dfs(int t, int u, int p) {
  bool ret = (t == u);
  for (const int v : C.gg[u]) if (p != v) ret = ret || dfs(t, v, u);
  if (ret && u >= N) on[u] = 1;
  return ret;
}
bool check(int s, int t) {
  if (C.rs[s] != C.rs[t]) return false;
  on.assign(C.gg.size(), 0);
  const bool res = dfs(t, s, -1);
  assert(res);
  for (int u = 0; u < N; ++u) for (const int x : C.gg[u]) if (on[x]) on[u] = 1;
// cerr<<"[check] "<<s<<" "<<t<<"; on = "<<on<<endl;
  vector<multiset<int>> graph(N);
  for (int i = 0; i < M; ++i) if (on[A[i]] && on[B[i]]) {
    graph[A[i]].insert(B[i]);
    graph[B[i]].insert(A[i]);
  }
  /*
    (u, -1): deg(u) = 2
    (u, v): multi-edge
  */
  auto deg2 = [&](int u) -> bool {
    return (on[u] && s != u && t != u && graph[u].size() == 2);
  };
  queue<pair<int, int>> que;
  for (int u = 0; u < N; ++u) if (deg2(u)) que.emplace(u, -1);
  for (; que.size(); ) {
    const int u = que.front().first;
    int v = que.front().second;
    que.pop();
// cerr<<"  "<<u<<" "<<v<<endl;
    if (~v) {
      graph[u].erase(graph[u].find(v));
      graph[v].erase(graph[v].find(u));
      for (const int w : {u, v}) if (deg2(w)) que.emplace(w, -1);
    } else {
      if (deg2(u)) {
        const vector<int> vs(graph[u].begin(), graph[u].end());
        assert(vs.size() == 2);
        if (vs[0] == vs[1]) {
          v = vs[0];
          graph[u].erase(graph[u].find(v));
          graph[v].erase(graph[v].find(u));
          for (const int w : {u, v}) if (deg2(w)) que.emplace(w, -1);
        } else {
          on[u] = 0;
          for (const int w : vs) graph[w].erase(graph[w].find(u));
          if (graph[vs[0]].find(vs[1]) != graph[vs[0]].end()) que.emplace(vs[0], vs[1]);
          graph[vs[0]].insert(vs[1]);
          graph[vs[1]].insert(vs[0]);
          for (const int w : vs) if (deg2(w)) que.emplace(w, -1);
        }
      }
    }
  }
  for (int u = 0; u < N; ++u) if (on[u] && s != u && t != u) return false;
  return true;
}
Int run() {
  Int ans = 0;
  for (int s = 0; s < N; ++s) for (int t = s + 1; t < N; ++t) if (check(s, t)) ++ans;
  return ans;
}
}  // brute

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    A.resize(M);
    B.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    C = Biconnected(N);
    for (int i = 0; i < M; ++i) C.ae(A[i], B[i]);
    C.build();
cerr<<"|gg| - N = "<<((int)C.gg.size()-N)<<endl;
// cerr<<", gg = "<<C.gg<<endl;
    
    const Int ans = brute::run();
    printf("%lld\n", ans);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 1ms
memory: 3852kb

input:

8 10
4 3
7 8
2 7
1 4
3 2
3 6
8 1
7 5
2 5
6 2

output:

19

result:

ok 1 number(s): "19"

Test #2:

score: 0
Wrong Answer
time: 53ms
memory: 3872kb

input:

79 100
37 30
10 62
22 74
45 18
34 17
63 19
35 31
71 76
46 23
56 37
38 27
31 62
56 28
50 6
53 33
65 73
74 27
13 35
79 25
61 28
36 51
53 32
45 38
33 32
69 21
61 46
78 60
62 13
37 56
42 24
68 3
38 11
39 16
60 8
38 77
29 12
68 69
25 76
17 6
38 24
57 40
70 57
2 45
67 47
47 43
29 73
34 52
20 55
71 34
68 7...

output:

1

result:

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

Test #3:

score: 0
Wrong Answer
time: 13ms
memory: 3884kb

input:

80 100
71 67
58 62
4 16
26 45
63 29
35 72
47 58
50 34
23 61
69 21
48 8
62 47
51 21
17 37
14 46
63 66
70 78
60 5
49 6
5 51
36 24
44 65
18 42
14 20
75 37
5 61
52 80
60 58
51 3
36 37
37 56
77 38
76 32
40 57
77 11
63 73
37 56
11 6
29 63
36 56
1 27
74 32
2 42
11 6
79 32
10 72
33 11
66 54
53 34
68 56
51 3...

output:

151

result:

wrong answer 1st numbers differ - expected: '614', found: '151'

Test #4:

score: 0
Time Limit Exceeded

input:

750 1000
95 448
299 649
50 53
560 399
93 258
476 245
601 713
258 49
242 440
592 161
339 509
615 103
73 708
450 529
82 438
543 599
282 457
549 418
693 729
230 541
441 489
479 557
732 727
403 186
717 188
665 739
400 64
167 660
458 53
673 724
214 533
378 737
584 382
329 7
683 553
389 582
534 537
127 48...

output:


result:


Test #5:

score: 0
Time Limit Exceeded

input:

73413 100000
38431 13450
2131 44812
30882 25050
25301 63322
32179 35347
2809 3474
21177 50989
6762 5035
71525 26365
17116 1450
32582 18040
62554 31914
41973 62590
69625 37122
31492 41066
56111 46589
9678 12588
42940 14054
31665 20181
23334 2308
37642 48954
58832 19593
69878 18784
22599 65362
1824 35...

output:


result:


Test #6:

score: 0
Time Limit Exceeded

input:

74692 100000
71866 14285
48822 59980
8238 31181
24220 28008
42366 12152
19828 51536
73872 57967
55809 41276
33319 27844
15324 38209
55096 29922
58706 25913
38125 6505
4146 23011
57823 16703
58341 56506
3045 28672
24160 8853
62472 23124
11624 45066
67702 14222
21219 72026
19228 8374
53032 57782
41911...

output:


result:


Test #7:

score: 0
Time Limit Exceeded

input:

77760 100000
56956 36835
58488 49951
8895 46686
33826 6828
47837 55219
52643 75522
46911 75788
11678 43170
5523 65371
51825 54637
56676 69989
36608 34835
57082 3934
26683 24742
63620 62302
59022 52132
4352 11402
4377 2959
44754 72445
33040 28210
37355 32236
56300 16313
19902 17799
34580 13090
75355 ...

output:


result:


Test #8:

score: 0
Time Limit Exceeded

input:

75180 100000
43136 26274
15951 9769
8124 61474
74101 7566
20367 28583
38014 61708
38114 36560
27822 72068
16891 48855
69684 41427
24562 8871
1120 23875
16601 33450
15501 71750
67045 16308
59061 2541
72469 50298
55114 24554
42305 55769
63878 8412
36165 63883
41258 27149
5985 31021
63762 36893
49649 1...

output:


result:


Test #9:

score: 0
Time Limit Exceeded

input:

74603 100000
45477 40361
73878 65917
64326 23394
46093 29153
9509 6853
62304 39445
30235 33071
40858 30634
74086 39530
14426 15319
61600 74089
39221 28182
24897 16213
2322 54603
7239 40232
20569 23800
7010 63789
2880 73471
10851 15573
1827 16119
31805 28120
47831 49343
29435 64413
10174 62870
14012 ...

output:


result:


Test #10:

score: 0
Time Limit Exceeded

input:

76028 100000
25011 42925
2419 18852
63219 32864
37819 3195
47024 57475
8588 59185
22933 22367
45574 57833
73928 37505
53391 12919
1162 11636
43684 51594
5013 21503
687 12417
47900 51598
59607 11652
46310 31199
66511 74936
69911 32832
18529 69872
58330 33379
38367 46833
10324 61814
11736 41642
48893 ...

output:


result: