답안 #402802

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
402802 2021-05-12T11:18:18 Z dxz05 철인 이종 경기 (APIO18_duathlon) C++14
31 / 100
866 ms 1048580 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define revall(x) (x).rbegin(), (x).rend()

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 3e2;
const int M = 2222;

int n, m;
vector<int> g[N];

set<pair<int, int>> deleted;

bool check(int s, int c, int f){
    queue<int> q;
    vector<int> parent(n + 1);
    vector<bool> used(n + 1);
    q.push(c);
    while (!q.empty()){
        int x = q.front(); q.pop();
        for (int y : g[x]){
            if (!used[y] && y != f){
                used[y] = true;
                parent[y] = x;
                q.push(y);
            }
        }
    }

    if (!used[s]) return false;

    int ss = s;
    while (ss != c){
        deleted.insert(make_pair(ss, parent[ss]));
        deleted.insert(make_pair(parent[ss], ss));
        ss = parent[ss];
    }

    used.assign(n + 1, 0);

    q.push(c);
    while (!q.empty()){
        int x = q.front(); q.pop();
        for (int y : g[x]){
            if (!used[y] && deleted.find(make_pair(x, y)) == deleted.end() && y != s){
                used[y] = true;
                q.push(y);
            }
        }
    }

    deleted.clear();

    if (!used[f]) return false;
//    cout << s << ' ' << c << ' ' << f << endl;
    return true;
}

void Subtask_1(){
    int ans = 0;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            for (int k = 1; k <= n; k++){
                if (i == j || i == k || j == k) continue;
                bool cur = check(i, j, k) || check(k, j, i);
                ans += cur;
            }
        }
    }
    cout << ans;
    exit(0);
}

int cnt = 0, color[N];

bool cycle = false;

void dfs(int v, int p){
    color[v] = 1;
    cnt++;
    for (int u : g[v]){
        if (u == p) continue;
        if (color[u] == 1) cycle = true;
        if (color[u] == 0) dfs(u, v);
    }
    color[v] = 2;
}
void Subtask_3(){
    ll ans = 0;
    for (int i = 1; i <= n; i++){
        if (color[i] == 0){
            cnt = 0;
            cycle = false;
            dfs(i, 0);
            ans += 1ll * cnt * (cnt - 1) * (cnt - 2) / (cycle ? 1 : 3);
        }
    }

    cout << ans;

    exit(0);
}
ll ans5 = 0;
int subtree[N];

bool used[N];

void dfs2(int v, int p){
    used[v] = true;
    subtree[v] = 1;
    for (int u : g[v]){
        if (u != p){
            dfs2(u, v);
            subtree[v] += subtree[u];
        }
    }

    for (int u : g[v]){
        if (u != p){
            ll x = subtree[u], y = cnt - subtree[u] - 1;
            ans5 += x * y;
        } else {
            ll x = subtree[v] - 1, y = cnt - x - 1;
            ans5 += x * y;
        }
    }

}

void Subtask_5(){
    memset(color, 0, sizeof(color));
    for (int i = 1; i <= n; i++){
        if (!used[i]){
            cnt = 0;
            dfs(i, i);
            dfs2(i, i);
        }
    }
    cout << ans5 << endl;
    exit(0);
}

void solve(int TC) {
    cin >> n >> m;

    bool ok3 = true;
    while (m--) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);

        ok3 &= g[u].size() <= 2;
        ok3 &= g[v].size() <= 2;
    }

    if (n <= 50 && m <= 100) {
        Subtask_1();
    }

    if (ok3){
        Subtask_3();
    }

    Subtask_5();

}


