답안 #252125

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
252125 2020-07-24T09:23:58 Z Mlxa 철인 이종 경기 (APIO18_duathlon) C++14
8 / 100
315 ms 63184 KB
#include <bits/stdc++.h>
using ll = long long;
#define int ll
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define mp make_pair
#define mt make_tuple
using namespace std;

const int N = 2e5 + 10;

void umin(int &a, int b) {
    a = min(a, b);
}
void umax(int &a, int b) {
    a = max(a, b);
}

int n;
int m;
vector<int> g[N];
int con[N];
int bycon[N];
int curcon = 0;
int timer  = 0;
int in[N];
int up[N];
int sz[N];
bool cp[N];
int pr[N];
bool used[N];

void cutpoints(int v, int p) {
    assert(con[v] == -1);
    con[v] = curcon;
    in[v] = up[v] = timer++;
    sz[v] = 1;
    pr[v] = p;
    int soncnt = 0;
    for (int u : g[v]) {
        if (con[u] == -1) {
            ++soncnt;
            cutpoints(u, v);
            umin(up[v], up[u]);
            cp[v] |= in[v] <= up[u];
            sz[v] += sz[u];
        } else if (u != p) {
            umin(up[v], in[u]);
        }
    }
    if (p == -1) {
        cp[v] = soncnt >= 2;
    }
}

int newc = 0;

vector<int> vertex[N];
map<int, int> bycol[N];
int maxcol[N];
set<int> colors[N];

void edge(int v, int u, int c) {
    //cerr << "edge " << v + 1 << " " << u + 1 << " " << c + 1 << endl;
    vertex[c].push_back(v);
    vertex[c].push_back(u);
    umax(maxcol[v], c);
    umax(maxcol[u], c);
    colors[v].insert(c);
    colors[u].insert(c);
}

void paint(int v, int c) {
    used[v] = true;
    bycol[v][c] += bycon[con[v]] - sz[v];
    for (int u : g[v]) {
        if (u == pr[v]) {
            continue;
        }
        if (!used[u]) {
            if (in[v] <= up[u]) {
                edge(v, u, newc);
                bycol[v][newc] += sz[u];
                paint(u, newc++);
            } else {
                edge(v, u, c);
                paint(u, c);
            }
        } else if (in[u] < in[v]) {
            edge(v, u, c);
        }
    }
}

signed main() {
#ifdef LC
    assert(freopen("input.txt", "r", stdin));
#endif // LC
    ios::sync_with_stdio(0);
    cin.tie(0);
    fill_n(con, N, -1);
    cin >> n >> m;
    for (int v, u, i = 0; i < m; ++i) {
        cin >> v >> u;
        --v;
        --u;
        g[v].push_back(u);
        g[u].push_back(v);
    }
    for (int i = 0; i < n; ++i) {
        if (con[i] != -1) {
            continue;
        }
        cutpoints(i, -1);
        ++curcon;
    }
    for (int i = 0; i < n; ++i) {
        ++bycon[con[i]];
    }
    /*for (int i = 0; i < n; ++i) {
        cerr << cp[i];
    }
    cerr << endl;*/
    for (int i = 0; i < n; ++i) {
        if (!used[i]) {
            paint(i, newc++);
        }
    }
    int answer = 0;
    for (int i = 0; i < newc; ++i) {
        sort(all(vertex[i]));
        vertex[i].erase(unique(all(vertex[i])), vertex[i].end());
        int cur = (int)vertex[i].size();
        if (!cur) {
            continue;
        }
        answer += cur * (cur - 1) * (cur - 2);
        vector<int> oth;
        for (int v : vertex[i]) {
            for (auto e : bycol[v]) {
                if (e.x != i) {
                    oth.push_back(e.y);
                }
            }
        }
        /*cerr << i + 1 << ":\t";
        for (int e : oth) {
            cerr << e << " ";
        }
        cerr << endl;*/
        int cn = con[vertex[i][0]];
        //cerr << "\t\t###\t\t" << cur << " " << bycon[cn] - cur << endl;
        answer += 2 * (cur - 1) * (cur - 1) * (bycon[cn] - cur);
        cur = 0;
        for (int v : vertex[i]) {
            cur += maxcol[v] == i;
        }
        int half = 0;
        int pref = 0;
        for (int e : oth) {
            half += cur * pref * e;
            pref += e;
        }
        answer += 2 * half;
    }
    for (int i = 0; i < n; ++i) {
        if (colors[i].empty()) {
            continue;
        }
        vector<int> oth;
        for (int c : colors[i]) {
            assert(vertex[c].size() > 0);
            oth.push_back((int)vertex[c].size() - 1);
        }
        int pref = 0;
        int half = 0;
        for (int e : oth) {
            half += pref * e;
            pref += e;
        }
        answer -= 2 * half;
        oth.clear();
        int mcs = -1;
        vector<pair<int, int>> oth2;
        for (int c : colors[i]) {
            assert(vertex[c].size() > 0);
            if (c != maxcol[i]) {
                oth2.emplace_back((int)vertex[c].size() - 1, bycol[i][c]);
            } else {
                mcs = (int)vertex[c].size();
            }
        }
        assert(mcs != -1);
        //cerr << "###\t" << i << " " << mcs  << endl;
        for (auto e : oth2) {
            int small = e.x;
            int part  = e.y;
            int full  = bycon[con[i]];
            answer -= 2 * small * (full - mcs - part);
        }
        pref = 0;
        half = 0;
        for (auto e : oth2) {
            int small = e.x;
            half += pref * small;
            pref += small;
        }
        answer += 2 * half;
    }
    /*
    for (int i = 0; i < newc; ++i) {
        cerr << "\t\tcolor " << i + 1 << ":" << endl;
        for (int j : vertex[i]) {
            cerr << j + 1 << " ";
        }
        cerr << endl;
    }
    for (int i = 0; i < n; ++i) {
        cerr << "vertex " << i + 1 << endl;
        for (auto e : bycol[i]) {
            cerr << e.x + 1 << " " << e.y << endl;
        }
    }
    */
    cout << answer << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 30072 KB Output is correct
2 Correct 18 ms 30080 KB Output is correct
3 Correct 17 ms 30080 KB Output is correct
4 Correct 17 ms 30080 KB Output is correct
5 Correct 17 ms 30080 KB Output is correct
6 Correct 17 ms 30080 KB Output is correct
7 Incorrect 18 ms 30080 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 30072 KB Output is correct
2 Correct 18 ms 30080 KB Output is correct
3 Correct 17 ms 30080 KB Output is correct
4 Correct 17 ms 30080 KB Output is correct
5 Correct 17 ms 30080 KB Output is correct
6 Correct 17 ms 30080 KB Output is correct
7 Incorrect 18 ms 30080 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 227 ms 59364 KB Output is correct
2 Correct 213 ms 59340 KB Output is correct
3 Correct 259 ms 60908 KB Output is correct
4 Correct 229 ms 60108 KB Output is correct
5 Correct 227 ms 57192 KB Output is correct
6 Correct 266 ms 60012 KB Output is correct
7 Correct 241 ms 59448 KB Output is correct
8 Correct 251 ms 60016 KB Output is correct
9 Correct 253 ms 58356 KB Output is correct
10 Correct 261 ms 57968 KB Output is correct
11 Correct 207 ms 55032 KB Output is correct
12 Correct 212 ms 55800 KB Output is correct
13 Correct 188 ms 55288 KB Output is correct
14 Correct 184 ms 54776 KB Output is correct
15 Correct 153 ms 52600 KB Output is correct
16 Correct 150 ms 52344 KB Output is correct
17 Correct 31 ms 41208 KB Output is correct
18 Correct 30 ms 41208 KB Output is correct
19 Correct 29 ms 41088 KB Output is correct
20 Correct 29 ms 41088 KB Output is correct
21 Correct 30 ms 40952 KB Output is correct
22 Correct 29 ms 41120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 30464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 297 ms 62968 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 30464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 315 ms 63184 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 30072 KB Output is correct
2 Correct 18 ms 30080 KB Output is correct
3 Correct 17 ms 30080 KB Output is correct
4 Correct 17 ms 30080 KB Output is correct
5 Correct 17 ms 30080 KB Output is correct
6 Correct 17 ms 30080 KB Output is correct
7 Incorrect 18 ms 30080 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 30072 KB Output is correct
2 Correct 18 ms 30080 KB Output is correct
3 Correct 17 ms 30080 KB Output is correct
4 Correct 17 ms 30080 KB Output is correct
5 Correct 17 ms 30080 KB Output is correct
6 Correct 17 ms 30080 KB Output is correct
7 Incorrect 18 ms 30080 KB Output isn't correct
8 Halted 0 ms 0 KB -