제출 #1168510

#제출 시각아이디문제언어결과실행 시간메모리
11685101binMaking Friends on Joitter is Fun (JOI20_joitter2)C++20
0 / 100
9 ms14412 KiB
#include <bits/stdc++.h> using namespace std; #define all(v) v.begin(), v.end() typedef long long ll; const int NMAX = 1e5 + 5; ll n, m, par[NMAX], sz[NMAX], ans, a, b, pa, pb; set<int> v[NMAX], in[NMAX], out[NMAX]; int find(int x){return par[x] == -1 ? x : par[x] = find(par[x]);} queue<pair<int, int>> q; void add(int a, int f) { ll c = v[a].size() - sz[a]; if (f == -1) ans -= sz[a] * (sz[a] - 1) + sz[a] * c; else ans += sz[a] * (sz[a] - 1) + sz[a] * c; } void merge(int a, int b) { add(a, -1); add(b, -1); if (v[a].size() + out[a].size() + in[a].size() < v[b].size() + out[b].size() + in[b].size()) swap(a, b); par[b] = a; sz[a] += sz[b]; in[a].erase(b); out[a].erase(b); in[b].erase(a); out[b].erase(a); for (auto& t : v[b]) v[a].emplace(t); for (auto&t : out[b]) { in[t].erase(b); q.emplace(a, t); } for (auto&t : in[b]) { out[t].erase(b); q.emplace(t, a); } add(a, 1); } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { v[i].emplace(i); sz[i] = 1; par[i] = -1; } while (m--) { cin >> a >> b; q.emplace(a, b); while (q.size()) { auto[a, b] = q.front(); q.pop(); pa = find(a); pb = find(b); if (pa == pb) continue; add(pb, -1); v[pb].emplace(a); out[pa].emplace(pb); in[pb].emplace(pa); add(pb, 1); if (out[pb].find(pa) != out[pb].end()) merge(pa, pb); } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...