This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Wei Wai Wei Wai
Zumigat nan damu dimi kwayt rayt
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
/*typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod FM(2);
*/
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10;
const int inf = 1e9;
int n, m, par[maxn], a[maxn], cnt[maxn], sz[maxn], h[maxn];
vector<int> g[maxn], t[maxn];
bool vis[maxn], mark[maxn];
ll ans = 0;
int dfs(int v){
vis[v] = true;
int res = h[v], tmp = 0, cnt = (h[v] != 0);
for (auto u: g[v]){
if (!vis[u]){
h[u] = h[v] + 1;
int x = dfs(u);
res = min(res, x);
tmp = max(tmp, x);
cnt++;
}
else res = min(res, h[u]);
}
if (cnt > 1 && tmp >= h[v]) mark[v] = true;
return res;
}
int getpar(int v){
return (par[v] == -1? v: par[v] = getpar(par[v]));
}
void merge(int u, int v){
if ((u = getpar(u)) == (v = getpar(v))) return;
par[v] = u;
}
void solve(int v, int p = -1){
vis[v] = true;
sz[v] = cnt[v];
for (auto u: t[v]){
if (u != p){
solve(u, v);
sz[v] += sz[u];
}
}
}
void Calc(int v, int r, int p = -1){
ans += 1ll * (sz[r] - cnt[v]) * (sz[r] - cnt[v] - 1);
if (mark[v]){
for (auto u: g[v]){
ans += 1ll * cnt[u] * (cnt[u] - 1);
if (u != p) ans -= 1ll * sz[u] * (sz[u] - 1);
else ans -= 1ll * (sz[r] - sz[v]) * (sz[r] - sz[v] - 1);
}
}
else{
ans += 1ll * cnt[v] * (cnt[v]-1) * (cnt[v]-2);
for (auto u: g[v]){
if (u != p){
ans += 2ll * sz[u] * cnt[v] * (cnt[v]-1);
ans -= 1ll * sz[u] * (sz[u] - 1);
}
else{
ans += 2ll * (sz[r] - sz[v]) * cnt[v] * (cnt[v]-1);
ans -= 1ll * (sz[r] - sz[v]) * (sz[r] - sz[v] - 1);
}
}
}
for (auto u: t[v]) if (u != p) Calc(u, r, v);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
memset(par, -1, sizeof par);
cin >> n >> m;
for (int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i);
for (int i = 1; i <= n; i++){
for (auto u: g[i]){
if (u > i && !mark[u] && !mark[i]) merge(i, u);
}
}
for (int i = 1; i <= n; i++){
a[i] = getpar(i);
cnt[a[i]]++;
}
for (int i = 1; i <= n; i++){
for (auto u: g[i]){
if (u > i && (mark[u] || mark[i]) && getpar(u) != getpar(i)){
t[a[u]].push_back(a[i]);
t[a[i]].push_back(a[u]);
merge(u, i);
}
}
}
memset(vis, false, sizeof vis);
for (int i = 1; i <= n; i++){
if (!vis[a[i]]){
solve(a[i]);
Calc(a[i], a[i]);
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |