Uva 13153 & Uvalive 7638 - Number of Connected Components
Problem Statement
Solution Code: Using Disjoint Set [Faster than DFS]
Link: Number of Connected Components(Disjoint Set Solution)
Solution Code: Using DFS
/**
* @Author: Pranta Sarker
*
**/
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 1e6+5;
bitset<high>bs;
vii adj[high] , compo , prime;
int prm[high], plen=0 , visited[high];
void CLR()
{
for(int i=0; i<high; i++)
{
adj[i].clr;
visited[i] = 0;
}
prime.clr;
compo.clr;
}
void sieve()
{
LL i , j;
bs.set();
bs[0] = bs[1] = 0;
for(i=2; i<high; i++)
{
if(bs[i])
{
prm[plen++] = i;
for(j=2*i; j<high; j+=i)
{
bs[j] = 0;
}
}
}
}
void get_Divisor(int n)
{
int x = n;
for(int i=0; prm[i] * prm[i] <= n; i++)
{
if(!(n % prm[i]))
{
while(!(n % prm[i]))
{
n /= prm[i];
}
int y = prm[i];
adj[x].psb(y);
adj[y].psb(x);
}
}
if(n > 1)
{
int y = n;
adj[x].psb(y);
adj[y].psb(x);
}
}
void DFS(int u)
{
visited[u] = 1;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
}
int main()
{
sieve();
int test, tc=0;
sf("%d", &test);
while(test--)
{
CLR();
int i , N , x , j , cnt=0;
sf("%d", &N);
for(i=0; i<N; i++)
{
sf("%d", &x);
if(x == 1) cnt+=1;
if(bs[x]) prime.push_back(x);
else if(bs[x] == 0 && x > 1) compo.push_back(x);
}
int csz = compo.size();
for(i=0; i<csz; i++)
{
get_Divisor(compo[i]);
}
int psz = prime.size();
for(i=0; i<psz; i++)
{
if(!visited[prime[i]])
{
cnt += 1;
DFS(prime[i]);
}
}
for(i=0; i<csz; i++)
{
if(!visited[compo[i]])
{
cnt+=1;
DFS(compo[i]);
}
}
pf("Case %d: %d\n", ++tc , cnt);
}
return 0;
}
Solution Code: Using Disjoint Set [Faster than DFS]
Link: Number of Connected Components(Disjoint Set Solution)
Solution Code: Using DFS
/**
* @Author: Pranta Sarker
*
**/
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 1e6+5;
bitset<high>bs;
vii adj[high] , compo , prime;
int prm[high], plen=0 , visited[high];
void CLR()
{
for(int i=0; i<high; i++)
{
adj[i].clr;
visited[i] = 0;
}
prime.clr;
compo.clr;
}
void sieve()
{
LL i , j;
bs.set();
bs[0] = bs[1] = 0;
for(i=2; i<high; i++)
{
if(bs[i])
{
prm[plen++] = i;
for(j=2*i; j<high; j+=i)
{
bs[j] = 0;
}
}
}
}
void get_Divisor(int n)
{
int x = n;
for(int i=0; prm[i] * prm[i] <= n; i++)
{
if(!(n % prm[i]))
{
while(!(n % prm[i]))
{
n /= prm[i];
}
int y = prm[i];
adj[x].psb(y);
adj[y].psb(x);
}
}
if(n > 1)
{
int y = n;
adj[x].psb(y);
adj[y].psb(x);
}
}
void DFS(int u)
{
visited[u] = 1;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
}
int main()
{
sieve();
int test, tc=0;
sf("%d", &test);
while(test--)
{
CLR();
int i , N , x , j , cnt=0;
sf("%d", &N);
for(i=0; i<N; i++)
{
sf("%d", &x);
if(x == 1) cnt+=1;
if(bs[x]) prime.push_back(x);
else if(bs[x] == 0 && x > 1) compo.push_back(x);
}
int csz = compo.size();
for(i=0; i<csz; i++)
{
get_Divisor(compo[i]);
}
int psz = prime.size();
for(i=0; i<psz; i++)
{
if(!visited[prime[i]])
{
cnt += 1;
DFS(prime[i]);
}
}
for(i=0; i<csz; i++)
{
if(!visited[compo[i]])
{
cnt+=1;
DFS(compo[i]);
}
}
pf("Case %d: %d\n", ++tc , cnt);
}
return 0;
}
Comments
Post a Comment