Advertisement
pb_jiang

ABC282D WA

Dec 18th, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  5. template <typename... Args> void logger(string vars, Args &&... values)
  6. {
  7.     cerr << vars << " = ";
  8.     string delim = "";
  9.     (..., (cerr << delim << values, delim = ", "));
  10.     cerr << endl;
  11. }
  12.  
  13. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  14. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  15. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  16. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  17.  
  18. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  19. using vi = vector<int>;
  20. using ll = long long;
  21. using pii = pair<int, int>;
  22.  
  23. vi g[200003];
  24. int color[200003];
  25. int vis[200003];
  26. int cp_cnt[200003];
  27.  
  28. int coloring(int u, int c, int cp_id)
  29. {
  30.     color[u] = c;
  31.     vis[u] = 1;
  32.     cp_cnt[u] = cp_id;
  33.     int ret = 1;
  34.     for (auto v : g[u]) {
  35.         if (vis[v]) {
  36.             if (c * -1 != color[v]) {
  37.                 return 0;
  38.             }
  39.         } else {
  40.             ret = ret & coloring(v, c * -1, cp_id);
  41.         }
  42.     }
  43.     return ret;
  44. }
  45.  
  46. int main(int argc, char **argv)
  47. {
  48.     int n, m;
  49.     cin >> n >> m;
  50.     int u, v;
  51.     for (int i = 0; i < m; ++i)
  52.         cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
  53.  
  54.     int cp_id = 1;
  55.     for (int i = 1; i <= n; ++i) {
  56.         if (vis[i] == 0) {
  57.             int bi_par = coloring(i, 1, cp_id);
  58.             if (bi_par == 0) {
  59.                 cout << 0 << endl;
  60.                 return 0;
  61.             }
  62.         }
  63.     }
  64.  
  65.     int cnt[2] = {0, 0};
  66.     map<int, array<int, 2>> cp_map;
  67.     for (int i = 1; i <= n; ++i) {
  68.         cnt[(color[i] + 1) / 2]++;
  69.         cp_map[cp_cnt[i]][(color[i] + 1) / 2]++;
  70.     }
  71.     ll ans = 0;
  72.     for (int i = 1; i <= n; ++i) {
  73.         int c = (color[i] + 1) / 2;
  74.         // dbg(c, cnt[1 - c]);
  75.         // ans += cnt[1 - c] - g[i].size();
  76.         ans += cp_map[cp_cnt[i]][1 - c] - g[i].size();
  77.         ans += n - cp_map[cp_cnt[i]][0] - cp_map[cp_cnt[i]][1];
  78.     }
  79.     ans /= 2;
  80.     cout << ans << endl;
  81.     return 0;
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement