Submission #3011624


Source Code Expand

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

bool dfs(vector<vector<long long> > &adj, vector<long long> &used, long long node, long long color){

    used[node] = color;
    //cout << node << " " << color << endl;
    for(auto to : adj[node]){

        //同色なら
        if(used[to] == color) return false;
        else if(used[to] == 0){
            //見訪問なら
            long long nextcolor = color + 1;
            if(nextcolor == 3) nextcolor = 1;
            if(!dfs(adj, used, to, nextcolor)) return false;
        }
    }

    return true;
}

int main(){

    long long n, m; cin >> n >> m;
    vector<vector<long long> > adj(n);
    for(long long i = 0; i < m; i++){
        long long a, b; cin >> a >> b;
        a--;
        b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    vector<long long> used(n, false);
    if(dfs(adj, used, 0, 1)){
        long long B = 0;
        long long R = 0;
        for(auto color : used){
            color == 1 ? B++ : R++;
        }

        cout << B * R - m << endl;
    }
    else cout << (n * (n - 1) / 2) - m << endl;
    return 0;
}

Submission Info

Submission Time
Task C - 3 Steps
User monkukui
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1167 Byte
Status AC
Exec Time 79 ms
Memory 7552 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 28
Set Name Test Cases
sample sample-01.txt, sample-02.txt
all sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
01-01.txt AC 1 ms 256 KB
01-02.txt AC 1 ms 256 KB
01-03.txt AC 1 ms 256 KB
01-04.txt AC 1 ms 256 KB
01-05.txt AC 1 ms 256 KB
01-06.txt AC 1 ms 256 KB
01-07.txt AC 1 ms 256 KB
01-08.txt AC 1 ms 256 KB
01-09.txt AC 2 ms 256 KB
01-10.txt AC 3 ms 384 KB
02-01.txt AC 73 ms 6656 KB
02-02.txt AC 76 ms 7040 KB
02-03.txt AC 75 ms 7040 KB
02-04.txt AC 79 ms 7040 KB
02-05.txt AC 79 ms 7040 KB
02-06.txt AC 75 ms 7040 KB
02-07.txt AC 70 ms 4992 KB
02-08.txt AC 72 ms 6656 KB
02-09.txt AC 73 ms 7040 KB
02-10.txt AC 44 ms 3072 KB
02-11.txt AC 58 ms 3584 KB
02-12.txt AC 72 ms 5888 KB
02-13.txt AC 73 ms 6528 KB
02-14.txt AC 78 ms 7552 KB
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB