#include<bits/stdc++.h>
#define int long long
using namespace std;
multiset<pair<int, bool>> ad[100005];
unordered_set<int> sus[100005], node[100005];
int ans = 0, root[100005], sz[100005];
int getroot(int u)
{
if(root[u] == u) return u;
else return getroot(root[u]);
}
void unite(int u, int v)
{
u = getroot(u); v = getroot(v);
if(u == v) return;
root[v] = u; sz[u] += sz[v];
}
set<int> mergeComponent(int u, int v)
{
u = getroot(u); v = getroot(v);
if(u == v) return {};
if(sz[u] < sz[v]) swap(u, v);
//Erase all edges between two components
int num = ad[v].count({u, 0}), num2 = ad[u].count({v, 0});
set<int> add;
ans += -num*sz[u] - num2*sz[v]+ 2*sz[u]*sz[v];
ad[v].erase({u, 0}); ad[v].erase({u, 1});
ad[u].erase({v, 0}); ad[u].erase({v, 1});
//cout<<"A"<<ans<<endl;
//Merge ad[] vector
for(pair<int, bool> i : ad[v]){ //Merge to ad[u]
if(ad[i.first].count(make_pair(u, i.second)) > 0) add.insert(i.first);/**/
ad[u].insert(i);
pair<int, bool> j = make_pair(v, i.second^1);
ad[i.first].erase(j);
j.first = u;
ad[i.first].insert(j);
}
//Merge sus[]
//cout<<"A"<<ans<<" "<<sus[u].size()<<" "<<sus[v].size()<<endl;
for(int i : node[v]){
node[u].insert(i);
if(sus[u].count(i) == 1) sus[u].erase(i);
}
ans += -sus[u].size()*sz[u] - (sus[v].size()-num2)*sz[v];
/*cout<<"C"<<ans<<" "<<sus[u].size()<<" "<<sus[v].size()<<" "<<u<<" "<<v<<endl;
if(u == 3 && v == 4){
cout<<"D"<<sus[4].size()<<endl;
for(int i : sus[4]) cout<<i<<" ";
cout<<endl;
}*/
for(int i : sus[v]){
if(node[u].count(i) == 0) sus[u].insert(i);
}
ans += sus[u].size()*(sz[u]+sz[v]);
unite(u, v);
/*cout<<"B"<<sus[u].size()<<endl;
for(int i : sus[u]) cout<<i<<" ";
cout<<endl;*/
return add;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin>>n>>m;
for(int i = 1; i <= n; i++){
root[i] = i; sz[i] = 1; node[i].insert(i);
}
for(int test = 1; test <= m; test++){
int u, v;
cin>>u>>v;
int reu = u;
u = getroot(u); v = getroot(v);
if(u == v || sus[v].count(reu) == 1){cout<<ans<<'\n'; continue;}
int num = ad[v].count({u, 0});
if(num > 0){
//Merge two components
vector<int> order; order.push_back(v);
for(int i = 0; i < order.size(); i++){
set<int> x = mergeComponent(u, order[i]);
for(int j : x) order.push_back(j);
}
}
else{
ans += sz[v];
ad[u].insert({v, 0});
ad[v].insert({u, 1});
sus[v].insert(reu);
}
cout<<ans<<'\n';
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:81:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i = 0; i < order.size(); i++){
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
15964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
15964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
15964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |