/***
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[100] ;
int visited[1001] ;
vector<int>data ;
int point,tree ;
map<char,int>mp ;
void BFS(int src)
{
int cnt =0 ;
queue<int>q ;
q.push(src) ;
visited[src] =1 ;
cnt++ ;
int u ,v ,i;
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) ;
cnt++ ;
}
}
}
if(cnt==1)
point++ ;
else if(cnt>1)
tree++ ;
}
int main()
{
int test,i,u,v ;
sc("%d",&test) ;
while(test--)
{
string str ;
int k =1 ;
point =0 ;
tree =0 ;
while(cin>>str)
{
if(str[0]=='*')
break ;
if(mp[str[1]]==0)
{
mp[str[1]] =k ;
k++ ;
}
if(mp[str[3]]==0)
{
mp[str[3]] =k ;
k++ ;
}
int u = mp[str[1]] ;
int v = mp[str[3]] ;
G[u].pb(v) ;
G[v].pb(u) ;
}
cin>>str ;
for(i=0 ; i<str.size() ; i++)
{
if(str[i]>='A' && str[i]<='Z')
{
if(mp[str[i]]==0)
{
mp[str[i]] =k ;
k++ ;
}
u = mp[str[i]] ;
data.pb(u) ;
}
}
sort(data.begin(),data.end()) ;
for(i=0 ; i<data.size() ; i++)
{
u = data[i] ;
if(!visited[u])
{
BFS(u) ;
}
}
pf("There are %d tree(s) and %d acorn(s).\n",tree,point) ;
data.clear() ;
mp.clear() ;
for(i=1 ; i<k ; i++)
G[i].clear() ;
memset(visited,0,sizeof(visited)) ;
}
return 0;
}
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[100] ;
int visited[1001] ;
vector<int>data ;
int point,tree ;
map<char,int>mp ;
void BFS(int src)
{
int cnt =0 ;
queue<int>q ;
q.push(src) ;
visited[src] =1 ;
cnt++ ;
int u ,v ,i;
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) ;
cnt++ ;
}
}
}
if(cnt==1)
point++ ;
else if(cnt>1)
tree++ ;
}
int main()
{
int test,i,u,v ;
sc("%d",&test) ;
while(test--)
{
string str ;
int k =1 ;
point =0 ;
tree =0 ;
while(cin>>str)
{
if(str[0]=='*')
break ;
if(mp[str[1]]==0)
{
mp[str[1]] =k ;
k++ ;
}
if(mp[str[3]]==0)
{
mp[str[3]] =k ;
k++ ;
}
int u = mp[str[1]] ;
int v = mp[str[3]] ;
G[u].pb(v) ;
G[v].pb(u) ;
}
cin>>str ;
for(i=0 ; i<str.size() ; i++)
{
if(str[i]>='A' && str[i]<='Z')
{
if(mp[str[i]]==0)
{
mp[str[i]] =k ;
k++ ;
}
u = mp[str[i]] ;
data.pb(u) ;
}
}
sort(data.begin(),data.end()) ;
for(i=0 ; i<data.size() ; i++)
{
u = data[i] ;
if(!visited[u])
{
BFS(u) ;
}
}
pf("There are %d tree(s) and %d acorn(s).\n",tree,point) ;
data.clear() ;
mp.clear() ;
for(i=1 ; i<k ; i++)
G[i].clear() ;
memset(visited,0,sizeof(visited)) ;
}
return 0;
}
No comments:
Post a Comment