Submission #399598

# Submission time Handle Problem Language Result Execution time Memory
399598 2021-05-06T08:25:07 Z erray Making Friends on Joitter is Fun (JOI20_joitter2) C++11
100 / 100
2281 ms 82596 KB
// author: erray
#include <bits/stdc++.h>
 
using namespace std;
 template<typename A, typename B> string to_string(pair<A, B> p);
template<typename A, typename B, typename C> string to_string(tuple<A, B, C> t);
template<typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> t);

string to_string(string s) {
  return '"' + s + '"';
}

string to_string(char c) {
  return string("'") + c + "'";
}

string to_string(const char* c) {
  return to_string(string(c));
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

template<size_t T> string to_string(bitset<T> bs) {
  return bs.to_string();
}

string to_string(vector<bool> v) {
  string res = "{";
  for (int i = 0; i < int(v.size()); ++i) {
    if (i > 0) {
      res += ", ";
    }
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template<typename T> string to_string(T v) {
  string res = "{";
  for (auto& e : v) {
    if (int(res.size()) > 1) {
      res += ", ";
    }
    res += to_string(e);
  }
  res += "}";
  return res;
}

template<typename A, typename B> string to_string(pair<A, B> p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(tuple<A, B, C> t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + '}';
}
template<typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + '}';   
}

void debug_out() {
  cerr << endl;
}

template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << "  " << to_string(H);
  debug_out(T...);
}

#ifdef DEBUG 
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) void(37)
#endif

struct dsu {
  vector<int> par, sz;
  dsu(int n) {
    par.resize(n);
    sz.resize(n, 1);
    iota(par.begin(), par.end(), 0);
  }

  inline int get(int x) {
    return par[x] = (par[x] == x ? x : get(par[x]));
  }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (par[x] == par[y]) {
      return false;
    }
    sz[x] += sz[y];
    par[y] = x;
    return true;
  }

  int size(int x) {
    return sz[get(x)];
  }
};
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<set<int>> g(n);
  vector<set<int>> ids(n);
  vector<bool> vis(n);
  long long ans = 0;
  dsu d(n);
  dsu ud(n);
  vector<array<int, 2>> es(m);
  set<array<int, 2>> av;

  function<void(int, int)> Merge = [&](int x, int y) {
    if (!vis[y]) {
      --ans;
    }  
    if (!vis[x]) {
      --ans;
    }
    vis[x] = vis[y] = true;
    x = d.get(x);
    y = d.get(y);
    if (x == y) {
      return;
    }
    if (int(g[x].size()) < int(g[y].size())) {
        swap(x, y);
    }
    
    debug(g[x], g[y]);
    int unique = int(g[x].size());
    for (auto e : g[y]) {
      if (g[x].count(e)) {
        --unique;
        continue;
      }
      g[x].insert(e);
      ans += d.size(x);
    }
    ans += 1LL * unique * d.size(y); 

    vector<pair<int, int>> nw;
    int px = ud.get(x);
    int py = ud.get(y);
    if (int(ids[px].size()) < int(ids[py].size())) {
      swap(px, py);
    }
    for (auto e : ids[py]) {
      if (ids[px].count(e)) {
        ids[px].erase(e);
        continue;
      }  
      if (av.count(es[e])) {
        av.erase(es[e]);
        if (es[e][0] == py) {
          es[e][0] = px;
        } else {
          es[e][1] = px;
        }
        if (av.count({es[e][1], es[e][0]})) {
          nw.emplace_back(es[e][0], es[e][1]);
        } else {
          ids[px].insert(e);
          av.insert(es[e]);
        }
      }
    }

    ud.unite(px, py);
    d.unite(x, y);
    for (auto[v, u] : nw) {
      Merge(v, u);
    }
  };

  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    int py = d.get(y);
    int px = d.get(x);
    int ux = ud.get(x);
    int uy = ud.get(y);
    es[i] = {ux, uy};
    bool unite = av.find({uy, ux}) != av.end();
    if (px != py) {
      if (!g[py].count(x)) {
        ans += d.size(py);
        g[py].insert(x);     
        ids[ux].insert(i);
        ids[uy].insert(i);
        av.insert({ux, uy});
      }
      debug(ans);

      if (unite) {
        av.erase({uy, ux});
        Merge(x, y);
      } 
    }

    cout << ans << '\n';
  }
}

Compilation message

