#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 12, MOD = (int)1e9 + 7;
int n,m,s[N],p[N];
map<int,int> g[N],gr[N];
int get(int v) {
if(p[v] == v) return v;
return p[v] = get(p[v]);
}
ll cl(int x) {
return x * 1ll * (x + 1);
}
ll res = 0;
void merge(int a,int b) {
res -= g[a][b] * 1ll * s[b];
res -= g[b][a] * 1ll * s[a];
res -= cl(s[a]);res -= cl(s[b]);
res += cl(s[a] + s[b]);
vector<pair<int,int>> mrg;
p[a] = b;
for(auto [x,y]:gr[b]) {
res += y * 1ll * s[a];
}
for(auto [x,y]:gr[a]) {
if(!y) continue;
if(x == b) continue;
res += y * 1ll * s[b];
g[x].erase(a);
g[x][b] += y;
gr[b][x] += y;
if(g[b].count(x)) {
mrg.push_back({x,b});
}
}
for(auto [x,y]:g[a]) {
if(x == b || !y) continue;
gr[x].erase(a);
gr[x][b] += y;
g[b][x] += y;
if(gr[b].count(x)) {
mrg.push_back({x,b});
}
}
s[b] += s[a];
g[a].clear();
gr[a].clear();
for(auto [x,y]:mrg) {
x = get(x);
y = get(y);
if(x == y) continue;
merge(x,y);
}
}
void test() {
cin >> n >> m;
for(int i = 1;i <= n;i++) {
p[i] = i;
s[i] = 1;
}
for(int i = 1;i <= m;i++) {
int a,b;
cin >> a >> b;
a = get(a);
b = get(b);
if(a == b || g[a].count(b)) {
cout << res << '\n';
continue;
}
if(!g[b].count(a)) {
g[a][b]++;
gr[b][a]++;
res += s[b];
cout << res << '\n';
} else {
merge(a,b);
cout << res << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |