Submission #1603411


Source Code Expand

#include "bits/stdc++.h"
using namespace std;

#define all(x)  x.begin(), x.end()
#define mp      make_pair
#define pii     pair<int, int>
#define pll     pair<long long, long long>
#define ll      long long

struct UnionFind {
        int n;
        vector<int> parent;
        vector<int> rank;
        vector<int> num;
        int find(int x) {
                if (parent[x] == x) return  x;
                return parent[x] = find(parent[x]);
        }
        UnionFind(int n_) {
                n = n_;
                parent.resize(n);
                for (int i = 0; i < n; i ++) parent[i] = i;
                rank.assign(n, 0);
                num.assign(n, 1);
        }
        void unite(int x, int y) {
                if ((x = find(x)) != (y = find(y))) {
                        if (rank[x] < rank[y]) {
                                parent[x] = y;
                                num[y] += num[x];
                        } else {
                                parent[y] = x;
                                if (rank[x] == rank[y]) rank[x] ++;
                                num[x] += num[y];
                        }
                        n --;
                }
        }
        bool same(int x, int y) { return find(x) == find(y); }
        int get() { return n; }
        int get(int x) { return num[find(x)]; }
};

vector<int> g[100000], rg[100000];

int color[100000];
int A, B, C, edge;

int dfs(int v, int c) {
        if (color[v] == -1) { 
                color[v] = c;
                if (c == 0) A ++;
                else if (c == 1) B ++;
                else C ++;
        } else if (color[v] != c) return -1;
        else return 0;
        for (int u : g[v]) { 
                edge ++;
                if (dfs(u, (c + 1) % 3) == -1) return -1;
        }
        for (int u : rg[v]) { 
                edge ++;
                if (dfs(u, (c - 1 + 3) % 3) == -1) return -1;
        }
        return 0;
}

int main() {
        int n, m;
        cin >> n >> m;
        UnionFind uf(n + 1);
        for (int i = 0; i < m; i ++) {
                int a, b;
                cin >> a >> b;
                a --, b --;
                g[a].push_back(b);
                rg[b].push_back(a);
                uf.unite(a, b);
        }
        memset(color, -1, sizeof color);
        long long ans = 0;
        for (int i = 0; i < n; i ++) if (!uf.same(i, n)) {
                long long num = uf.get(i);
                uf.unite(i, n); //used
                A = 0, B = 0, C = 0, edge = 0;
                if (dfs(i, 0) == -1) ans += num * num;
                else if (C == 0 || B == 0) ans += edge / 2;
                else ans += 1LL * A * B + 1LL * B * C + 1LL * C * A;
        }
        cout << ans << endl;
        return 0;
}

Submission Info

Submission Time
Task F - Blackout
User KokiYmgch
Language C++14 (GCC 5.4.1)
Score 1700
Code Size 2859 Byte
Status AC
Exec Time 87 ms
Memory 17408 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1700 / 1700
Status
AC × 3
AC × 53
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt
Case Name Status Exec Time Memory
0_00.txt AC 3 ms 5376 KB
0_01.txt AC 3 ms 5376 KB
0_02.txt AC 3 ms 5376 KB
1_00.txt AC 3 ms 5376 KB
1_01.txt AC 5 ms 6528 KB
1_02.txt AC 71 ms 12800 KB
1_03.txt AC 75 ms 17408 KB
1_04.txt AC 75 ms 17408 KB
1_05.txt AC 76 ms 17408 KB
1_06.txt AC 75 ms 17408 KB
1_07.txt AC 76 ms 17408 KB
1_08.txt AC 74 ms 17408 KB
1_09.txt AC 75 ms 17408 KB
1_10.txt AC 74 ms 17408 KB
1_11.txt AC 75 ms 17408 KB
1_12.txt AC 74 ms 17408 KB
1_13.txt AC 74 ms 14336 KB
1_14.txt AC 55 ms 9976 KB
1_15.txt AC 55 ms 9976 KB
1_16.txt AC 55 ms 9976 KB
1_17.txt AC 46 ms 6656 KB
1_18.txt AC 82 ms 10368 KB
1_19.txt AC 81 ms 10368 KB
1_20.txt AC 80 ms 10368 KB
1_21.txt AC 83 ms 10496 KB
1_22.txt AC 84 ms 10496 KB
1_23.txt AC 80 ms 10368 KB
1_24.txt AC 79 ms 10368 KB
1_25.txt AC 81 ms 10496 KB
1_26.txt AC 81 ms 10368 KB
1_27.txt AC 80 ms 10368 KB
1_28.txt AC 80 ms 10496 KB
1_29.txt AC 87 ms 10496 KB
1_30.txt AC 79 ms 10368 KB
1_31.txt AC 80 ms 10368 KB
1_32.txt AC 80 ms 10496 KB
1_33.txt AC 82 ms 10496 KB
1_34.txt AC 79 ms 9600 KB
1_35.txt AC 73 ms 8320 KB
1_36.txt AC 78 ms 9088 KB
1_37.txt AC 80 ms 10112 KB
1_38.txt AC 72 ms 7936 KB
1_39.txt AC 69 ms 7808 KB
1_40.txt AC 80 ms 10368 KB
1_41.txt AC 80 ms 10240 KB
1_42.txt AC 72 ms 8704 KB
1_43.txt AC 74 ms 8448 KB
1_44.txt AC 79 ms 9728 KB
1_45.txt AC 68 ms 7552 KB
1_46.txt AC 83 ms 9984 KB
1_47.txt AC 82 ms 10240 KB
1_48.txt AC 73 ms 8192 KB
1_49.txt AC 78 ms 8960 KB