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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n, m;
vector<vector<int>> v, tree, backedge;
vector<bool> bl;
vector<bool> marked;
vector<int> sz;
ll ans = 0;
void dfs_tree(int nd){
bl[nd] = 1;
for(int x: v[nd]){
if(!bl[x]){
bl[x] = 1;
tree[nd].pb(x);
dfs_tree(x);
}
else backedge[nd].pb(x);
}
}
void mark(int nd){
marked[nd] = !marked[nd];
for(int x: tree[nd]) mark(x);
}
void dfs_sz(int nd){
sz[nd] = 1;
for(int x: tree[nd]) dfs_sz(x);
for(int x: tree[nd]) sz[nd]+=sz[x];
}
int SIZE = 0;
int dfs(int nd){
int s = 0;
for(int x: tree[nd]) s+=dfs(x);
ans+=s;
bool bkg = 0;
for(int x: backedge[nd]) if(!marked[x]) bkg = 1;
if(bkg) ans+=SIZE-sz[nd], s++;
return s;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
v.assign(n, {});
for(int i = 0; i < n; i++){
int a, b; cin >> a >> b; a--, b--;
v[a].pb(b);
v[b].pb(a);
}
marked.assign(n, 0);
sz.resize(n);
for(int i = 0; i < n; i++){
tree.assign(n, {});
backedge.assign(n, {});
bl.assign(n, 0);
dfs_tree(i);
dfs_sz(i);
for(int x: tree[i]){
ans+=ll(sz[i]-1-sz[x])*ll(sz[x]);
SIZE=sz[x];
mark(x);
dfs(x);
mark(x);
}
}
cout << 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... |