/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
vector<int> G[50001] ;
queue<int> q ;
int visited[500001] ;
void reset(int n)
{
int i ;
for(i=1 ; i<=n ; i++)
{
G[i].clear() ;
visited[i] =0 ;
}
}
void BFS(int src)
{
int u,v,i ;
visited[src] =1 ;
q.push(src) ;
while(!q.empty())
{
u = q.front() ;
q.pop() ;
for(i=0 ; i<G[u].size() ; i++)
{
v = G[u][i] ;
if(!visited[v])
{
visited[v] =1 ;
q.push(v) ;
}
}
}
}
int main()
{
int cnt =1 ;
int u,v,n,m ,p=0,i;
while(sc("%d %d",&n,&m) &&n &&m)
{
reset(n) ;
for(i=1 ; i<=m ; i++)
{
sc("%d %d",&u,&v) ;
G[u].pb(v) ;
G[v].pb(u) ;
}
p =0 ;
for(i=1 ; i<=n; i++)
{
if(!visited[i])
{
BFS(i) ;
p++ ;
}
}
pf("Case %d: %d\n",cnt++,p) ;
}
return 0;
}
/*
3 2
1 2
2 3
*/
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
vector<int> G[50001] ;
queue<int> q ;
int visited[500001] ;
void reset(int n)
{
int i ;
for(i=1 ; i<=n ; i++)
{
G[i].clear() ;
visited[i] =0 ;
}
}
void BFS(int src)
{
int u,v,i ;
visited[src] =1 ;
q.push(src) ;
while(!q.empty())
{
u = q.front() ;
q.pop() ;
for(i=0 ; i<G[u].size() ; i++)
{
v = G[u][i] ;
if(!visited[v])
{
visited[v] =1 ;
q.push(v) ;
}
}
}
}
int main()
{
int cnt =1 ;
int u,v,n,m ,p=0,i;
while(sc("%d %d",&n,&m) &&n &&m)
{
reset(n) ;
for(i=1 ; i<=m ; i++)
{
sc("%d %d",&u,&v) ;
G[u].pb(v) ;
G[v].pb(u) ;
}
p =0 ;
for(i=1 ; i<=n; i++)
{
if(!visited[i])
{
BFS(i) ;
p++ ;
}
}
pf("Case %d: %d\n",cnt++,p) ;
}
return 0;
}
/*
3 2
1 2
2 3
*/
No comments:
Post a Comment