joitter2.cpp: In lambda function:
joitter2.cpp:178:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  178 |     for (auto[v, u] : nw) {
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 3 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 3 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 5 ms 588 KB Output is correct
35 Correct 126 ms 9248 KB Output is correct
36 Correct 175 ms 12912 KB Output is correct
37 Correct 176 ms 12992 KB Output is correct
38 Correct 173 ms 12516 KB Output is correct
39 Correct 5 ms 980 KB Output is correct
40 Correct 9 ms 1472 KB Output is correct
41 Correct 9 ms 1488 KB Output is correct
42 Correct 5 ms 1100 KB Output is correct
43 Correct 7 ms 1100 KB Output is correct
44 Correct 7 ms 1100 KB Output is correct
45 Correct 5 ms 1100 KB Output is correct
46 Correct 5 ms 1100 KB Output is correct
47 Correct 9 ms 1228 KB Output is correct
48 Correct 9 ms 1220 KB Output is correct
49 Correct 22 ms 2252 KB Output is correct
50 Correct 184 ms 13084 KB Output is correct
51 Correct 13 ms 1484 KB Output is correct
52 Correct 141 ms 10820 KB Output is correct
53 Correct 21 ms 2124 KB Output is correct
54 Correct 164 ms 11952 KB Output is correct
55 Correct 12 ms 1356 KB Output is correct
56 Correct 11 ms 1356 KB Output is correct
57 Correct 10 ms 1864 KB Output is correct
58 Correct 10 ms 1868 KB Output is correct
59 Correct 8 ms 1484 KB Output is correct
60 Correct 114 ms 8804 KB Output is correct
61 Correct 10 ms 1356 KB Output is correct
62 Correct 165 ms 12220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 3 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 5 ms 588 KB Output is correct
35 Correct 126 ms 9248 KB Output is correct
36 Correct 175 ms 12912 KB Output is correct
37 Correct 176 ms 12992 KB Output is correct
38 Correct 173 ms 12516 KB Output is correct
39 Correct 5 ms 980 KB Output is correct
40 Correct 9 ms 1472 KB Output is correct
41 Correct 9 ms 1488 KB Output is correct
42 Correct 5 ms 1100 KB Output is correct
43 Correct 7 ms 1100 KB Output is correct
44 Correct 7 ms 1100 KB Output is correct
45 Correct 5 ms 1100 KB Output is correct
46 Correct 5 ms 1100 KB Output is correct
47 Correct 9 ms 1228 KB Output is correct
48 Correct 9 ms 1220 KB Output is correct
49 Correct 22 ms 2252 KB Output is correct
50 Correct 184 ms 13084 KB Output is correct
51 Correct 13 ms 1484 KB Output is correct
52 Correct 141 ms 10820 KB Output is correct
53 Correct 21 ms 2124 KB Output is correct
54 Correct 164 ms 11952 KB Output is correct
55 Correct 12 ms 1356 KB Output is correct
56 Correct 11 ms 1356 KB Output is correct
57 Correct 10 ms 1864 KB Output is correct
58 Correct 10 ms 1868 KB Output is correct
59 Correct 8 ms 1484 KB Output is correct
60 Correct 114 ms 8804 KB Output is correct
61 Correct 10 ms 1356 KB Output is correct
62 Correct 165 ms 12220 KB Output is correct
63 Correct 1004 ms 75452 KB Output is correct
64 Correct 1018 ms 75416 KB Output is correct
65 Correct 1040 ms 75596 KB Output is correct
66 Correct 385 ms 40644 KB Output is correct
67 Correct 932 ms 68948 KB Output is correct
68 Correct 388 ms 40676 KB Output is correct
69 Correct 762 ms 40772 KB Output is correct
70 Correct 430 ms 40780 KB Output is correct
71 Correct 423 ms 40736 KB Output is correct
72 Correct 958 ms 52760 KB Output is correct
73 Correct 965 ms 54584 KB Output is correct
74 Correct 2281 ms 82596 KB Output is correct
75 Correct 1131 ms 49196 KB Output is correct
76 Correct 1670 ms 63172 KB Output is correct
77 Correct 1680 ms 63184 KB Output is correct
78 Correct 381 ms 39544 KB Output is correct
79 Correct 843 ms 50912 KB Output is correct
80 Correct 450 ms 38260 KB Output is correct
81 Correct 906 ms 45952 KB Output is correct
82 Correct 1437 ms 57028 KB Output is correct
83 Correct 1431 ms 57156 KB Output is correct
84 Correct 1321 ms 81740 KB Output is correct
85 Correct 1251 ms 81744 KB Output is correct
86 Correct 652 ms 75844 KB Output is correct
87 Correct 687 ms 78916 KB Output is correct
88 Correct 1041 ms 53956 KB Output is correct
89 Correct 1689 ms 61156 KB Output is correct