# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
55732 | mohammad_kilani | Duathlon (APIO18_duathlon) | C++17 | 260 ms | 76536 KiB |
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>
using namespace std;
#define mod 1000000007
#define oo 1000000000
const int N = 100010;
int n , m , u , v , dfs_low[N] , dfs_num[N] , dfs_cnt = 0 , all[N], comp[N] , comp_cnt = 0;
vector< int > g[N] ;
bool vis[N];
long long ans = 0 , A , B;
void DFS2(int node){
vis[node] = true;
comp[node] = comp_cnt;
all[comp_cnt]++;
for(int i=0;i<g[node].size();i++){
if(!vis[g[node][i]])
DFS2(g[node][i]);
}
}
pair<int,pair<int,pair<long long,int> > > DFS(int node,int prnt){
dfs_low[node] = dfs_num[node] = ++dfs_cnt;
int num1 = 1 , num2 = 1 , num3 = all[comp[node]] - 1, num5 = 0 , num6 = 0;
long long num4 = 0;
pair<int,pair<int,pair<long long,int> > > tmp;
for(int i=0;i<g[node].size();i++){
if(g[node][i] == prnt) continue;
if(dfs_num[g[node][i]] != 0){
dfs_low[node] = min(dfs_low[node],dfs_num[g[node][i]]);
continue;
}
tmp = DFS(g[node][i],node);
num1 += tmp.first;
dfs_low[node] = min(dfs_low[node],dfs_low[g[node][i]]);
if(dfs_low[g[node][i]] == dfs_num[node]){
num3 -= tmp.first;
num4 += (long long)tmp.first * num6;
num5 += tmp.first;
tmp.second.second.first += (long long)(all[comp[node]] - 1 - tmp.first) * tmp.second.second.second;
ans += (long long)tmp.first * num3 * 2;
ans += (long long)(tmp.second.first + 1) * tmp.second.first * (tmp.second.first - 1);
ans += (long long)tmp.second.first * (tmp.second.first - 1) * (all[comp[node]] - tmp.second.first - 1) * 2;
ans += (long long)(tmp.second.first - 1) * tmp.second.second.first * 2;
}
else if(dfs_low[g[node][i]] > dfs_num[node]){
num5 += tmp.first;
num4 += (long long)tmp.first * num6;
num3 -= tmp.first;
ans += (long long)tmp.first * num3 * 2;
}
else{
num2 += tmp.second.first;
num4 += tmp.second.second.first;
num4 += (long long)num5 * tmp.second.second.second;
num5 += tmp.second.second.second;
num6 += tmp.second.second.second;
}
}
return make_pair(num1,make_pair(num2,make_pair(num4,num5)));
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for(int i=1;i<=n;i++){
if(!vis[i]){
DFS2(i);
comp_cnt++;
}
}
for(int i=1;i<=n;i++){
if(dfs_num[i] == 0)
DFS(i,-1);
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
# | 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... |