답안 #212010

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
212010 2020-03-22T02:06:47 Z EntityIT 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1241 ms 87932 KB
#include<bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;

mt19937 rng( (uint32_t)chrono::steady_clock::now().time_since_epoch().count() );

struct Dsu {
  vector<int> pSet, nEle;
  LL nEdge;
  vector<map<int, set<int> > > edge;
  vector<set<int> > in, out;
  queue<pair<int, int> > q;
  Dsu(int nSet) {
    pSet.assign(nSet, 0); iota(all(pSet), 0);
    nEle.assign(nSet, 1);
    nEdge = 0;
    edge.assign(nSet, {});
    in.assign(nSet, {});
    out.assign(nSet, {});
  }

  int findSet(int i) { return i ^ pSet[i] ? pSet[i] = findSet(pSet[i]) : i; }
  void unite(int i, int j) {
    i = findSet(i); j = findSet(j);

    if (i == j) return ;

    if (nEle[i] > nEle[j]) swap(i, j);

    nEdge -= (LL)sz(edge[i][j]) * nEle[j]
             + (LL)sz(edge[j][i]) * nEle[i];
    for (const auto &k : edge[i][j]) {
      out[k].erase(j);
      in[j].erase(k);
    }
    edge[i].erase(j);
    for (const auto &k : edge[j][i]) {
      out[k].erase(i);
      in[i].erase(k);
    }
    edge[j].erase(i);
    nEdge += (LL)nEle[i] * nEle[j] * 2;

    nEdge -= (LL)sz(in[i]) * nEle[i] + (LL)sz(in[j]) * nEle[j];
    for (const auto &k : in[i]) {
      in[j].emplace(k);
      if (edge[j].find(findSet(k) ) != edge[j].end() ) q.emplace(j, k);
      out[k].erase(i); edge[findSet(k)][i].erase(k);
      out[k].emplace(j); edge[findSet(k)][j].emplace(k);
    }
    for (const auto &k : edge[i]) {
      for (const auto &t : k.second) {
        edge[j][k.first].emplace(t);
        if (edge[k.first].find(j) != edge[k.first].end() ) q.emplace(k.first, j);
      }
    }
    nEdge += (LL)sz(in[j]) * (nEle[i] + nEle[j]);

    pSet[i] = j;
    nEle[j] += nEle[i];
  }
  void doQ() {
    while (sz(q) ) {
      unite(q.front().first, q.front().second);
      q.pop();
    }
  }
};

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);

  #ifdef FourLeafClover
  freopen("input", "r", stdin);
  #endif // FourLeafCLover

  int n, m; cin >> n >> m;

  Dsu dsu(n);
  while (m--) {
    int u, v; cin >> u >> v; --u; --v;
    int i = dsu.findSet(u), j = dsu.findSet(v);

    if (i ^ j && dsu.out[u].find(j) == dsu.out[u].end() ) {
      dsu.out[u].emplace(j); dsu.in[j].emplace(u);
      dsu.edge[i][j].emplace(u);
      dsu.nEdge += dsu.nEle[j];
      if (dsu.edge[j].find(i) != dsu.edge[j].end() ) {
        dsu.q.emplace(i, j);
        dsu.doQ();
      }
    }

//    for (int i = 0; i < n; ++i) {
//      cerr << i + 1 << " in " << dsu.findSet(i) + 1 << '\n';
//      if (i == dsu.findSet(i) ) {
//        for (const auto &j : dsu.in[i]) cerr << j + 1 << ' ';
//        cerr << '\n';
//        for (const auto &j : dsu.out[i]) cerr << j + 1 << ' ';
//        cerr << '\n';
//      }
//    }
//    cerr << "end\n";

    cout << dsu.nEdge << '\n';
  }

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 360 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 256 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 360 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 256 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 9 ms 512 KB Output is correct
35 Correct 98 ms 5112 KB Output is correct
36 Correct 135 ms 9080 KB Output is correct
37 Correct 137 ms 9336 KB Output is correct
38 Correct 131 ms 8696 KB Output is correct
39 Correct 7 ms 640 KB Output is correct
40 Correct 8 ms 1024 KB Output is correct
41 Correct 8 ms 1024 KB Output is correct
42 Correct 7 ms 640 KB Output is correct
43 Correct 9 ms 896 KB Output is correct
44 Correct 9 ms 896 KB Output is correct
45 Correct 7 ms 640 KB Output is correct
46 Correct 7 ms 640 KB Output is correct
47 Correct 8 ms 1024 KB Output is correct
48 Correct 11 ms 1024 KB Output is correct
49 Correct 18 ms 2560 KB Output is correct
50 Correct 138 ms 9468 KB Output is correct
51 Correct 13 ms 1408 KB Output is correct
52 Correct 107 ms 6392 KB Output is correct
53 Correct 17 ms 2304 KB Output is correct
54 Correct 120 ms 7808 KB Output is correct
55 Correct 11 ms 1664 KB Output is correct
56 Correct 11 ms 1664 KB Output is correct
57 Correct 10 ms 1664 KB Output is correct
58 Correct 11 ms 1664 KB Output is correct
59 Correct 7 ms 640 KB Output is correct
60 Correct 91 ms 3064 KB Output is correct
61 Correct 10 ms 1152 KB Output is correct
62 Correct 127 ms 8440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 360 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 256 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 9 ms 512 KB Output is correct
35 Correct 98 ms 5112 KB Output is correct
36 Correct 135 ms 9080 KB Output is correct
37 Correct 137 ms 9336 KB Output is correct
38 Correct 131 ms 8696 KB Output is correct
39 Correct 7 ms 640 KB Output is correct
40 Correct 8 ms 1024 KB Output is correct
41 Correct 8 ms 1024 KB Output is correct
42 Correct 7 ms 640 KB Output is correct
43 Correct 9 ms 896 KB Output is correct
44 Correct 9 ms 896 KB Output is correct
45 Correct 7 ms 640 KB Output is correct
46 Correct 7 ms 640 KB Output is correct
47 Correct 8 ms 1024 KB Output is correct
48 Correct 11 ms 1024 KB Output is correct
49 Correct 18 ms 2560 KB Output is correct
50 Correct 138 ms 9468 KB Output is correct
51 Correct 13 ms 1408 KB Output is correct
52 Correct 107 ms 6392 KB Output is correct
53 Correct 17 ms 2304 KB Output is correct
54 Correct 120 ms 7808 KB Output is correct
55 Correct 11 ms 1664 KB Output is correct
56 Correct 11 ms 1664 KB Output is correct
57 Correct 10 ms 1664 KB Output is correct
58 Correct 11 ms 1664 KB Output is correct
59 Correct 7 ms 640 KB Output is correct
60 Correct 91 ms 3064 KB Output is correct
61 Correct 10 ms 1152 KB Output is correct
62 Correct 127 ms 8440 KB Output is correct
63 Correct 623 ms 87696 KB Output is correct
64 Correct 612 ms 87672 KB Output is correct
65 Correct 619 ms 87932 KB Output is correct
66 Correct 169 ms 17400 KB Output is correct
67 Correct 370 ms 31096 KB Output is correct
68 Correct 163 ms 17404 KB Output is correct
69 Correct 483 ms 29176 KB Output is correct
70 Correct 166 ms 17332 KB Output is correct
71 Correct 167 ms 17400 KB Output is correct
72 Correct 431 ms 33016 KB Output is correct
73 Correct 424 ms 33016 KB Output is correct
74 Correct 1241 ms 81024 KB Output is correct
75 Correct 736 ms 38340 KB Output is correct
76 Correct 853 ms 60240 KB Output is correct
77 Correct 849 ms 60640 KB Output is correct
78 Correct 362 ms 39160 KB Output is correct
79 Correct 569 ms 41208 KB Output is correct
80 Correct 256 ms 39032 KB Output is correct
81 Correct 386 ms 40568 KB Output is correct
82 Correct 929 ms 68600 KB Output is correct
83 Correct 940 ms 68284 KB Output is correct
84 Correct 686 ms 65868 KB Output is correct
85 Correct 652 ms 65868 KB Output is correct
86 Correct 180 ms 16632 KB Output is correct
87 Correct 210 ms 17784 KB Output is correct
88 Correct 442 ms 33784 KB Output is correct
89 Correct 822 ms 56980 KB Output is correct