이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back
using namespace std;
using pii=pair <int,int>;
int n,m;
vector <int> g[100010],bc[200020];
bool visited[200010];
int tme,dfn[200020],low[200020],curid;
stack <int> st;
int weigh[200020];
int cntnode;
void dfs(int cur){
tme++;
dfn[cur]=tme; low[cur]=tme;
st.push(cur);
cntnode++;
visited[cur]=1;
for (int i:g[cur]){
if (!visited[i]){
dfs(i);
low[cur]=min(low[cur],low[i]);
if (low[i]==dfn[cur]){
curid++;
while (st.top()!=cur){
bc[st.top()].pb(curid);
bc[curid].pb(st.top());
st.pop();
weigh[curid]++;
}
bc[cur].pb(curid);
bc[curid].pb(cur);
weigh[curid]++;
}
} else low[cur]=min(low[cur],dfn[i]);
}
}
int ans,rtsz;
int siz[200020];
void dfs2(int cur,int prv){
siz[cur]=(cur<=n);
for (int i:bc[cur]){
if (i==prv) continue;
dfs2(i,cur);
ans+=(cur<=n?-1:weigh[cur])*siz[cur]*siz[i];
siz[cur]+=siz[i];
}
ans+=(cur<=n?-1:weigh[cur])*siz[cur]*(cntnode-siz[cur]);
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin>>n>>m;
while (m--){
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
curid=n;
for (int i=1; i<=n; i++){
if (visited[i]) continue;
while (!st.empty()) st.pop();
cntnode=0;
dfs(i);
dfs2(i,-1);
}
cout<<2*ans<<'\n';
}
# | 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... |