标签:int cto friend i++ private parent solution vector style
1. FriendCycle
class Solution { public: int findCircleNum(vector<vector<int>>& M) { int len = M.size(); if (len == 0) return 0; vector<int> parent(len, 0); for (int i=0; i<len; i++) { parent[i] = i; } int count = len; for (int i=0; i<len; i++) for (int j=i+1; j<len; j++) { if (M[i][j] == 1) { int parent1 = findParent(i, parent); int parent2 = findParent(j, parent); if (parent1 != parent2) { parent[parent2] = parent1; count--; } } } return count; } private: int findParent(int pos, vector<int> &parent){ if (parent[pos] == pos) return pos; int root = findParent(parent[pos], parent); parent[pos] = root; return root; } };
标签:int cto friend i++ private parent solution vector style
原文地址:https://www.cnblogs.com/cookcoder-mr/p/11080041.html