QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#74175#5437. Graph CompletingnweeksWA 58ms200004kbC++174.5kb2023-01-31 00:20:042023-01-31 00:20:07

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-31 00:20:07]
  • 评测
  • 测评结果:WA
  • 用时:58ms
  • 内存:200004kb
  • [2023-01-31 00:20:04]
  • 提交

answer

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

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
  bool first = true;
  string res = "[";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "]";
  return res;
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << to_string(H);
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const int MOD = 998244353;

int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

int sub(int a, int b) {
  a -= b;
  if (a < 0)
    a += MOD;
  return a;
}

int mult(int a, int b) { return a * b % MOD; }

const int MAX = 5000 * 5000 + 1;
int pow2[MAX];

vector<int> bridge;
vector<vector<pair<int, int>>> ed;

vector<int> num, st;
int Time;

int dfs(int at, int par) {
  int me = num[at] = ++Time, e, y, top = me;
  for (auto pa : ed[at])
    if (pa.second != par) {
      tie(y, e) = pa;
      if (num[y]) {
        top = min(top, num[y]);
        if (num[y] < me)
          st.push_back(e);
      } else {
        int si = st.size();
        int up = dfs(y, e);
        top = min(top, up);
        if (up == me) {
          st.push_back(e);
          st.resize(si);
        } else if (up < me)
          st.push_back(e);
        else
          bridge[e] = true;
      }
    }
  return top;
}

void bicomps() {
  num.assign(ed.size(), 0);
  for (int i = 0; i < (int)ed.size(); ++i)
    if (!num[i])
      dfs(i, -1);
}

struct UnionFind {
  vector<int> id;

  UnionFind(int N) : id(N, -1) {}

  int find(int u) {
    if (id[u] < 0)
      return u;
    return id[u] = find(id[u]);
  }

  bool merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v)
      return false;
    if (id[u] > id[v])
      swap(u, v);
    id[u] += id[v];
    id[v] = u;
    return true;
  }
};

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  pow2[0] = 1;
  for (int i = 1; i < MAX; ++i)
    pow2[i] = add(pow2[i - 1], pow2[i - 1]);

  int nbSommets, nbAretes;
  cin >> nbSommets >> nbAretes;
  bridge.assign(nbAretes, false);

  ed.resize(nbSommets);
  vector<pair<int, int>> aretes(nbAretes);
  for (int i = 0; i < nbAretes; ++i) {
    auto &[u, v] = aretes[i];
    cin >> u >> v;
    --u, --v;
    ed[u].emplace_back(v, i);
    ed[v].emplace_back(u, i);
  }

  bicomps();

  UnionFind dsu(nbSommets);

  for (int i = 0; i < nbAretes; ++i)
    if (!bridge[i])
      dsu.merge(aretes[i].first, aretes[i].second);
  dbg(dsu.id);

  vector<int> roots;
  for (int u = 0; u < nbSommets; ++u)
    if (dsu.id[u] < 0)
      roots.push_back(u);

  auto getId = [&](int u) {
    return lower_bound(roots.begin(), roots.end(), u) - roots.begin();
  };

  int nbCC = roots.size();
  vector<int> szCC(nbCC);
  for (int u = 0; u < nbSommets; ++u)
    szCC[getId(dsu.find(u))]++;

  vector<vector<int>> adj(nbCC);
  for (int i = 0; i < nbAretes; ++i)
    if (bridge[i]) {
      auto [u, v] = aretes[i];
      u = getId(u), v = getId(v);
      adj[u].push_back(v);
      adj[v].push_back(u);
    }

  auto merge = [&](vector<int> &dpRoot, vector<int> &dpSon) {
    vector<int> ret(dpRoot.size() + dpSon.size() - 1);
    for (int szRoot = 1; szRoot < (int)dpRoot.size(); ++szRoot)
      for (int szSon = 1; szSon < (int)dpSon.size(); ++szSon) {
        // not bad
        ret[szRoot + szSon] = add(
            ret[szRoot + szSon],
            mult(dpRoot[szRoot], mult(dpSon[szSon], pow2[szSon * szRoot - 1])));
        // bad
        ret[szRoot] = sub(ret[szRoot], mult(dpRoot[szRoot], dpSon[szSon]));
      }
    return ret;
  };

  auto Solve = [&](auto solve, int u, int p) -> vector<int> {
    vector<int> dp(szCC[u] + 1);
    dp[szCC[u]] = 1;
    for (int v : adj[u])
      if (v != p) {
        vector<int> dpSon = solve(solve, v, u);
        dp = merge(dp, dpSon);
      }
    dbg(u + 1, dp);
    return dp;
  };

  vector<int> ret = Solve(Solve, 0, 0);

  int sol = 0;
  for (int x : ret)
    sol = add(sol, x);
  dbg(sol);
  for (int u = 0; u < nbCC; ++u)
    sol = mult(sol, pow2[szCC[u] * (szCC[u] - 1) / 2]);
  for (int i = 0; i < nbAretes; ++i)
    if (!bridge[i])
      sol = mult(sol, (MOD + 1) / 2);
  cout << sol << endl;
}

