Submission #430900

#TimeUsernameProblemLanguageResultExecution timeMemory
430900Kevin_Zhang_TWMaking Friends on Joitter is Fun (JOI20_joitter2)C++17
17 / 100
5099 ms114628 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]; unordered_set<int> in[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]); } void clean(int a, int b) { vector<int> ina(AI(in[a])); for (int x : ina) { if (F(x) == F(b)) { in[a].erase(x); ans -= sz[ F(a) ]; } } } void add_edge(int a, int b) { if (F(a) == F(b)) return; if (in[ F(b) ].count(a)) return; ans += sz[ F(b) ]; in[F(b)].insert(a); bool mg = false; for (int x : in[ F(a) ]) if (F(x) == F(b)) { mg = true; } if (mg) { a = F(a), b = F(b); clean(a, b), clean(b, a); vector<int> nin(AI(in[a])); nin.insert(end(nin), AI(in[b])); ans -= 1ll * sz[a] * in[a].size() + 1ll * sz[b] * in[b].size(); in[a].clear(), in[b].clear(); ans += 2ll * sz[a] * sz[b]; g[a] = b; sz[b] += sz[a]; for (int i : nin) add_edge(i, a); } } 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...