제출 #211973

#제출 시각아이디문제언어결과실행 시간메모리
211973achvanov조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2)C++17
0 / 100
10 ms12032 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, m; long long ans; int p[N]; vector<int> cl[N]; set<int> in[N], in2[N]; long long tot(int v) { return 1ll * cl[v].size() * (cl[v].size() - 1); } int get(int v) { return (v == p[v] ? v : p[v] = get(p[v])); } long long kek(int v) { return 1ll * cl[v].size() * in[v].size(); } void unite(int u, int v) { u = get(u), v = get(v); if (u == v) { return; } if (cl[u].size() + in[u].size() < cl[v].size() + in[v].size()) { swap(u, v); } ans -= tot(u); ans -= tot(v); ans -= kek(v); ans -= kek(u); for (int x : cl[v]) { cl[u].push_back(x); if (in[u].count(x)) { in[u].erase(x); } } for (int x : in[v]) { if (get(x) != u) { in[u].insert(x); } } for (int x : in2[v]) { if (x != u) { in2[u].insert(x); } } for (int x : in[u]) { assert(get(x) != u); } ans += kek(u); ans += tot(u); p[v] = u; } void connect(int u, int v) { if (get(u) == get(v)) { return; } if (in[get(v)].count(u)) { return; } if (in2[get(u)].count(get(v))) { unite(u, v); } else { ans += cl[get(v)].size(); in[get(v)].insert(u); in2[get(v)].insert(get(u)); } } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) { cl[i].push_back(i); p[i] = i; } while (m--) { int u, v; cin >> u >> v; --u, --v; connect(u, v); cout << ans << '\n'; //cout.flush(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...