#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// My bug list :
// integer overflow
// 0based, 1based forgotten
// index out of bound
// n, m, i, j typo
// some cases are missing
const int MAX_N = 100010;
ll ans;
int n, m, g[MAX_N], sz[MAX_N];
unordered_set<int> out[MAX_N], in[MAX_N];
void init() {
iota(g, g+n+1, 0);
fill(sz, sz + n + 1, 1);
}
int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }
void clean(int a, int b) {
vector<int> ina(AI(in[a]));
for (int x : ina) {
if (F(x) == F(b)) {
in[a].erase(x);
ans -= sz[ F(a) ];
out[x].erase( F(a) );
}
}
}
void add_edge(int a, int b) {
static set<pair<int,int>> st;
if (F(a) == F(b)) return;
if (in[a].count(F(b))) {
DE("Merge ", a, b);
a = F(a), b = F(b);
clean(a, b), clean(b, a);
DE(ans);
ans += 1ll * in[a].size() * sz[b] + 1ll * in[b].size() * sz[a] + 2ll * sz[a] * sz[b];
for (int x : in[a]) {
out[x].erase(a), out[x].insert(b);
}
in[b].insert(AI(in[a]));
in[a].clear();
sz[b] += sz[a];
g[a] = b;
}
else {
st.insert({a, b});
if (out[a].count(F(b))) return;
out[a].insert(F(b));
in[F(b)].insert(a);
ans += sz[ F(b) ];
}
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
init();
while (m--) {
int a, b;
cin >> a >> b;
DE("edge ", a, b);
add_edge(a, b);
for (int i = 1;i <= n;++i) DE(i, F(i));
cout << ans << '\n';
DE(ans);
}
}
Compilation message
joitter2.cpp: In function 'void add_edge(int, int)':
joitter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
joitter2.cpp:53:3: note: in expansion of macro 'DE'
53 | DE("Merge ", a, b);
| ^~
joitter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
joitter2.cpp:56:3: note: in expansion of macro 'DE'
56 | DE(ans);
| ^~
joitter2.cpp: In function 'int32_t main()':
joitter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
joitter2.cpp:85:3: note: in expansion of macro 'DE'
85 | DE("edge ", a, b);
| ^~
joitter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
joitter2.cpp:87:30: note: in expansion of macro 'DE'
87 | for (int i = 1;i <= n;++i) DE(i, F(i));
| ^~
joitter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
joitter2.cpp:90:3: note: in expansion of macro 'DE'
90 | DE(ans);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
11252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
11252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
11252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |