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>
#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
using namespace std;
typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;
const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 1e5 + 11;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 333;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;
unordered_set < int > in[N], out[N], gc[N], g[N], gr[N];
unordered_map < int, int > cnt1[N];
int p[N], ans, n, m, v, u, cnt[N];
queue < pii > q;
vi comp[N];
int get (int v) {
return p[v] == v ? v : p[v] = get(p[v]);
}
void merge (int v, int u) {
v = get(v), u = get(u);
if (v == u)
return;
ans -= cnt1[v][u] * cnt[v];
ans -= cnt1[u][v] * cnt[u];
ans += 2 * cnt[v] * cnt[u];
if (cnt[v] + sz(g[v]) < cnt[u] + sz(g[u]))
swap(v, u);
for (auto x : comp[u])
if (g[v].count(x)) {
g[v].erase(x);
gr[x].erase(v);
}
ans += sz(g[v]) * cnt[u];
for (auto x : g[u]) {
if (get(x) == v)
continue;
if (g[v].count(x))
ans -= cnt[u];//common vertex
else {
g[v].insert(x);
gr[x].insert(v);
gr[x].erase(u);
++cnt1[v][get(x)];
ans += cnt[v];
}
x = get(x);
gc[v].insert(x);
if (gc[x].count(v))
q.push({v, x});
}
for (auto x : comp[u])
for (auto y : gr[x]) {
if (y == v)
continue;
++cnt1[y][v];
gc[y].insert(v);
if (gc[v].count(y))
q.push({v, y});
}
p[u] = v;
cnt[v] += cnt[u];
for (auto x : comp[u])
comp[v].pb(x);
}
main () {
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n; ++i) {
p[i] = i;
cnt[i] = 1;
comp[i].pb(i);
}
while (m--) {
scanf("%lld%lld", &u, &v);
int V = get(v), U = get(u);
if (V != U) {
if (gc[U].count(V))
q.push({V, U});
else {
gc[V].insert(U);
if (!g[V].count(u)) {
++cnt1[V][U];
ans += cnt[V];
g[V].insert(u);
gr[u].insert(V);
}
}
while (sz(q)) {
merge(q.front().f, q.front().s);
q.pop();
}
}
printf("%lld\n", ans);
}
}
Compilation message (stderr)
joitter2.cpp:111:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
joitter2.cpp: In function 'int main()':
joitter2.cpp:112:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~
joitter2.cpp:120:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |