Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update santa-banta.cpp #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 66 additions & 43 deletions Medium/Santa Banta/santa-banta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,93 @@ using namespace std;

// } Driver Code Ends

// Constant defining the maximum size of the array
const int maxn=1000001;

// Array for marking prime numbers
int a[maxn+1];

// Vector for storing prime numbers
vector<int> pl={2};

class Solution{
public:
int prime[100001];

bool isPrime(int n)
{
if(n==1)return false;
if(n==2 or n==3)return true;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return false;
// Function to precompute the prime numbers
void precompute(){
// Marking all numbers as prime initially
for(int i=1;i<=maxn;i++)
a[i]=1;

// Marking 0 and 1 as not prime
a[0]=a[1]=0;

// Sieve of Eratosthenes algorithm to mark non-prime numbers
for(int i=2;i*i<=maxn;i++){
if(a[i]==1){
for(int j=i*i;j<=maxn;j+=i){
a[j]=0;
}
}
}

return true;
// Storing all the prime numbers in the vector
for(int i=3;i<=maxn;i++)
if(a[i])
pl.push_back(i);
}

int count= 0;
void precompute(){
for(int i=2;i<=1000000;i++)
{
if(isPrime(i))
{
count++;
prime[count]= i;
// Depth-first search function to find the number of reachable nodes in a graph
int dfs(int i, vector<int> g[], vector<int> &vis){
// Marking the current node as visited
vis[i]=1;

// Counter variable to keep track of the number of reachable nodes
int cnt=1;

// Recursively traversing all the adjacent nodes of the current node
for(auto x:g[i]){
if(!vis[x]){
cnt+=dfs(x, g, vis);
}
}
prime[1]=-1;

// Returning the total number of reachable nodes
return cnt;
}

int dfs(int node,vector<bool> &vis,vector<vector<int>> &g)
// Function to help Santa navigate the given graph
int helpSanta(int n, int m, vector<vector<int>> &g)
{
vis[node]=true;
// Initializing the visited array
vector<int> vis(n+1, 0);

int ans=0;
for(auto it: g[node])
{
if(vis[it]==false)
{
ans+= 1+ dfs(it,vis,g);
}
// Creating an adjacency list for the given graph
vector<int> adj[n + 1];
for(auto i : g){
adj[i[0]].push_back(i[1]);
adj[i[1]].push_back(i[0]);
}

return ans;
}
int helpSanta(int n, int m, vector<vector<int>> &g){
vector<bool> vis(n+1, false);
// Variable to store the largest component size
int lc=0;

int maxi = -1;
for(int i=1;i<=n;i++)
{
if(vis[i]==false)
{
int val =1 + dfs(i,vis,g);
maxi= max(maxi,val);
// Traversing all the nodes from 0 to n and finding the largest component
for(int i = 0; i <= n; i++){
if(!vis[i]){
lc=max(lc,dfs(i, adj, vis));
}
}

if(maxi==-1)return -1;

return prime[maxi];
// Checking if there is only one component in the graph
// If yes, returning -1
if(lc==1)
return -1;

// Returning the prime number at the (largest component size - 1) index
return pl[lc-1];
}
};


//{ Driver Code Starts.

int main(){
Expand All @@ -93,4 +116,4 @@ int main(){



// } Driver Code Ends
// } Driver Code Ends