//InTheNameOfGOD
//PRS;)
#pragma GCC optimize("unroll-loops", "O2", "Ofast", "O3")
#pragma GCC target("sse", "sse2")
#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;
constexpr ii xn = 1e5 + 5;
vector<ii> g[xn], g1[xn];
ll s[xn], z[xn], ans;
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;
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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |