답안 #441452

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441452 2021-07-05T08:18:41 Z abc864197532 Paths (BOI18_paths) C++17
100 / 100
265 ms 20288 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define test(args...) abc("[" + string(#args) + "]", args)
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
const int mod = 1e9 + 7, N = 100000;

int cnt2[N][5][5];

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    vector <int> color(n, 0);
    vector <pii> edge;
    vector <vector <int>> cnt(n, vector <int>(k, 0));
    for (int i = 0; i < n; ++i) cin >> color[i], --color[i];
    for (int i = 0, u, v; i < m; ++i) {
        cin >> u >> v, --u, --v;
        if (color[u] == color[v]) continue;
        edge.eb(u, v);
        cnt[u][color[v]]++;
        cnt[v][color[u]]++;
    }
    lli ans = 0;
    // len = 2
    ans += edge.size() * 2;
    // len = 3;
    for (pii A : edge) {
        int u, v;
        tie(u, v) = A;
        for (int i = 0; i < k; ++i) if (i != color[v] && i != color[u]) {
            ans += cnt[u][i] + cnt[v][i];
        }
    }
    // len = 4
    for (pii A : edge) {
        int u, v;
        tie(u, v) = A;
        for (int i = 0; i < k; ++i) for (int j = 0; j < k; ++j) {
            if (i == j) continue;
            if (i == color[v] || i == color[u]) continue;
            if (j == color[v] || j == color[u]) continue;
            ans += 1ll * cnt[v][i] * cnt[u][j] + 1ll * cnt[v][j] * cnt[u][i];
        }
    }
    if (k <= 4) return cout << ans << endl, 0;
    // len = 5
    for (pii A : edge) {
        int u, v;
        tie(u, v) = A;
        for (int t : {0, 1}) {
            for (int i = 0; i < k; ++i) if (i != color[v]) {
                cnt2[u][color[v]][i] += cnt[v][i];
            }
            swap(u, v);
        }
    }
    for (pii A : edge) {
        int u, v;
        tie(u, v) = A;
        for (int i = 0; i < k; ++i) if (i != color[u] && i != color[v]) {
            int xorr = 4 ^ i ^ color[u] ^ color[v];
            for (int j = 0; j < k; ++j) if (j != i && j != color[u] && j != color[v]) {
                int nj = xorr ^ j;
                ans += 1ll * cnt[u][i] * cnt2[v][j][nj] + 1ll * cnt[v][i] * cnt2[u][j][nj];
            }
        }
    }
    cout << ans << endl;
}

Compilation message

paths.cpp: In function 'int main()':
paths.cpp:67:18: warning: unused variable 't' [-Wunused-variable]
   67 |         for (int t : {0, 1}) {
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 2756 KB Output is correct
2 Correct 68 ms 2736 KB Output is correct
3 Correct 201 ms 20212 KB Output is correct
4 Correct 86 ms 4400 KB Output is correct
5 Correct 63 ms 2124 KB Output is correct
6 Correct 165 ms 14364 KB Output is correct
7 Correct 227 ms 20156 KB Output is correct
8 Correct 216 ms 20288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 76 ms 2756 KB Output is correct
12 Correct 68 ms 2736 KB Output is correct
13 Correct 201 ms 20212 KB Output is correct
14 Correct 86 ms 4400 KB Output is correct
15 Correct 63 ms 2124 KB Output is correct
16 Correct 165 ms 14364 KB Output is correct
17 Correct 227 ms 20156 KB Output is correct
18 Correct 216 ms 20288 KB Output is correct
19 Correct 76 ms 2912 KB Output is correct
20 Correct 69 ms 2624 KB Output is correct
21 Correct 200 ms 20236 KB Output is correct
22 Correct 85 ms 4388 KB Output is correct
23 Correct 63 ms 2200 KB Output is correct
24 Correct 161 ms 14372 KB Output is correct
25 Correct 203 ms 20184 KB Output is correct
26 Correct 207 ms 20236 KB Output is correct
27 Correct 74 ms 2616 KB Output is correct
28 Correct 86 ms 3044 KB Output is correct
29 Correct 264 ms 20228 KB Output is correct
30 Correct 210 ms 11492 KB Output is correct
31 Correct 207 ms 11504 KB Output is correct
32 Correct 265 ms 20160 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 320 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 39 ms 1620 KB Output is correct
3 Correct 25 ms 1988 KB Output is correct
4 Correct 54 ms 7740 KB Output is correct
5 Correct 54 ms 7856 KB Output is correct
6 Correct 121 ms 17208 KB Output is correct
7 Correct 25 ms 1952 KB Output is correct
8 Correct 63 ms 7860 KB Output is correct
9 Correct 59 ms 7796 KB Output is correct
10 Correct 68 ms 7740 KB Output is correct
11 Correct 64 ms 9328 KB Output is correct
12 Correct 90 ms 13452 KB Output is correct
13 Correct 74 ms 9424 KB Output is correct
14 Correct 106 ms 17208 KB Output is correct
15 Correct 109 ms 17208 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct