# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
128350 | shafinalam | Duathlon (APIO18_duathlon) | C++14 | 1055 ms | 1048580 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;
const int mxn = 1e5+5;
typedef long long ll;
vector<int>adj[mxn];
ll sub[mxn];
bool vis[mxn];
ll ans = 0;
void dfs1(int u, int p)
{
sub[u] = 1;
vis[u] = 1;
for(auto v : adj[u])
{
if(v==p) continue;
dfs1(v, u);
sub[u]+=sub[v];
}
}
void dfs2(int u, int p, int root)
{
ans+=((sub[root]-sub[u])*(sub[u]-1))*2;
for(auto v : adj[u])
{
if(v==p) continue;
dfs2(v, u, root);
}
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
dfs1(i, i);
dfs2(i, i, i);
}
}
printf("%lld\n", ans);
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... |