Submission #749763

#TimeUsernameProblemLanguageResultExecution timeMemory
749763stevancvMaking Friends on Joitter is Fun (JOI20_joitter2)C++14
0 / 100
3 ms4948 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 1e5 + 2; const int inf = 1e9; int p[N], sz[N], szz[N]; vector<int> arr[N], brr[N]; void Init(int n) { for (int i = 1; i <= n; i++) { p[i] = i; sz[i] = 1; } } int Find(int x) { if (x == p[x]) return p[x]; return Find(p[x]); } ll C(ll x) { return x * (x - 1); } map<pair<int, int>, int> imaput, strelica; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; Init(n); ll ans = 0; while (m--) { int a, b; cin >> a >> b; imaput[{a, b}] = 1; if (Find(a) == Find(b)) { cout << ans << en; continue; } int x = Find(a); int y = Find(b); if (imaput[{b, a}] != 1) { if (strelica.find({y, a}) == strelica.end()) { ans += sz[y]; arr[y].push_back(a); brr[x].push_back(y); strelica[{y, a}] = 1; szz[y] += 1; } cout << ans << en; continue; } ans += C(sz[x] + sz[y]) - C(sz[x]) - C(sz[y]); if (sz[x] < sz[y]) {swap(x, y); swap(a, b);} int kol = 0; for (int u : arr[y]) { int o = Find(u); if (o == x) ans -= sz[y]; else { ans += sz[x]; arr[x].push_back(u); strelica[{y, u}] = 0; strelica[{x, u}] = 1; kol += 1; } } int cnt = szz[x]; for (int u : brr[y]) { int o = Find(u); if (o == x) { ans -= sz[x]; cnt -= 1; szz[x] -= 1; } else { brr[x].push_back(u); arr[o].push_back(y); } } ans += (ll)(cnt * sz[y]); sz[x] += sz[y]; szz[x] += kol; p[y] = x; cout << ans << en; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...