답안 #681788

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
681788 2023-01-14T09:56:24 Z peijar 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
2094 ms 548672 KB
#include <bits/extc++.h>
#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

struct chash { // large odd number for C
  const uint64_t C = (int)(4e18 * acos(0)) | 71;
  int operator()(int x) const { return __builtin_bswap64(x * C); }
};

using hash_set = __gnu_pbds::gp_hash_table<int, __gnu_pbds::null_type, chash>;
using hash_table = __gnu_pbds::gp_hash_table<int, hash_set, chash>;

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

  int nbSommet, nbAretes;
  cin >> nbSommet >> nbAretes;

  vector<hash_table> outEdges(nbSommet), inEdges(nbSommet);
  vector<int> inDeg(nbSommet);
  vector<int> idCC(nbSommet), szCC(nbSommet, 1);
  vector<vector<int>> inCC(nbSommet);
  for (int i = 0; i < nbSommet; ++i)
    inCC[i].push_back(i);
  iota(idCC.begin(), idCC.end(), 0LL);

  int nbFollows = 0;

  for (int iArete = 0; iArete < nbAretes; ++iArete) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    v = idCC[v];
    if (idCC[u] == idCC[v] or
        (inEdges[v].find(idCC[u]) != inEdges[v].end() and
         inEdges[v][idCC[u]].find(u) != inEdges[v][idCC[u]].end())) {
      cout << nbFollows << '\n';
      continue;
    }
    nbFollows += szCC[v];
    inEdges[v][idCC[u]].insert(u);
    outEdges[idCC[u]][v].insert(u);
    inDeg[v]++;
    u = idCC[u];
    queue<pair<int, int>> toMerge;

    if (outEdges[v].find(u) != outEdges[v].end())
      toMerge.emplace(u, v);

    while (!toMerge.empty()) {
      auto [A, B] = toMerge.front();
      toMerge.pop();
      A = idCC[A], B = idCC[B];
      if (A == B)
        continue;
      dbg(A, B);

      if (szCC[A] < szCC[B])
        swap(A, B);

      if (inEdges[A].find(B) != inEdges[A].end()) {
        nbFollows -= szCC[A] * inEdges[A][B].size();
        inDeg[A] -= inEdges[A][B].size();
        inEdges[A].erase(B);
        outEdges[B].erase(A);
      }
      if (outEdges[A].find(B) != outEdges[A].end()) {
        nbFollows -= szCC[B] * outEdges[A][B].size();
        inDeg[B] -= outEdges[A][B].size();
        outEdges[A].erase(B);
        inEdges[B].erase(A);
      }

      nbFollows += 2 * szCC[A] * szCC[B];
      nbFollows -= inDeg[A] * szCC[A] + inDeg[B] * szCC[B];
      for (auto &[cc, s] : inEdges[B]) {
        for (int y : s) {

          outEdges[cc][B].erase(y);

          if (inEdges[A].find(cc) == inEdges[A].end() or
              inEdges[A][cc].find(y) == inEdges[A][cc].end()) {
            inEdges[A][cc].insert(y), inDeg[A]++;
            outEdges[cc][A].insert(y);
          }
        }
        if (outEdges[A].find(cc) != outEdges[A].end())
          toMerge.emplace(A, cc);
      }

      for (auto &[cc, s] : outEdges[B]) {
        for (int y : s) {

          inEdges[cc][B].erase(y);
          inEdges[cc][A].insert(y);
          outEdges[A][cc].insert(y);
        }
        if (inEdges[A].find(cc) != inEdges[A].end())
          toMerge.emplace(A, cc);
      }

      szCC[A] += szCC[B];
      nbFollows += szCC[A] * inDeg[A];
      for (int x : inCC[B])
        inCC[A].push_back(x), idCC[x] = A;
    }
    cout << nbFollows << '\n';
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 584 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 584 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 3 ms 980 KB Output is correct
35 Correct 105 ms 14580 KB Output is correct
36 Correct 139 ms 32468 KB Output is correct
37 Correct 139 ms 33288 KB Output is correct
38 Correct 134 ms 29912 KB Output is correct
39 Correct 7 ms 5072 KB Output is correct
40 Correct 8 ms 5972 KB Output is correct
41 Correct 9 ms 5844 KB Output is correct
42 Correct 6 ms 5056 KB Output is correct
43 Correct 8 ms 5844 KB Output is correct
44 Correct 8 ms 5588 KB Output is correct
45 Correct 7 ms 4948 KB Output is correct
46 Correct 6 ms 4948 KB Output is correct
47 Correct 10 ms 6100 KB Output is correct
48 Correct 10 ms 6100 KB Output is correct
49 Correct 27 ms 12868 KB Output is correct
50 Correct 142 ms 34356 KB Output is correct
51 Correct 12 ms 6740 KB Output is correct
52 Correct 104 ms 21104 KB Output is correct
53 Correct 25 ms 11980 KB Output is correct
54 Correct 127 ms 27024 KB Output is correct
55 Correct 14 ms 8404 KB Output is correct
56 Correct 15 ms 8416 KB Output is correct
57 Correct 13 ms 7872 KB Output is correct
58 Correct 15 ms 7980 KB Output is correct
59 Correct 6 ms 5172 KB Output is correct
60 Correct 66 ms 7536 KB Output is correct
61 Correct 11 ms 6996 KB Output is correct
62 Correct 128 ms 28684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 584 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 3 ms 980 KB Output is correct
35 Correct 105 ms 14580 KB Output is correct
36 Correct 139 ms 32468 KB Output is correct
37 Correct 139 ms 33288 KB Output is correct
38 Correct 134 ms 29912 KB Output is correct
39 Correct 7 ms 5072 KB Output is correct
40 Correct 8 ms 5972 KB Output is correct
41 Correct 9 ms 5844 KB Output is correct
42 Correct 6 ms 5056 KB Output is correct
43 Correct 8 ms 5844 KB Output is correct
44 Correct 8 ms 5588 KB Output is correct
45 Correct 7 ms 4948 KB Output is correct
46 Correct 6 ms 4948 KB Output is correct
47 Correct 10 ms 6100 KB Output is correct
48 Correct 10 ms 6100 KB Output is correct
49 Correct 27 ms 12868 KB Output is correct
50 Correct 142 ms 34356 KB Output is correct
51 Correct 12 ms 6740 KB Output is correct
52 Correct 104 ms 21104 KB Output is correct
53 Correct 25 ms 11980 KB Output is correct
54 Correct 127 ms 27024 KB Output is correct
55 Correct 14 ms 8404 KB Output is correct
56 Correct 15 ms 8416 KB Output is correct
57 Correct 13 ms 7872 KB Output is correct
58 Correct 15 ms 7980 KB Output is correct
59 Correct 6 ms 5172 KB Output is correct
60 Correct 66 ms 7536 KB Output is correct
61 Correct 11 ms 6996 KB Output is correct
62 Correct 128 ms 28684 KB Output is correct
63 Correct 963 ms 408612 KB Output is correct
64 Correct 923 ms 408752 KB Output is correct
65 Correct 939 ms 408784 KB Output is correct
66 Correct 400 ms 236688 KB Output is correct
67 Correct 716 ms 283036 KB Output is correct
68 Correct 379 ms 236608 KB Output is correct
69 Correct 595 ms 271628 KB Output is correct
70 Correct 376 ms 236576 KB Output is correct
71 Correct 372 ms 236532 KB Output is correct
72 Correct 938 ms 293396 KB Output is correct
73 Correct 912 ms 294488 KB Output is correct
74 Correct 2094 ms 548672 KB Output is correct
75 Correct 737 ms 286252 KB Output is correct
76 Correct 1373 ms 410468 KB Output is correct
77 Correct 1370 ms 411980 KB Output is correct
78 Correct 407 ms 272872 KB Output is correct
79 Correct 590 ms 283448 KB Output is correct
80 Correct 406 ms 276520 KB Output is correct
81 Correct 562 ms 283488 KB Output is correct
82 Correct 1261 ms 395984 KB Output is correct
83 Correct 1260 ms 396100 KB Output is correct
84 Correct 1074 ms 384596 KB Output is correct
85 Correct 1058 ms 384540 KB Output is correct
86 Correct 494 ms 246696 KB Output is correct
87 Correct 479 ms 247672 KB Output is correct
88 Correct 872 ms 299620 KB Output is correct
89 Correct 1318 ms 395876 KB Output is correct