Submission #408058

# Submission time Handle Problem Language Result Execution time Memory
408058 2021-05-19T05:49:02 Z tengiz05 Duathlon (APIO18_duathlon) C++17
0 / 100
102 ms 10820 KB
#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 1e5;
std::vector<int> e[N];
int n, m;
std::vector<bool> vis;
bool cycle;
int sz;
void dfs(int u, int p) {
    sz++;
    vis[u] = true;
    for (auto v : e[u]) {
        if (v == p)continue;
        if (vis[v]) cycle = true;
        else dfs(v, u);
    }
}
i64 ans;
int calc(int u, int p) {
    int cnt = 0;
    for (auto v : e[u]) {
        if (v != p) {
            cnt += calc(v, u);
        }
    }
    ans += i64(cnt) * (sz - cnt - 1) * 2ll;
    return cnt + 1;
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    vis.assign(n, false);
    for (int i = 0; i < n; i++) {
        if (!vis[i]) {
            sz = 0;
            cycle = false;
            dfs(i, i);
            if (cycle) {
                ans += i64(sz) * (sz - 1) * (sz - 2);
            } else {
                calc(i, i);
            }
        }
    }
    std::cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2664 KB Output is correct
7 Incorrect 2 ms 2636 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2664 KB Output is correct
7 Incorrect 2 ms 2636 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 99 ms 10716 KB Output is correct
2 Correct 62 ms 10820 KB Output is correct
3 Incorrect 102 ms 8480 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2636 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 63 ms 6244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2636 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 81 ms 6144 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2664 KB Output is correct
7 Incorrect 2 ms 2636 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2664 KB Output is correct
7 Incorrect 2 ms 2636 KB Output isn't correct
8 Halted 0 ms 0 KB -