Submission #958356

#TimeUsernameProblemLanguageResultExecution timeMemory
958356MisterReaperMaking Friends on Joitter is Fun (JOI20_joitter2)C++17
100 / 100
901 ms70736 KiB
#include <bits/stdc++.h> using i64 = long long; constexpr int maxN = 1E5 + 5; std::set<int> in[maxN], rev[maxN], graph[maxN]; std::queue<std::pair<int, int>> strong; i64 ans = 0; int par[maxN], sz[maxN]; int find(int a) { if(par[a] == a) { return a; } return par[a] = find(par[a]); } void connect(int a, int b) { graph[a].emplace(b); rev[b].emplace(a); if(graph[b].count(a)) { strong.emplace(a, b); } } void unite(int a, int b) { a = find(a), b = find(b); if(a == b) { return; } if(sz[a] < sz[b]) { std::swap(a, b); } ans += 1LL * in[b].size() * sz[a] + 1LL * in[a].size() * sz[b]; sz[a] += sz[b]; par[b] = a; for(int v : in[b]) { if(!in[a].count(v)) { in[a].emplace(v); } else { ans -= sz[a]; } } graph[a].erase(b); graph[b].erase(a); rev[a].erase(b); rev[b].erase(a); for(int v : graph[b]) { rev[v].erase(b); connect(a, v); } for(int v : rev[b]) { graph[v].erase(b); connect(v, a); } } void solve() { int n, m; std::cin >> n >> m; for(int _ = 1; _ <= m; _++) { int a, b; std::cin >> a >> b; if(find(a) != find(b) && !in[find(b)].count(a)) { in[find(b)].emplace(a); ans += sz[find(b)]; connect(find(a), find(b)); while(!strong.empty()) { auto [u, v] = strong.front(); strong.pop(); unite(u, v); } } std::cout << ans << "\n"; } return; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::iota(par, par + maxN, 0); std::fill(sz, sz + maxN, 1); for(int i = 0; i < maxN; i++) { in[i].emplace(i); } int t = 1; //std::cin >> t; for(int i = 1; i <= t; i++) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...