#include<stdio.h>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
set<long long >edge[121212], redge[121212], ar[121212], a[121212], aar[121212];
vector<long long >L[121212];
long long par[121212], sz[121212], rsz[121212], ck[121212];
long long find(long long x) {
if (par[x] == x)return x;
return par[x] = find(par[x]);
}
long long ans = 0;
void un(int s, int e) {
long long ps = find(s), pe = find(e);
if (ps == pe)return;
if (sz[ps] < sz[pe]) { swap(s, e), swap(ps, pe); }
ans -= rsz[ps] * (rsz[ps] - 1);
ans -= rsz[pe] * (rsz[pe] - 1);
for (auto x : L[pe]) {
int ff = 1;
for (long long nxt : edge[x]) {
long long pnxt = find(nxt);
if (pnxt == ps) {
if(ff)ans -= rsz[ps];
ff = 0;
}
else {
aar[pnxt].erase(pe);
aar[pnxt].insert(ps);
}
}
for (long long nxt : redge[x]) {
long long pnxt = find(nxt);
if (pnxt == ps) {
if (ck[nxt] == 0) {
ans -= rsz[pe];
ck[nxt] = 1;
}
}
else {
a[pnxt].erase(pe);
a[pnxt].insert(ps);
}
}
L[ps].push_back(pe);
if (ar[ps].find(x) != ar[ps].end()) ar[ps].erase(x);
}
for (auto x : L[pe]) {
for (long long nxt : redge[x]) {
ck[nxt] = 0;
}
}
ans += rsz[pe] * ar[ps].size();
set<int>LL;
for (auto x : a[pe]) if (aar[ps].find(x) != aar[ps].end())LL.insert(x);
for (auto x : aar[pe]) if (a[ps].find(x) != a[ps].end())LL.insert(x);
for (auto x : ar[pe]) {
if (ar[ps].find(x) != ar[ps].end())ans -= rsz[pe];
else if (find(x) != ps) {
ans += rsz[ps];
ar[ps].insert(x);
}
}
for (auto x : a[pe])a[ps].insert(x);
for (auto x : aar[pe])aar[ps].insert(x);
par[pe] = ps;
rsz[ps] += rsz[pe];
sz[ps] += sz[pe];
ans += rsz[ps] * (rsz[ps] - 1);
for (auto x : LL) {
un(x, ps);
}
}
int main() {
long long n, m; scanf("%lld%lld", &n, &m);
long long i, j;
for (i = 1; i <= n; i++)par[i] = i, sz[i] = rsz[i] = 1, L[i].push_back(i);
for (i = 0; i < m; i++) {
long long s, e; scanf("%lld%lld", &s, &e);
long long ps = find(s), pe = find(e);
if (ps == pe) { printf("%lld\n", ans); continue; }
if (a[pe].find(ps) != a[pe].end()) {
un(s, e);
}
else {
if (ar[pe].find(s) == ar[pe].end()) {
edge[s].insert(e); redge[e].insert(s);
ar[pe].insert(s);
aar[pe].insert(ps);
a[ps].insert(pe);
sz[ps]++; sz[pe]++;
ans += rsz[pe];
}
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:78:15: warning: unused variable 'j' [-Wunused-variable]
78 | long long i, j;
| ^
joitter2.cpp:77:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | long long n, m; scanf("%lld%lld", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
joitter2.cpp:81:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | long long s, e; scanf("%lld%lld", &s, &e);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
31564 KB |
Output is correct |
2 |
Correct |
20 ms |
31564 KB |
Output is correct |
3 |
Correct |
20 ms |
31536 KB |
Output is correct |
4 |
Correct |
19 ms |
31616 KB |
Output is correct |
5 |
Correct |
20 ms |
31588 KB |
Output is correct |
6 |
Correct |
20 ms |
31620 KB |
Output is correct |
7 |
Correct |
20 ms |
31592 KB |
Output is correct |
8 |
Correct |
20 ms |
31564 KB |
Output is correct |
9 |
Correct |
21 ms |
31692 KB |
Output is correct |
10 |
Correct |
21 ms |
31600 KB |
Output is correct |
11 |
Incorrect |
20 ms |
31600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
31564 KB |
Output is correct |
2 |
Correct |
20 ms |
31564 KB |
Output is correct |
3 |
Correct |
20 ms |
31536 KB |
Output is correct |
4 |
Correct |
19 ms |
31616 KB |
Output is correct |
5 |
Correct |
20 ms |
31588 KB |
Output is correct |
6 |
Correct |
20 ms |
31620 KB |
Output is correct |
7 |
Correct |
20 ms |
31592 KB |
Output is correct |
8 |
Correct |
20 ms |
31564 KB |
Output is correct |
9 |
Correct |
21 ms |
31692 KB |
Output is correct |
10 |
Correct |
21 ms |
31600 KB |
Output is correct |
11 |
Incorrect |
20 ms |
31600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
31564 KB |
Output is correct |
2 |
Correct |
20 ms |
31564 KB |
Output is correct |
3 |
Correct |
20 ms |
31536 KB |
Output is correct |
4 |
Correct |
19 ms |
31616 KB |
Output is correct |
5 |
Correct |
20 ms |
31588 KB |
Output is correct |
6 |
Correct |
20 ms |
31620 KB |
Output is correct |
7 |
Correct |
20 ms |
31592 KB |
Output is correct |
8 |
Correct |
20 ms |
31564 KB |
Output is correct |
9 |
Correct |
21 ms |
31692 KB |
Output is correct |
10 |
Correct |
21 ms |
31600 KB |
Output is correct |
11 |
Incorrect |
20 ms |
31600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |