이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int DIM = 1E5+7;
const int INF = 1E9;
vector<int> G[DIM],NG[DIM];
int depth[DIM],sz[DIM],P[DIM],vis[DIM],sub[DIM];
ll dp[DIM];
int fcmp(int v,int par){
depth[v] = depth[par]+1;
int mi = depth[v];
for(auto to:G[v]){
if (to==par)
continue;
if (depth[to]){
if (depth[to]<depth[v])
mi = min(mi,depth[to]);
continue;
}
mi = min(mi,fcmp(to,v));
}
if (v!=par && mi<=depth[par])
P[v] = par;
else
P[v] = v;
return mi;
}
int F(int x){
return x==P[x]?x:P[x] = F(P[x]);
}
ll res = 0,ans = 0;
void dfs(int v){
vis[v] = 1;
dp[v] = ll(sz[v]-1)*(sz[v]-2) + sz[v] - 1;
ans+=ll(sz[v])*(sz[v]-1)*(sz[v]-2);
sub[v] = sz[v];
for(auto to:G[v]){
if (vis[to])
continue;
dfs(to);
res+=dp[to]*sub[v]+dp[v]*sub[to];
dp[v] = dp[v]+dp[to]+ll(sz[v])*sub[to];
sub[v]+=sz[to];
}
}
int solve(){
int n,m;
cin>>n>>m;
for(int i = 1;i<=m;++i){
ll u,v;
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
// del
for(int i = 1;i<=n;++i)
sz[i] = 1;
/*for(int i = 1;i<=n;++i){
if (!P[i]) {
fcmp(i, i);
}
}
for(int i = 1;i<=n;++i){
++sz[F(i)];
for(auto to:G[i])
if (F(to)!=F(i))
NG[F(to)].push_back(F(i)),NG[F(i)].push_back(F(to));
}
swap(G,NG);
*/
for(int i = 1;i<=n;++i){
if (sz[i] && !vis[i])
dfs(i);
}
cout<<res*2+ans<<endl;
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
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... |