Submission #269751

#TimeUsernameProblemLanguageResultExecution timeMemory
269751sjimedMaking Friends on Joitter is Fun (JOI20_joitter2)C++14
17 / 100
5048 ms44704 KiB
#include<bits/stdc++.h> using namespace std; #define fast ios::sync_with_stdio(false); cin.tie(0); #define fi first #define se second #define all(v) (v).begin(), (v).end() #define em emplace #define eb emplace_back #define mp make_pair typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; const ll INF = 1e18; const int inf = 1e9; const ll Mod = 1e9 + 7; int n, m; int a, b; int p[100010]; ll sz[100010]; set<int> in[100010], out[100010]; ll ans; vector<pii> q; int Find(int a) { return p[a] = a == p[a] ? a : Find(p[a]); } void Union(int a, int b) { a = Find(a); b = Find(b); if(a == b) return; if(sz[a] < sz[b]) swap(a, b); ans -= sz[a] * (sz[a]-1) + sz[b] * (sz[b] - 1); ans -= sz[a] * in[a].size(); ans -= sz[b] * in[b].size(); p[b] = a; sz[a] += sz[b]; sz[b] = 0; ans += sz[a] * (sz[a] - 1); for(auto i : out[b]) { if(Find(i) == a || Find(i) == b) continue; out[a].insert(Find(i)); if(out[Find(i)].find(a) != out[Find(i)].end()) { q.eb(a, Find(i)); } } out[b].clear(); for(auto i : in[b]) { if(Find(i) == a) continue; in[a].insert(i); out[Find(i)].insert(a); if(out[a].find(Find(i)) != out[a].end()) { q.eb(a, Find(i)); } } in[b].clear(); vector<int> v; for(auto i : in[a]) { if(Find(i) == a) v.eb(i); } for(auto i : v) in[a].erase(i); ans += sz[a] * in[a].size(); } int main() { fast; cin >> n >> m; for(int i=1; i<=n; i++) p[i] = i, sz[i] = 1; for(int i=1; i<=m; i++) { cin >> a >> b; if(Find(a) == Find(b) || in[Find(b)].find(a) != in[Find(b)].end()) { cout << ans << "\n"; continue; } in[Find(b)].insert(a); out[Find(a)].insert(Find(b)); ans += sz[Find(b)]; if(out[Find(b)].find(Find(a)) != out[Find(b)].end()) { q.eb(a, b); } while(q.size()) { auto x = q.back(); q.pop_back(); Union(x.fi, x.se); } cout << ans << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...