This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
const int MAXN = 100013;
int N, M;
int dsu[MAXN];
vi nodes[MAXN];
ll ans;
set<int> in[MAXN], out[MAXN], cc[MAXN];
//set of nodes going into a clique
//set of cliques going out of a node
//set of cliques going out of a clique
vpi todo;
vpi merges;
int get(int u)
{
return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void proc()
{
while(!todo.empty())
{
int s = todo.back().fi, t = todo.back().se;
todo.pop_back();
s = get(s); t = get(t);
if (s == t) continue;
cc[s].erase(t);
cc[t].erase(s);
// cerr << "merge " << s << ' ' << t << endl;
ans -= (ll) SZ(in[s]) * SZ(nodes[s]);
ans -= (ll) SZ(in[t]) * SZ(nodes[t]);
if (SZ(nodes[s]) > SZ(nodes[t])) swap(s, t);
//so in[s] and in[t] merge.
for (int c : cc[s])
{
if (cc[c].count(t))
{
todo.PB({c, t});
}
cc[t].insert(c);
}
cc[s].clear();
for (int u : in[s])
{
if (cc[t].count(get(u)))
{
todo.PB({get(u), t});
}
cc[get(u)].erase(s);
cc[get(u)].insert(t);
out[u].erase(s);
out[u].insert(t);
in[t].insert(u);
}
in[s].clear();
nodes[t].insert(nodes[t].end(), ALL(nodes[s]));
dsu[s] = t;
ans += (ll) SZ(in[t]) * SZ(nodes[t]);
}
return;
}
void update(int u, int v)
{
v = get(v);
if (in[v].count(u)) return;
//if v -> u doesn't exist.
if (!cc[v].count(get(u)))
{
// cerr << "no " << v << ' ' << get(u) << endl;
in[v].insert(u);
// cerr << "in " << v << " insert " << u << endl;
out[u].insert(v);
ans += SZ(nodes[v]);
cc[get(u)].insert(v);
return;
}
todo.PB({get(u), v});
proc();
}
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
FOR(i, 0, N)
{
dsu[i] = i;
nodes[i].PB(i);
in[i].insert(i);
cc[i].insert(i);
}
//cc stores the edges in a forward direction.
FOR(i, 0, M)
{
int u, v;
cin >> u >> v; u--; v--;
update(u, v);
// ans = -N;
// FOR(i, 0, N)
// {
// ans += SZ(in[i]) * SZ(nodes[i]);
// }
cout << ans << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |