Submission #1499167


Source Code Expand

#include <iostream>
#include <vector>
#include <queue>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
using ll = long long;
using namespace std;
int main() {
    // input
    int n, m; cin >> n >> m;
    vector<vector<int> > g(n);
    repeat (i,m) {
        int u, v; cin >> u >> v; -- u; -- v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    // decompose
    int a_components = 0, a_nodes = 0; // connected graph: |V|  = 1
    int b_components = 0, b_nodes = 0; // connected graph: |V| >= 2 and no odd cyles
    int c_components = 0, c_nodes = 0; // connected graph: |V| >= 2 and odd cycles exist
    vector<char> used(n);
    repeat (root,n) if (not used[root]) {
        bool is_bipartite = true;
        int size = 0;
        queue<int> que;
        que.push(root);
        used[root] = 'A';
        while (not que.empty()) {
            int i = que.front(); que.pop();
            ++ size;
            for (int j : g[i]) {
                if (used[j]) {
                    if (used[i] == used[j]) is_bipartite = false;
                } else {
                    used[j] = (used[i] == 'A' ? 'B' : 'A');
                    que.push(j);
                }
            }
        }
        if (size == 1) {
            ++ a_components; a_nodes += size;
        } else if (is_bipartite) {
            ++ b_components; b_nodes += size;
        } else {
            ++ c_components; c_nodes += size;
        }
    }
    // output
    ll ans = 0;
    ans += a_nodes *(ll) a_nodes; // A A
    ans += 2 * a_nodes *(ll) b_nodes; // A B ; B A
    ans += 2 * a_nodes *(ll) c_nodes; // A C ; C A
    ans += b_components *(ll) b_components * 2; // B B
    ans += 2 * b_components *(ll) c_components; // B C ; C B
    ans += c_components *(ll) c_components; // C C
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task C - Squared Graph
User kimiyuki
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1878 Byte
Status AC
Exec Time 153 ms
Memory 7040 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 2
AC × 32
Set Name Test Cases
Sample sample1.txt, sample2.txt
All sample1.txt, sample2.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in3.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt
Case Name Status Exec Time Memory
in1.txt AC 1 ms 256 KB
in10.txt AC 18 ms 2688 KB
in11.txt AC 1 ms 256 KB
in12.txt AC 52 ms 5760 KB
in13.txt AC 51 ms 5760 KB
in14.txt AC 84 ms 5760 KB
in15.txt AC 31 ms 2944 KB
in16.txt AC 79 ms 4224 KB
in17.txt AC 56 ms 4992 KB
in18.txt AC 112 ms 5888 KB
in19.txt AC 82 ms 5504 KB
in2.txt AC 1 ms 256 KB
in20.txt AC 83 ms 5504 KB
in21.txt AC 153 ms 7040 KB
in22.txt AC 109 ms 3328 KB
in23.txt AC 89 ms 2432 KB
in24.txt AC 27 ms 2688 KB
in25.txt AC 151 ms 7040 KB
in26.txt AC 1 ms 256 KB
in27.txt AC 2 ms 256 KB
in28.txt AC 150 ms 7040 KB
in3.txt AC 1 ms 256 KB
in4.txt AC 1 ms 256 KB
in5.txt AC 134 ms 5376 KB
in6.txt AC 27 ms 2944 KB
in7.txt AC 24 ms 2816 KB
in8.txt AC 35 ms 3584 KB
in9.txt AC 63 ms 4096 KB
sample1.txt AC 1 ms 256 KB
sample2.txt AC 1 ms 256 KB