int main() {
    startTime = clock();
    cin.tie(nullptr); cout.tie(nullptr);
    ios_base::sync_with_stdio(false);

    bool llololcal = false;
#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    llololcal = true;
#endif

    int TC = 1;
    //  cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

    if (llololcal) cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 16 ms 23832 KB Output is correct
3 Correct 16 ms 23720 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23796 KB Output is correct
6 Correct 15 ms 23712 KB Output is correct
7 Incorrect 16 ms 23800 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 16 ms 23832 KB Output is correct
3 Correct 16 ms 23720 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23796 KB Output is correct
6 Correct 15 ms 23712 KB Output is correct
7 Incorrect 16 ms 23800 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 31664 KB Output is correct
2 Correct 66 ms 31772 KB Output is correct
3 Correct 72 ms 30092 KB Output is correct
4 Correct 64 ms 30912 KB Output is correct
5 Correct 72 ms 29624 KB Output is correct
6 Correct 74 ms 29628 KB Output is correct
7 Correct 68 ms 29092 KB Output is correct
8 Correct 80 ms 29380 KB Output is correct
9 Correct 67 ms 28836 KB Output is correct
10 Correct 62 ms 29040 KB Output is correct
11 Correct 55 ms 28280 KB Output is correct
12 Correct 53 ms 28212 KB Output is correct
13 Correct 50 ms 28112 KB Output is correct
14 Correct 52 ms 27976 KB Output is correct
15 Correct 44 ms 27404 KB Output is correct
16 Correct 43 ms 27264 KB Output is correct
17 Correct 16 ms 24220 KB Output is correct
18 Correct 16 ms 24132 KB Output is correct
19 Correct 16 ms 24200 KB Output is correct
20 Correct 18 ms 24140 KB Output is correct
21 Correct 16 ms 24140 KB Output is correct
22 Correct 15 ms 24108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 27724 KB Output is correct
2 Correct 19 ms 27744 KB Output is correct
3 Correct 18 ms 27792 KB Output is correct
4 Correct 16 ms 23884 KB Output is correct
5 Correct 17 ms 27796 KB Output is correct
6 Correct 18 ms 27724 KB Output is correct
7 Correct 17 ms 23756 KB Output is correct
8 Correct 17 ms 27688 KB Output is correct
9 Correct 17 ms 27800 KB Output is correct
10 Correct 21 ms 27788 KB Output is correct
11 Correct 21 ms 27668 KB Output is correct
12 Correct 21 ms 27704 KB Output is correct
13 Correct 17 ms 27724 KB Output is correct
14 Correct 17 ms 27768 KB Output is correct
15 Correct 17 ms 27788 KB Output is correct
16 Correct 17 ms 27724 KB Output is correct
17 Correct 17 ms 27724 KB Output is correct
18 Correct 18 ms 27788 KB Output is correct
19 Correct 18 ms 27796 KB Output is correct
20 Correct 18 ms 27716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 32452 KB Output is correct
2 Correct 86 ms 32656 KB Output is correct
3 Correct 74 ms 32688 KB Output is correct
4 Correct 78 ms 32652 KB Output is correct
5 Correct 72 ms 32640 KB Output is correct
6 Correct 72 ms 30400 KB Output is correct
7 Correct 77 ms 35376 KB Output is correct
8 Correct 82 ms 34712 KB Output is correct
9 Correct 86 ms 33960 KB Output is correct
10 Correct 92 ms 32656 KB Output is correct
11 Correct 81 ms 32536 KB Output is correct
12 Correct 68 ms 32588 KB Output is correct
13 Correct 68 ms 32528 KB Output is correct
14 Correct 65 ms 32396 KB Output is correct
15 Correct 58 ms 32068 KB Output is correct
16 Correct 44 ms 31144 KB Output is correct
17 Correct 53 ms 32828 KB Output is correct
18 Correct 52 ms 32828 KB Output is correct
19 Correct 55 ms 33040 KB Output is correct
20 Correct 57 ms 32856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 27700 KB Output is correct
2 Correct 17 ms 27724 KB Output is correct
3 Runtime error 715 ms 1048580 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 31616 KB Output is correct
2 Correct 87 ms 31668 KB Output is correct
3 Runtime error 866 ms 1048580 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 16 ms 23832 KB Output is correct
3 Correct 16 ms 23720 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23796 KB Output is correct
6 Correct 15 ms 23712 KB Output is correct
7 Incorrect 16 ms 23800 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 16 ms 23832 KB Output is correct
3 Correct 16 ms 23720 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 16 ms 23796 KB Output is correct
6 Correct 15 ms 23712 KB Output is correct
7 Incorrect 16 ms 23800 KB Output isn't correct
8 Halted 0 ms 0 KB -