Skip to content

Commit

Permalink
Time: 121 ms (89.08%) | Memory: 56.4 MB (28.15%) - LeetSync
Browse files Browse the repository at this point in the history
  • Loading branch information
pandarudra committed Jan 30, 2025
1 parent 10647b3 commit cefbfc6
Showing 1 changed file with 55 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,86 +1,71 @@
#include <vector>
#include <queue>
using namespace std;

#define INF 0x3f3f3f3f
class Solution {
private:
bool isBipartite(int src, vector<vector<int>>& graph, vector<int>& component) {
queue<int> q;
q.push(src);
component.push_back(src);
vector<int> color(graph.size(), -1);
color[src] = 0;

while (!q.empty()) {
int node = q.front();
q.pop();

for (auto &neighbor : graph[node]) {
if (color[neighbor] == -1) {
color[neighbor] = 1 - color[node];
q.push(neighbor);
component.push_back(neighbor);
} else if (color[neighbor] == color[node]) {
return false; // Not bipartite
bool isBipartite(vector<vector<int>>& graph , vector<int>& connected , int src){
int n = graph.size() ;
vector<int> color(n , -1) ;
queue<int> q ;
color[src] = 0 ;
q.push(src) ;
connected.push_back(src) ;
while(!q.empty()){
int node = q.front() ;
q.pop() ;
for(auto &edge : graph[node]){
if(color[edge] == -1){
color[edge] = 1 - color[node] ;
connected.push_back(edge) ;
q.push(edge) ;
}else if(color[edge] == color[node]){
return false ;
}
}
}
return true;
return true ;
}

int bfsMaxLevel(int src, vector<vector<int>>& graph) {
queue<int> q;
q.push(src);
vector<int> vis(graph.size(), 0);
vis[src] = 1;
int level = 0;

while (!q.empty()) {
int sz = q.size();
while (sz--) {
int node = q.front();
q.pop();
for (auto &neighbor : graph[node]) {
if (!vis[neighbor]) {
vis[neighbor] = 1;
q.push(neighbor);
}
int bfs(int src , vector<vector<int>>& graph){
queue<int> q ;
q.push(src) ;
int dist = 0 ;
vector<int> d (graph.size() , -1) ;
d[src] = 0 ;
while(!q.empty()){
int node = q.front() ;
q.pop();
for(auto &edge : graph[node]){
if(d[edge] == -1){
d[edge] = d[node] + 1 ;
dist = max(dist , d[edge]) ;
q.push(edge) ;
}
}
level++;
}
return level;
return dist + 1 ;
}

public:
int magnificentSets(int n, vector<vector<int>>& edges) {
vector<vector<int>> graph(n);
for (auto &edge : edges) {
graph[edge[0] - 1].push_back(edge[1] - 1);
graph[edge[1] - 1].push_back(edge[0] - 1);
vector<vector<int>> graph(n) ;
for(auto &edge : edges){
graph[edge[0] - 1].push_back(edge[1] - 1) ;
graph[edge[1] - 1].push_back(edge[0] - 1) ;
}

vector<int> visited(n, 0);
int result = 0;

for (int i = 0; i < n; i++) {
if (!visited[i]) {
vector<int> component;
if (!isBipartite(i, graph, component)) {
return -1; // Not bipartite, return immediately
}

int maxLevel = 0;
for (int node : component) {
maxLevel = max(maxLevel, bfsMaxLevel(node, graph));
}
result += maxLevel;

for (int node : component) {
visited[node] = 1;
}
vector<int> vis(n , 0) ;
int res = 0 ;
for(int i = 0 ; i < n ; i++){
if(vis[i])continue ;
vector<int> connected ;
if(!isBipartite(graph , connected , i)){
return -1 ;
}
int mx = 0 ;
for(auto &node : connected){
mx = max(mx , bfs(node , graph)) ;
}
res += mx ;
for(auto &node : connected){
vis[node] = 1 ;
}
}
return result;
return res ;
}
};
};

0 comments on commit cefbfc6

Please sign in to comment.