Advertisement
pb_jiang

LC2493 AC

Dec 4th, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. class Solution {
  2.     vector<int> vis;
  3.     vector<int> color;
  4.     // int nn;
  5.     vector<int> g[501];
  6.     bool check_odd_ring(int u, int c) {
  7.         if (color[u] && color[u] != c) {
  8.             return false;
  9.         }
  10.         color[u] = c;
  11.         vis[u] = 1;
  12.         for (auto v: g[u]) {
  13.             if (vis[v] == 0) {
  14.                 if (check_odd_ring(v, c * -1) == false) return false;
  15.             } else if (color[v] != c * -1) {
  16.                 return false;
  17.             }
  18.         }
  19.         return true;
  20.     }
  21.     int bfs(int u) {
  22.         // unordered_set<int> vv;
  23.         using pii = pair<int, int>;
  24.         queue<pii> q;
  25.         q.push({u, 1});
  26.         int h = 1;
  27.         while(q.empty() == false) {
  28.             auto x = q.front(); q.pop();
  29.             if (vis[x.first]) continue;
  30.             h = max(x.second, h);
  31.             vis[x.first] = 1;
  32.             for (auto v: g[x.first]) {
  33.                 if (vis[v] == 0) {
  34.                     q.push({v, x.second + 1});
  35.                 }
  36.             }
  37.         }
  38.         // cout << "bfs on: " << u << " ans: " << h << endl;
  39.         return h;      
  40.     }
  41.     int height(int u) {
  42.         using pii = pair<int, int>;
  43.         queue<pii> q;
  44.         q.push({u, 1});
  45.         vector<int> nodes;
  46.         int h = 1;
  47.         while(q.empty() == false) {
  48.             auto x = q.front(); q.pop();
  49.             if (vis[x.first]) continue;
  50.             h = max(x.second, h);
  51.             nodes.push_back(x.first);
  52.             vis[x.first] = 1;
  53.             for (auto v: g[x.first]) {
  54.                 if (vis[v] == 0) {
  55.                     q.push({v, x.second + 1});
  56.                 }
  57.             }
  58.         }
  59.         for (auto x: nodes) {
  60.             for (auto y: nodes) {
  61.                 vis[y] = 0;
  62.             }
  63.             h = max(h, bfs(x));
  64.         }
  65.         // cout << "h: " << h << " size: " << nodes.size() << endl;
  66.         return h;
  67.     }
  68. public:
  69.     int magnificentSets(int n, vector<vector<int>>& edges) {
  70.         for (const auto& e: edges) {
  71.             auto u = e[0], v = e[1];
  72.             g[u].push_back(v);
  73.             g[v].push_back(u);
  74.         }
  75.         vis = vector<int>(n + 1);
  76.         color = vector<int>(n + 1);
  77.         for (int i = 1; i <= n; ++i) {
  78.             if (vis[i] == 0 && check_odd_ring(i, 1) == false)
  79.                 return -1;
  80.         }
  81.         // nn = n;
  82.         int ans = 0;
  83.         vis = vector<int>(n + 1);
  84.         for (int i = 1; i <= n; ++i) {
  85.             if (vis[i] == 0) {
  86.                 ans += height(i);
  87.             }
  88.         }
  89.         return ans;
  90.     }
  91. };
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement