Submission #8529342


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(long long i=0;i<n;++i)
#define REPP(i,m,n) for(long long i=m;i<n;++i)
#define de cout << "line : " << __LINE__ << " debug" << endl;

struct UnionFind{
    vector<long long> par,r;

    //はじめに全てが根であるとしておく
    UnionFind(long long N):par(N),r(N,1){iota(par.begin(),par.end(),0);}

    long long root(long long x){//根に当たるまで再帰で探す
        if(par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    bool same(long long x,long long y){//つながっているかどうか判定
        return root(x) == root(y);
    }

    long long size(long long x){ //根の大きさを判定する
        return r[root(x)];
    }

    void unite(long long x, long long y){//根が同じでなかったらつなげる
        x = root(x), y = root(y);
        if(x == y) return;
        if(r[x] < r[y]) swap(x,y); //値を小さいほうを根にする
        r[x] += r[y];
        par[y] = x;
        return;
    }
};


signed main() {
    cin.tie(0);
	ios::sync_with_stdio(false);
    long long N,M;cin >> N >> M;
    UnionFind UF(2*N);
    long long A,B;
    REP(i,M){
        cin >> A >> B;A--;B--;
        UF.unite(A,B + N);
        UF.unite(A + N,B);
    }

    REP(i,N){
        if(UF.same(i,i + N)){
            cout << (long long)(N-1)*N/2 - M << endl;
            return 0;
        }
    }
    long long b = 0,w = 0;
    REP(i,N){
        if(UF.same(0,i)) b++;
        else w++;
    }
    cout << (long long)b*w - M << endl;
}

Submission Info

Submission Time
Task C - 3 Steps
User rat
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1622 Byte
Status AC
Exec Time 28 ms
Memory 3456 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 2 ms 256 KB
02-01.txt AC 25 ms 3072 KB
02-02.txt AC 26 ms 3456 KB
02-03.txt AC 26 ms 3328 KB
02-04.txt AC 28 ms 3456 KB
02-05.txt AC 28 ms 3456 KB
02-06.txt AC 26 ms 3456 KB
02-07.txt AC 21 ms 1792 KB
02-08.txt AC 25 ms 3072 KB
02-09.txt AC 26 ms 3328 KB
02-10.txt AC 14 ms 256 KB
02-11.txt AC 16 ms 640 KB
02-12.txt AC 21 ms 1792 KB
02-13.txt AC 22 ms 2688 KB
02-14.txt AC 27 ms 3328 KB
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB