This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 2e5 + 10;
const int MOD = 998244353;
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { // read int128
int x; cin >> x; return (ll)x;
}
long long bm(long long b, long long p) {
if(p==0) return 1 % MOD;
long long r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
long long inv(long long b) {
return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) {
long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
ans *= inv(f[n-r]); ans %= MOD; return ans;
}
void precomp() {
for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD);
}
int fastlog(int x) {
if(x == 0) return -1;
return 32 - __builtin_clz(x) - 1;
}
void gay(int i) {
cout << "Case #" << i << ": ";
}
vector<int> adj[MAXN];
bool vis[MAXN];
int ans = 0;
int tin[MAXN], tout[MAXN];
int low[MAXN];
int cur = 0;
map<pair<int,int>, bool> is_bridge;
int par[MAXN];
void dfs(int node) {
tin[node] = ++cur;
vis[node] = 1;
vector<int> tree_edges;
for(int x: adj[node]) {
if(!vis[x]) {
tree_edges.push_back(x);
par[x] = node;
dfs(x);
}
}
tout[node] = ++cur;
low[node] = tin[node];
for(int x: adj[node]) {
if(tin[x] > 0 && tout[x] == 0 && par[node] != x) { /// back edge
low[node] = min(low[node], tin[x]);
}
}
for(int x: tree_edges) { /// tree edge
low[node] = min(low[node], low[x]);
//cout << "edge " << node << " -> " << x << ": " << low[x] << " vs " << tin[node] << "\n";
if(low[x] > tin[node]) {
is_bridge[{x, node}] = is_bridge[{node, x}] = 1;
//cout << "found bridge " << x << " " << node << "\n";
}
}
}
vector<int> adj2[MAXN];
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
int sz[MAXN];
int uwu;
int n;
void getsize(int x, int prv) {
uwu += sz[x];
for(int y: adj2[x]) {
if(y != prv) getsize(y, x);
}
}
int dpf(int node) {
vis[node] = 1;
int ret = sz[node];
vector<int> v;
for(int x: adj2[node]) {
if(!vis[x]) {
int q = dpf(x);
ret += q;
v.push_back(q);
}
}
v.push_back(uwu - ret);
for(int x: v) {
ans += sz[node] * x * (uwu - x - sz[node]);
}
return ret;
}
void solve(int tc) {
int m;
cin >> n >> m;
for(int i=1; i<=n; i++) {
dsu[i] = i;
}
for(int i=1; i<=m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1; i<=n; i++) {
if(!vis[i]) {
cur = 0;
dfs(i);
}
}
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(!is_bridge[{i, x}] && set_of(i) != set_of(x)) union_(i, x);
}
}
map<pair<int, int>, bool> fucked;
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(set_of(i) != set_of(x) && !fucked[{set_of(i), set_of(x)}]) {
fucked[{set_of(i), set_of(x)}] = fucked[{set_of(x), set_of(i)}] = 1;
adj2[set_of(i)].push_back(set_of(x));
//cout << set_of(i) << " " << set_of(x) << "\n";
}
}
}
for(int i=1; i<=n; i++) sz[set_of(i)]++;
for(int i=1; i<=n; i++) {
vis[i] = 0;
}
for(int i=1; i<=n; i++) {
if(set_of(i) == i && !vis[i]) {
uwu = 0;
getsize(i, -1);
dpf(i);
if(sz[i] >= 3) {
ans += sz[i] * (sz[i] - 1) * (sz[i] - 2);
}
if(sz[i] >= 2) {
ans += 2 * (sz[i] * (sz[i] - 1) * (uwu - sz[i]) - sz[i] + 1);
}
}
}
cout << ans << '\n';
}
int32_t main(){
precomp();
//ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// I don't know geometry.
# | 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... |