이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
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 = 2e5 + 10;
const int inf = 1e9;
int n, m, a[maxn], cnt[maxn], sz[maxn], st[maxn], lw[maxn], tme, cmcnt, vercnt;
vector<int> g[maxn], t[maxn], comp[maxn], stk, going;
bool vis[maxn], mark[maxn];
ll ans = 0;
void dfs(int v){
lw[v] = st[v] = ++tme;
vis[v] = true;
going.push_back(v);
stk.push_back(v);
for (auto u: g[v]){
if (!vis[u]){
dfs(u);
lw[v] = min(lw[v], lw[u]);
if (lw[u] >= st[v]){
if (st[v] > 1 || st[u] > 2) mark[v] = true;
cmcnt++;
comp[cmcnt].push_back(v);
while(comp[cmcnt].back() != u){
comp[cmcnt].push_back(stk.back());
stk.pop_back();
}
}
}
else lw[v] = min(lw[v], st[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){
if (mark[v]){
ans += 1ll * (sz[r] - 1) * (sz[r] - 2);
for (auto u: t[v]){
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);
ans += 1ll * (sz[r] - cnt[v]) * (sz[r] - cnt[v] - 1) * (cnt[v] - 2 + (int)t[v].size());
ans += 1ll * cnt[v] * (cnt[v] - 1) * ((int)t[v].size());
for (auto u: t[v]){
if (u != p){
ans += 2ll * sz[u] * cnt[v] * ((int)t[v].size() - 1);
ans += 2ll * sz[u] * cnt[v] * (cnt[v] - 1);
ans -= 1ll * sz[u] * (sz[u] - 1) * (cnt[v] - 2 + (int)t[v].size());
}
else{
ans += 2ll * (sz[r] - sz[v]) * cnt[v] * ((int)t[v].size() - 1);
ans += 2ll * (sz[r] - sz[v]) * cnt[v] * (cnt[v] - 1);
ans -= 1ll * (sz[r] - sz[v]) * (sz[r] - sz[v] - 1) * (cnt[v] - 2 + (int)t[v].size());
}
}
}
for (auto u: t[v]) if (u != p) Calc(u, r, v);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
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]){
stk.clear();
going.clear();
tme = 0;
dfs(i);
if (going.size() == 1){
cmcnt++;
comp[cmcnt].push_back(i);
}
}
}
for (int i = 1; i <= cmcnt; i++){
cnt[n+i] = comp[i].size();
for (auto x: comp[i]){
if (mark[x]){
cnt[n+i]--;
t[x].push_back(n+i);
t[n+i].push_back(x);
}
}
}
memset(vis, false, sizeof vis);
for (int i = 1; i <= n; i++) cnt[i] = 1;
for (int i = 1; i <= cmcnt; i++){
if (!vis[n+i]){
solve(n+i);
Calc(n+i, n+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... |