// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 300005;
int n, m;
ll ans;
struct thing {
int v, u;
bool operator< (const thing &o) const {
return v < o.v;
}
};
set<thing> adj[MAXN];
set<ii> iadj[MAXN];
set<int> adjp[MAXN];
int p[MAXN], sz[MAXN];
void init() {
REP (i, 1, n + 1) {
p[i] = i;
sz[i] = 1;
}
}
int findp(int i) {
if (p[i] == i) {
return i;
}
return p[i] = findp(p[i]);
}
bool join(int a, int b) {
int pa = findp(a), pb = findp(b);
if (pa == pb) {
return 1;
}
if (sz[pa] < sz[pb]) {
swap(pa, pb);
}
ans -= (ll) SZ(adj[pa]) * sz[pa];
ans -= (ll) SZ(adj[pb]) * sz[pb];
adjp[pa].erase(pb);
adjp[pb].erase(pa);
for (auto [v, u] : adj[pb]) {
if (findp(v) == pa) {
iadj[pa].erase({u, v});
} else {
adj[pa].insert({v, u});
adjp[pa].insert(findp(v));
}
}
for (auto [v, u] : iadj[pb]) {
if (findp(v) == pa) {
adj[pa].erase({u, v});
} else {
iadj[pa].insert({v, u});
adjp[findp(v)].erase(pb);
adjp[findp(v)].insert(pa);
}
}
/*
for (auto [x, y] : eg[pa][pb]) {
adj[pa].erase(y);
}
for (auto [x, y] : eg[pb][pa]) {
adj[pb].erase(y);
}
*/
ans -= (ll) sz[pa] * (sz[pa] - 1);
ans -= (ll) sz[pb] * (sz[pb] - 1);
sz[pa] += sz[pb];
ans += (ll) sz[pa] * (sz[pa] - 1);
for (auto [v, u] : adj[pa]) {
assert(findp(v) != pa);
}
ans += (ll) SZ(adj[pa]) * sz[pa];
p[pb] = pa;
return 1;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> m;
init();
REP (i, 0, m) {
int a, b; cin >> a >> b;
if (findp(a) != findp(b)) {
bool gd = adj[findp(b)].insert({a, b}).SE;
if (gd) {
ans += sz[findp(b)];
}
iadj[findp(a)].insert({b, a});
adjp[findp(b)].insert(findp(a));
if (adjp[findp(a)].count(findp(b))) {
cerr << "JOIN " << a << ' ' << b << '\n';
join(a, b);
}
}
/*
while (1) {
bool fnd = 0;
RREP (j, i, 0) {
RREP (k, j - 1, 0) {
if (findp(a[j]) == findp(b[k]) && findp(a[k]) == findp(b[j]) && findp(a[j]) != findp(b[j])) {
fnd = 1;
join(a[j], b[j]);
}
}
}
if (!fnd) {
break;
}
}
ll ans = 0;
set<ii> st;
RREP (j, i, 0) {
if (findp(a[j]) == findp(b[j])) {
continue;
}
int pa = a[j], pb = findp(b[j]);
if (st.find({pa, pb}) != st.end()) {
continue;
}
st.insert({pa, pb});
ans += sz[pb];
}
REP (j, 1, n + 1) {
if (findp(j) != j) {
continue;
}
ans += (ll) sz[j] * (sz[j] - 1);
}
cout << ans << '\n';
*/
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
42484 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Incorrect |
19 ms |
42580 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
42484 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Incorrect |
19 ms |
42580 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
42484 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Incorrect |
19 ms |
42580 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |