Submission #431088

#TimeUsernameProblemLanguageResultExecution timeMemory
431088Kevin_Zhang_TWMaking Friends on Joitter is Fun (JOI20_joitter2)C++17
0 / 100
6 ms9676 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define pb emplace_back #define AI(i) begin(i), end(i) template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); } template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); } #ifdef KEV #define DE(args...) kout("[ " + string(#args) + " ] = ", args) void kout() { cerr << endl; } template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); } template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; } #else #define DE(...) 0 #define debug(...) 0 #endif // My bug list : // integer overflow // 0based, 1based forgotten // index out of bound // n, m, i, j typo // some cases are missing const int MAX_N = 100010; ll ans; int n, m, g[MAX_N], sz[MAX_N], cost[MAX_N]; //unordered_set<int> in[MAX_N], out[MAX_N]; set< pair<int,int> > in[MAX_N];//, out[MAX_N]; set<int> out[MAX_N]; void init() { iota(g, g+n+1, 0); fill(sz, sz + n + 1, 1); } int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); } pair<int,int> tok(int i) { return make_pair(F(i), i); } void kill_edge(int a, int b) { in[F(b)].erase(tok(a)); out[F(a)].erase(F(b)); } void resetg(set<pair<int,int>> &st, int a, int b) { auto it = st.lower_bound(make_pair(a, -1)); vector<pair<int,int>> pt; while (it != end(st) && it->first == a) { pt.pb(*it); it = st.erase(it); } for (auto [x, y] : pt) st.insert({b, y}); } void kick(int a, int b) { auto it = in[a].lower_bound(make_pair(b, -1)); vector<pair<int,int>> pt; while (it != end(in[a]) && it->first == b) pt.pb(*it++); for (auto [_, y] : pt) { kill_edge(y, a); ans -= sz[a]; } } bool hg(set<pair<int,int>> &st, int v) { return st.lower_bound({v, -1}) != st.lower_bound({v+1, -1}); } void add_edge(int a, int b) { if (F(a) == F(b)) return; if (in[ F(b) ].count(tok(a))) return; ans += sz[ F(b) ]; in[F(b)].insert(tok(a)); out[F(a)].insert(F(b)); ++cost[F(a)], ++cost[F(b)]; if (out[F(b)].count(F(a))) { a = F(a), b = F(b); kick(a, b), kick(b, a); if (cost[a] > cost[b]) swap(cost[a], cost[b]); vector<int> nmg; for (int x : out[a]) { if (hg(in[b], x)) { nmg.pb(x); } else { resetg(in[x], a, b); out[b].insert(x); } } ans += in[b].size() * 1ll * sz[a]; for (auto [g, id] : in[a]) { if (out[b].count(g)) { nmg.pb(g); } else { out[g].erase(a), out[g].insert(b); in[b].insert({g, id}); ans += sz[b]; } } // for (int x : nmg) { kick(a, x), kick(x, a), kick(x, b), kick(b, x); } ans += 2ll * sz[a] * sz[b]; g[a] = b; cost[b] += cost[a]; sz[b] += sz[a]; for (int x : nmg) add_edge(x, a), add_edge(a, x); } } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> m; init(); while (m--) { int a, b; cin >> a >> b; add_edge(a, b); cout << ans << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...