#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
VI par;
VI cnt;
vector<set<int>> ing;
int get(int u) {
return u == par[u] ? u : (par[u] = get(par[u]));
}
void join(int u, int v) {
u = get(u);
v = get(v);
if (u == v) return;
if (cnt[v] < cnt[u]) swap(u, v);
cnt[u] += cnt[v];
for (int x : ing[v]) ing[u].insert(x);
par[v] = u;
}
int value(int u) {
u = get(u);
return cnt[u] * ing[u].size();
}
void solve() {
int n, m;
cin >> n >> m;
par = cnt = VI(n);
rep(i, n) {
par[i] = i;
cnt[i] = 1;
}
set<pair<int, int>> edges;
ing = vector<set<int>>(n);
int ans = 0;
rep(i, m) {
int u, v;
cin >> u >> v;
u--, v--;
if (edges.count({v, u})) {
ans -= value(u);
ans -= value(v);
join(u, v);
ans += value(u);
} else {
ans += ing[get(v)].insert(u).ss;
}
edges.insert({u, v});
cout << ans << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
/*freopen("mincross.in", "r", stdin); */
/*freopen("mincross.out", "w", stdout); */
int t = 1;
/*cin >> t; */
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |