Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
- template <typename... Args> void logger(string vars, Args &&... values)
- {
- cerr << vars << " = ";
- string delim = "";
- (..., (cerr << delim << values, delim = ", "));
- cerr << endl;
- }
- template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
- template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
- template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
- template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using vi = vector<int>;
- using ll = long long;
- using pii = pair<int, int>;
- vi g[200003];
- int color[200003];
- int vis[200003];
- int cp_cnt[200003];
- int coloring(int u, int c, int cp_id)
- {
- color[u] = c;
- vis[u] = 1;
- cp_cnt[u] = cp_id;
- int ret = 1;
- for (auto v : g[u]) {
- if (vis[v]) {
- if (c * -1 != color[v]) {
- return 0;
- }
- } else {
- ret = ret & coloring(v, c * -1, cp_id);
- }
- }
- return ret;
- }
- int main(int argc, char **argv)
- {
- int n, m;
- cin >> n >> m;
- int u, v;
- for (int i = 0; i < m; ++i)
- cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
- int cp_id = 1;
- for (int i = 1; i <= n; ++i) {
- if (vis[i] == 0) {
- int bi_par = coloring(i, 1, cp_id);
- if (bi_par == 0) {
- cout << 0 << endl;
- return 0;
- }
- }
- }
- int cnt[2] = {0, 0};
- map<int, array<int, 2>> cp_map;
- for (int i = 1; i <= n; ++i) {
- cnt[(color[i] + 1) / 2]++;
- cp_map[cp_cnt[i]][(color[i] + 1) / 2]++;
- }
- ll ans = 0;
- for (int i = 1; i <= n; ++i) {
- int c = (color[i] + 1) / 2;
- // dbg(c, cnt[1 - c]);
- // ans += cnt[1 - c] - g[i].size();
- ans += cp_map[cp_cnt[i]][1 - c] - g[i].size();
- ans += n - cp_map[cp_cnt[i]][0] - cp_map[cp_cnt[i]][1];
- }
- ans /= 2;
- cout << ans << endl;
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement