Submission #226167

#TimeUsernameProblemLanguageResultExecution timeMemory
226167MinnakhmetovMaking Friends on Joitter is Fun (JOI20_joitter2)C++14
0 / 100
5 ms512 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(aaa) aaa.begin(), aaa.end() const int N = 2005; vector<int> outgoing_edges[N], ingoing_edges[N]; int gr[N][N], 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); } w[x] += w[y]; w[y] = x; for (int i = 0; i < n; i++) { if (gr[y][i]) gr[x][i] = true; if (gr[i][y]) gr[i][x] = 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) { x = find_set(x); y = find_set(y); 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); vector<pair<int, int>> v; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--, y--; add_edge(x, y); v.push_back({x, y}); for (auto &p : v) { p.first = find_set(p.first); p.second = find_set(p.second); } ll ans = 0; for (auto &p : v) { if (p.first != p.second) ans -= w[p.second]; } for (int j = 0; j < n; j++) { if (w[j] < 0) ans += w[j] * (w[j] + 1); } cout << ans << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...