UVA 280 Solution- Vertex
UVA 280 Solution- Vertex
uva id: shoaib05
Accepted Time: 0.156
#include<iostream>
#include<string>
#include<string.h>
#include<queue>
using namespace std;
int table[105][105];
bool reachable[105];
int bfs(int x)
{
queue<int> q;
int i,top,vis=0;
q.push(x);
while(!q.empty())
{
top=q.front();
q.pop();
for(i=1;i<=table[top][0];i++)
{
x=table[top][i];
if(!reachable[x])
{
vis++;
reachable[x]=true;
q.push(x);
}
}
}
return vis;
}
int main()
{
freopen("file.txt","r",stdin);
int n,x,y,i,j,m;
while(cin>>n)
{
if(n==0)
break;
for(i=1;i<=n;i++)
table[i][0]=0;
while(cin>>x)
{
if(x==0)
break;
while(cin>>y)
{
if(y==0)
break;
table[x][0]+=1;
table[x][table[x][0]]=y;
}
}
cin>>m;
for(i=1;i<=m;i++)
{
cin>>y;
for(j=1;j<=n;j++)
{
reachable[j]=false;
}
y=n-bfs(y);
cout<<y;
if(y!=0)
{
for(j=1;j<=n;j++)
{
if(!reachable[j])
cout<<" "<<j;
}
}
cout<<endl;
}
}
return 0;
}
#include<string>
#include<string.h>
#include<queue>
using namespace std;
int table[105][105];
bool reachable[105];
int bfs(int x)
{
queue<int> q;
int i,top,vis=0;
q.push(x);
while(!q.empty())
{
top=q.front();
q.pop();
for(i=1;i<=table[top][0];i++)
{
x=table[top][i];
if(!reachable[x])
{
vis++;
reachable[x]=true;
q.push(x);
}
}
}
return vis;
}
int main()
{
freopen("file.txt","r",stdin);
int n,x,y,i,j,m;
while(cin>>n)
{
if(n==0)
break;
for(i=1;i<=n;i++)
table[i][0]=0;
while(cin>>x)
{
if(x==0)
break;
while(cin>>y)
{
if(y==0)
break;
table[x][0]+=1;
table[x][table[x][0]]=y;
}
}
cin>>m;
for(i=1;i<=m;i++)
{
cin>>y;
for(j=1;j<=n;j++)
{
reachable[j]=false;
}
y=n-bfs(y);
cout<<y;
if(y!=0)
{
for(j=1;j<=n;j++)
{
if(!reachable[j])
cout<<" "<<j;
}
}
cout<<endl;
}
}
return 0;
}
Comments
Post a Comment