이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define N 100005
using namespace std;
vector<int> adj[N];
vector<int> adj2[N];
vector<int> adj3[N];
int d[N];
int low[N];
vector<pair<int,int>> edges;
void dfs(int v,int par){
d[v] = d[par] + 1;
low[v] = d[v];
for(auto u:adj[v]){
if(u == par)
continue;
if(d[u] != 0){
low[v] = min(d[u],low[v]);
if(d[v] < d[u]){
adj2[v].push_back(u);
adj2[u].push_back(v);
}
}
else{
dfs(u,v);
low[v] = min(low[v],low[u]);
if(low[u] <= d[v]){
adj2[v].push_back(u);
adj2[u].push_back(v);
}
else{
edges.push_back({u,v});
}
}
}
}
int cnt[N];
int group[N];
int sub[N];
int n;
long long ans = 0;
void dfs2(int v){
cnt[group[v]]++;
for(auto u:adj2[v]){
if(group[u])continue;
group[u] = group[v];
dfs2(u);
}
}
void dfs3(int v,int par){
sub[v] = cnt[group[v]];
for(auto u:adj3[group[v]]){
if(group[u] == group[par])
continue;
dfs3(u,v);
sub[v] += sub[u];
}
// cout << v << ' ' << sub[v] << ' ' << cnt[group[v]] << endl;
ans += 2 * (long long)cnt[group[v]] * (cnt[group[v]] - 1) * (n - cnt[group[v]]);
ans -= 2 * (long long)(cnt[group[v]] - 1) * (n - cnt[group[v]]);
for(auto u:adj3[group[v]]){
if(u == par){
ans += (long long)cnt[group[v]] * (n - sub[v]) * (sub[v] - cnt[group[v]]);
}
else{
ans += (long long)cnt[group[v]] * (n - sub[u] - cnt[group[v]]) * (sub[u]);
}
}
}
void solve(){
int m;
cin >> n >> m;
for(int i = 1;i<=m;i++){
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,0);
for(int i = 1;i<=n;i++){
if(group[i] == 0){
group[i] = i;
dfs2(i);
ans += (long long)cnt[i] * (cnt[i]-1) * (cnt[i]-2);
}
// cout << i << ' ' << group[i] << endl;
}
for(auto u:edges){
adj3[group[u.first]].push_back(u.second);
adj3[group[u.second]].push_back(u.first);
}
dfs3(1,0);
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
# | 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... |