详细

Test #1:

score: 100
Accepted
time: 56ms
memory: 198680kb

input:

3 2
1 2
2 3

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 58ms
memory: 198672kb

input:

4 4
1 2
2 3
3 4
4 1

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 43ms
memory: 198732kb

input:

2 1
1 2

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 53ms
memory: 198616kb

input:

3 3
1 2
2 3
3 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 36ms
memory: 198780kb

input:

4 3
1 2
2 3
3 4

output:

5

result:

ok 1 number(s): "5"

Test #6:

score: 0
Accepted
time: 51ms
memory: 198728kb

input:

4 3
1 2
1 3
1 4

output:

4

result:

ok 1 number(s): "4"

Test #7:

score: 0
Accepted
time: 40ms
memory: 198780kb

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #8:

score: 0
Accepted
time: 43ms
memory: 198800kb

input:

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

output:

1

result:

ok 1 number(s): "1"

Test #9:

score: 0
Accepted
time: 34ms
memory: 199404kb

input:

141 9870
124 111
31 87
121 106
127 90
54 125
38 17
115 23
129 111
8 116
90 85
10 29
96 110
24 125
51 113
119 33
58 64
8 5
54 97
112 44
70 138
116 85
38 138
138 21
26 18
69 128
68 31
69 42
126 110
49 118
83 124
69 4
9 110
88 104
48 53
46 30
111 120
99 85
13 85
73 85
40 124
39 38
121 40
46 100
29 61
4...

output:

1

result:

ok 1 number(s): "1"

Test #10:

score: 0
Accepted
time: 28ms
memory: 199540kb

input:

142 10000
19 3
4 86
36 122
36 88
130 86
107 59
3 119
132 90
80 124
122 95
75 66
70 123
63 119
8 44
114 9
81 19
106 77
96 93
79 141
104 50
117 66
30 48
128 109
56 73
106 116
70 8
72 130
59 110
140 20
40 11
134 71
27 51
33 93
82 96
133 118
50 14
32 64
71 12
48 33
22 32
116 17
104 45
66 71
111 142
131 ...

output:

2048

result:

ok 1 number(s): "2048"

Test #11:

score: 0
Accepted
time: 36ms
memory: 199380kb

input:

200 10000
47 42
33 120
146 144
94 170
170 181
20 101
185 190
197 33
18 37
12 86
148 115
136 120
41 182
120 11
44 132
167 67
118 139
114 52
80 37
171 56
93 139
113 112
129 122
166 4
47 60
57 6
104 119
179 104
107 1
8 70
197 70
39 127
134 1
18 26
85 100
158 121
61 105
33 113
51 54
45 85
45 130
97 164
...

output:

365281854

result:

ok 1 number(s): "365281854"

Test #12:

score: 0
Accepted
time: 44ms
memory: 199624kb

input:

500 10000
453 98
266 181
170 163
213 8
447 241
197 380
44 136
383 217
142 351
252 381
34 87
8 100
173 306
322 35
481 398
267 493
94 457
391 198
381 436
455 468
481 415
307 470
376 1
178 480
379 75
133 248
466 165
394 296
302 50
142 42
388 454
92 239
63 310
118 159
397 257
282 308
137 370
24 389
353 ...

output:

980584487

result:

ok 1 number(s): "980584487"

Test #13:

score: 0
Accepted
time: 40ms
memory: 199612kb

input:

1000 10000
818 182
136 75
353 22
34 927
455 560
720 103
752 822
493 253
627 976
34 951
329 587
292 180
189 524
345 84
420 939
97 11
141 631
232 79
600 473
351 100
567 735
237 571
582 459
39 723
709 632
784 391
448 176
643 808
336 874
696 44
819 143
5 470
690 781
875 230
872 570
681 211
270 157
106 1...

output:

588230924

result:

ok 1 number(s): "588230924"

Test #14:

score: 0
Accepted
time: 28ms
memory: 199636kb

input:

2000 10000
820 636
1257 375
1342 1314
1243 1839
1469 1206
46 675
172 1422
1121 412
1882 900
1543 709
1811 727
1217 1205
1411 674
365 738
1184 1568
1781 1999
1591 556
1755 432
28 1231
1809 1461
270 1485
1087 1636
1471 1683
148 984
452 321
393 1844
800 1491
657 951
1943 1550
1593 924
1201 1474
1148 70...

output:

950164126

result:

ok 1 number(s): "950164126"

Test #15:

score: -100
Wrong Answer
time: 48ms
memory: 200004kb

input:

5000 10000
2319 4192
4971 4546
4619 2058
1652 3642
2789 4237
2458 3238
2642 4855
2347 4170
1752 4173
2834 3683
1659 4380
4572 2645
116 4683
2667 3234
895 4589
2283 2027
53 3963
3590 726
783 3836
2019 722
3464 461
1805 2302
2404 3192
4015 3107
4256 1911
4734 3106
2902 3995
4592 2782
2099 478
3687 228...

output:

783799228

result:

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