#include <bits/stdc++.h>
using namespace std;
#define all(s) s.begin(), s.end()
#define ll long long
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define pi pair < int, int >
const int N = 3e5 + 7;
const int INF = 1e9 + 7;
ll sum;
ll p[N];
int n, m;
set < int > g[N], q[N];
int findp (int x){
if (p[x] < 0)
return x;
return p[x] = findp(p[x]);
}
bool check (int v, int u){
v = findp(v);
u = findp(u);
if (v != u){
for (auto to : g[u])
if (findp(to) == v)
return true;
}
return false;
}
void unite (int v, int u){
v = findp(v);
u = findp(u);
if (v != u){
sum -= 1ll * int(g[u].size()) * -p[u];
sum -= 1ll * int(g[v].size()) * -p[v];
if (g[u].size() > g[v].size())
swap(v, u);
for (auto to : g[u]){
g[v].insert(to);
q[findp(to)].erase(u);
q[findp(to)].insert(v);
}
g[v].insert(u);
g[v].erase(v);
p[v] += p[u];
p[u] = v;
sum += 1ll * int(g[v].size()) * -p[v];
// cout << v << ' ' << u << endl;
vector < int > go;
for (auto to : g[u]){
if (q[v].find(findp(to)) != q[v].end()){
go.pb(to);
}
}
for (auto to : q[u]){
if (q[to].find(v) != q[to].end()){
go.pb(to);
}
}
for (int to : go){
unite(v, to);
}
}
}
main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(p, -1, sizeof(p));
cin >> n >> m;
for (int i = 1; i <= m; i++){
int a, b;
cin >> a >> b;
if (q[findp(b)].find(findp(a)) != q[findp(b)].end())
unite(a, b);
else {
b = findp(b);
sum -= 1ll * int(g[b].size()) * -p[b];
g[b].insert(a);
g[b].erase(b);
a = findp(a);
q[a].insert(b);
sum += 1ll * int(g[b].size()) * -p[b];
}
cout << sum << endl;
}
}
Compilation message
joitter2.cpp:74:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
74 | main (){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
30796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
30796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
30796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |