# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
54068 | bogdan10bos | Duathlon (APIO18_duathlon) | C++14 | 135 ms | 13236 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 FILE_IO
typedef long long LL;
LL ans;
int N, M;
int sz[100005], cmp[100005];
int f[100005];
vector<int> edg[100005];
void DFS(int nod, int fth = 0)
{
if(f[nod]) return;
f[nod] = 1;
if(fth == 0) cmp[nod] = nod;
else cmp[nod] = cmp[fth];
sz[nod] = 1;
LL cnt = 0;
for(auto nxt: edg[nod])
{
if(nxt == fth) continue;
DFS(nxt, nod);
sz[nod] += sz[nxt];
}
}
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
scanf("%d%d", &N, &M);
for(int i = 1; i <= M; i++)
{
int x, y;
scanf("%d%d", &x, &y);
edg[x].push_back(y);
edg[y].push_back(x);
}
for(int i = 1; i <= N; i++)
if(!f[i])
DFS(i);
for(int i = 1; i <= N; i++)
{
int K = sz[ cmp[i] ];
int nod = i;
for(auto nxt: edg[nod])
{
if(sz[nxt] > sz[nod]) continue;
ans += 1LL * sz[nxt] * (K - sz[nxt] - 1);
}
ans += 1LL * (K - sz[nod]) * (sz[nod] - 1);
}
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... |