Submission #226179

#TimeUsernameProblemLanguageResultExecution timeMemory
226179MinnakhmetovMaking Friends on Joitter is Fun (JOI20_joitter2)C++14
0 / 100
5 ms384 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(aaa) aaa.begin(), aaa.end() const int N = 2005; bool gr[N][N], ing[N][N], mem[N][N]; int w[N]; ll ans = 0; int n, m; int find_set(int x) { return w[x] < 0 ? x : w[x] = find_set(w[x]); } void mrg(int x, int y) { x = find_set(x); y = find_set(y); if (x == y) return; if (w[x] > w[y]) { swap(x, y); } ans -= w[x] * (w[x] + 1); ans -= w[y] * (w[y] + 1); // cout << w[x] * (w[x] + 1) + w[y] * (w[y] + 1) << "\n"; for (int i = 0; i < n; i++) { if (ing[x][i]) { ans += w[x]; } if (ing[y][i]) { ans += w[y]; } if (mem[y][i]) { ing[x][i] = false; mem[x][i] = true; } if (ing[y][i] && !mem[x][i]) { ing[x][i] = true; } } w[x] += w[y]; w[y] = x; ans += w[x] * (w[x] + 1); for (int i = 0; i < n; i++) { if (ing[x][i]) { ans -= w[x]; } } for (int i = 0; i < n; i++) { if (gr[i][y]) gr[i][x] = true; if (gr[y][i]) gr[x][i] = true; } for (int i = 0; i < n; i++) { if (gr[x][i] && gr[i][x]) mrg(x, i); } } void add_edge(int x, int y) { y = find_set(y); if (!ing[y][x]) { ans -= w[y]; ing[y][x] = true; } x = find_set(x); if (x == y || gr[x][y]) return; gr[x][y] = true; if (gr[y][x]) mrg(x, y); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; fill(w, w + n, -1); for (int i = 0; i < n; i++) { mem[i][i] = true; } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--, y--; add_edge(x, y); cout << ans << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...