Submission #8529237
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#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 rep(i,n) for(long long i = n-1;i>=0;--i)
#define repp(i,n,m) for(long long i = n-1; i >= m; --i)
#define ALL(N) (N.begin(),N.end())
#define de cout << "line : " << __LINE__ << " debug" << endl;
#define pb push_back
#define pq priority_queue
#define Dcout(N) cout << setprecision(20) << N << endl
constexpr ll INF = 2147483647;
constexpr long long INFF = 9223372036854775807;
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]);
}
long long deep(long long x,long long A){
if(par[x] == x) return A;
else{A++;return deep(par[x],A);}
}
bool same(long long x,long long y){//つながっているかどうか判定
return par[x] == par[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);
}
long long sum = 0,sq = 0;
REP(i,N) sum += i;
REP(i,N){
if(UF.same(i,i + N)){
cout << sum - M << endl;
return 0;
}
}
ll b = 0,w = 0;
REP(i,N){
if(UF.same(0,i)) b++;
else w++;
}
sq = b*w;
cout << sq - M << endl;
}
Submission Info
Submission Time |
|
Task |
C - 3 Steps |
User |
rat |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
2139 Byte |
Status |
WA |
Exec Time |
26 ms |
Memory |
3328 KB |
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
0 / 500 |
Status |
|
|
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 |
WA |
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 |
WA |
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 |
3328 KB |
02-03.txt |
AC |
26 ms |
3328 KB |
02-04.txt |
WA |
25 ms |
3328 KB |
02-05.txt |
WA |
26 ms |
3328 KB |
02-06.txt |
AC |
25 ms |
3328 KB |
02-07.txt |
AC |
20 ms |
1792 KB |
02-08.txt |
AC |
25 ms |
3072 KB |
02-09.txt |
AC |
26 ms |
3328 KB |
02-10.txt |
AC |
15 ms |
256 KB |
02-11.txt |
AC |
16 ms |
640 KB |
02-12.txt |
WA |
20 ms |
1792 KB |
02-13.txt |
WA |
22 ms |
2688 KB |
02-14.txt |
WA |
25 ms |
3328 KB |
sample-01.txt |
AC |
1 ms |
256 KB |
sample-02.txt |
AC |
1 ms |
256 KB |