제출 #635651

#제출 시각아이디문제언어결과실행 시간메모리
635651ParsaEs조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2)C++17
0 / 100
3 ms4948 KiB
#include<bits/stdc++.h> #define Fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define rep(i, l, r) for(ii i = l; i < r; i++) #define pb push_back typedef long long ll; typedef int ii; using namespace std; const ii xn = 1e5 + 5; vector<ii> g[xn], g1[xn]; ll s[xn], z[xn]; ii in[xn], p[xn]; unordered_map<ii, ii> m; unordered_map<ii, bool> m1; ii gp(ii v) { return p[v] == v ? v : p[v] = gp(p[v]); } ii main() { Fast; ii n, q; cin >> n >> q; rep(i, 0, n) in[i] = p[i] = i, s[i] = 1; ll ans = 0; while(q--) { ii u, v; cin >> u >> v; u--, v--, v = gp(v); if(m1[in[u] * n + in[v]]) { cout << ans << '\n'; continue; } m1[in[u] * n + in[v]] = 1, g1[in[v]].pb(u), u = gp(u); if(u == v) { cout << ans << '\n'; continue; } if(!m[in[v] * n + in[u]]) { m[in[u] * n + in[v]]++, g[in[v]].pb(u), ans += s[v], z[v]++; cout << ans << '\n'; continue; } ans -= (s[u] + z[u] - 1) * s[u] + (s[v] + z[v] - 1) * s[v], z[u] -= m[in[v] * n + in[u]]; if(s[u] < s[v]) swap(u, v); if(g[in[u]].size() + g1[in[u]].size() < g[in[v]].size() + g1[in[v]].size()) swap(in[u], in[v]); for(ii i : g[in[v]]) m[in[i] * n + in[u]]++, g[in[u]].pb(i); g[in[v]].clear(); for(ii i : g1[in[v]]) m1[in[i] * n + in[u]] = 1, g1[in[u]].pb(i); g1[in[v]].clear(), s[u] += s[v], z[u] += z[v], ans += (s[u] + z[u] - 1) * s[u], p[v] = u; cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...