Submission #37344296
Source Code Expand
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
#define chmax(x,y) x = max(x,y);
#define chmin(x,y) x = min(x,y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const double PI = acos(-1);
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> to(n);
dsu d(n);
vector<P> edges;
rep(i,m) {
int u, v;
cin >> u >> v;
u--; v--;
edges.emplace_back(u, v);
to[u].push_back(v);
to[v].push_back(u);
d.merge(u,v);
}
int groupnum = 0;
rep(i,n) {
if (d.leader(i) == i) groupnum++;
}
vector<int> color(n, -1);
auto dfs = [&](auto f, int v, int c, int p=-1) -> bool {
color[v] = c;
for (int u : to[v]) {
if (u == p) continue;
if (color[u] == c) return false;
if (color[u] == -1 && !f(f, u, c^1, v)) return false;
}
return true;
};
rep(i,n) {
if (d.leader(i) == i) {
if (!dfs(dfs, i, 1)) {
cout << 0 << endl;
return 0;
}
}
}
map<int, P> mp;
rep(i,n) {
if (color[i] == 0) mp[d.leader(i)].first++;
if (color[i] == 1) mp[d.leader(i)].second++;
}
vector<ll> cnt0(groupnum), cnt1(groupnum);
int idx = 0;
for (auto [_, p] : mp) {
cnt0[idx] = p.first;
cnt1[idx] = p.second;
idx++;
}
ll ans = (ll)n * (n-1) / 2;
rep(i,groupnum) {
ans -= (ll)cnt0[i] * (cnt0[i]-1) / 2;
ans -= (ll)cnt1[i] * (cnt1[i]-1) / 2;
}
// rep(i,groupnum)rep(j,i) {
// ans += (cnt0[i]+cnt1[i]) * (cnt0[j]+cnt1[j]);
// }
// for (auto [u, v] : edges) {
// if (!d.same(u, v)) ans--;
// else if (color[u] != color[v]) ans--;
// }
cout << ans - m << endl;
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - Make Bipartite 2 |
| User | taki0711 |
| Language | C++ (GCC 9.2.1) |
| Score | 400 |
| Code Size | 2142 Byte |
| Status | AC |
| Exec Time | 191 ms |
| Memory | 40664 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 400 / 400 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt, example1.txt, example2.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, example0.txt, example1.txt, example2.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 8 ms | 3560 KiB |
| 001.txt | AC | 2 ms | 3504 KiB |
| 002.txt | AC | 69 ms | 24968 KiB |
| 003.txt | AC | 9 ms | 9552 KiB |
| 004.txt | AC | 135 ms | 19600 KiB |
| 005.txt | AC | 179 ms | 35220 KiB |
| 006.txt | AC | 191 ms | 40664 KiB |
| 007.txt | AC | 181 ms | 40656 KiB |
| 008.txt | AC | 68 ms | 9216 KiB |
| 009.txt | AC | 70 ms | 9372 KiB |
| 010.txt | AC | 70 ms | 9272 KiB |
| 011.txt | AC | 45 ms | 6916 KiB |
| 012.txt | AC | 91 ms | 21704 KiB |
| 013.txt | AC | 79 ms | 10416 KiB |
| 014.txt | AC | 128 ms | 17596 KiB |
| 015.txt | AC | 67 ms | 24928 KiB |
| 016.txt | AC | 61 ms | 25180 KiB |
| 017.txt | AC | 57 ms | 25180 KiB |
| 018.txt | AC | 59 ms | 25128 KiB |
| 019.txt | AC | 136 ms | 15788 KiB |
| 020.txt | AC | 31 ms | 4996 KiB |
| 021.txt | AC | 85 ms | 9428 KiB |
| 022.txt | AC | 36 ms | 6028 KiB |
| 023.txt | AC | 34 ms | 6108 KiB |
| 024.txt | AC | 101 ms | 24272 KiB |
| 025.txt | AC | 69 ms | 24384 KiB |
| 026.txt | AC | 56 ms | 25076 KiB |
| 027.txt | AC | 58 ms | 24968 KiB |
| 028.txt | AC | 59 ms | 25136 KiB |
| 029.txt | AC | 107 ms | 12364 KiB |
| 030.txt | AC | 99 ms | 11000 KiB |
| 031.txt | AC | 85 ms | 10100 KiB |
| 032.txt | AC | 77 ms | 9344 KiB |
| 033.txt | AC | 11 ms | 3648 KiB |
| 034.txt | AC | 135 ms | 23124 KiB |
| 035.txt | AC | 52 ms | 25132 KiB |
| 036.txt | AC | 63 ms | 24856 KiB |
| 037.txt | AC | 56 ms | 25132 KiB |
| 038.txt | AC | 54 ms | 25084 KiB |
| 039.txt | AC | 166 ms | 24920 KiB |
| 040.txt | AC | 116 ms | 15408 KiB |
| 041.txt | AC | 81 ms | 9416 KiB |
| 042.txt | AC | 74 ms | 9028 KiB |
| 043.txt | AC | 10 ms | 3592 KiB |
| example0.txt | AC | 2 ms | 3608 KiB |
| example1.txt | AC | 2 ms | 3564 KiB |
| example2.txt | AC | 1 ms | 3608 KiB |