# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
49592 | mohammad_kilani | Duathlon (APIO18_duathlon) | C++17 | 120 ms | 14324 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 200000000
const int N = 100010;
const int M = 100010;
int n , m ,comp[N] , num[N] , comp_cnt = 0 , u , v , dfs_cnt = 0 , dfs_low[N] , dfs_num[N] , num1[N] , num2[N] ;
bool vis[N] ;
long long all = 0 , ans = 0;
vector< int > g[N] ;
void DFS1(int node){
comp[node] = comp_cnt;
num[comp_cnt]++;
vis[node] = true;
for(int i=0;i<g[node].size();i++){
if(vis[g[node][i]] == false)
DFS1(g[node][i]);
}
}
void DFS(int node, int prnt){
dfs_low[node] = dfs_num[node] = dfs_cnt++;
num1[node] = 1;
num2[node] = 1;
vis[node] = true;
for(int i=0;i<g[node].size();i++){
if(g[node][i] == prnt) continue;
if(vis[g[node][i]]){
dfs_low[node] = min(dfs_low[node],dfs_num[g[node][i]]);
continue;
}
DFS(g[node][i],node);
dfs_low[node] = min(dfs_low[node],dfs_low[g[node][i]]);
num1[node] += num1[g[node][i]];
if(dfs_low[g[node][i]] == dfs_num[node]){
ans += (long long)num2[g[node][i]] * (num2[g[node][i]] + 1) * (num2[g[node][i]] - 1);
all -= (long long)num2[g[node][i]] * (num2[g[node][i]] + 1) * (num2[g[node][i]] - 1) / 6;
ans += (long long)(num[comp[node]] - (1 + num1[g[node][i]])) * num1[g[node][i]] * 2;
all -= (long long)(num[comp[node]] - (1 + num1[g[node][i]])) * num1[g[node][i]];
}
else if(dfs_low[g[node][i]] < dfs_num[node]){
num2[node] += num2[g[node][i]];
}
else{
ans += (long long)(num[comp[node]] - (1 + num1[g[node][i]])) * num1[g[node][i]] * 2;
all -= (long long)(num[comp[node]] - (1 + num1[g[node][i]])) * num1[g[node][i]];
}
}
}
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] == false){
DFS1(i);
all += (long long)num[comp_cnt] * (num[comp_cnt] - 1) * (num[comp_cnt] - 2) / 6;
comp_cnt++;
}
}
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
if(vis[i] == false)
DFS(i,-1);
}
ans += (long long)all * 4;
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... |