Re: 280 - Vertex
Posted: Fri Nov 21, 2014 10:08 pm
Don't read from a file.
Code: Select all
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
typedef struct linked_list
{
int data;
struct linked_list *add;
}node;
node *head[110];
void create(node **header,int a,int b)
{
if((*header)==NULL)
{
*header=(node *)malloc(sizeof(node));
(*header)->data=a;
(*header)->add=(node *)malloc(sizeof(node));
(*header)->add->data=b;
(*header)->add->add=NULL;
return;
}
node *current;
current=*header;
while(current->add!=NULL)
{
current=current->add;
if(current->data == b)
return;
}
current->add=(node *)malloc(sizeof(node));
current->add->data=b;
current->add->add=NULL;
}
void find_vertex(int v,int n)
{
bool visited[110];
int i,j=n;
queue<int> q;
node *current;
for(i=1;i<=n;i++)
visited[i]=false;
q.push(v);
// visited[v]=true;
while(!q.empty())
{
v=q.front();
q.pop();
if(head[v]==NULL)
break;
for(current=(head[v])->add;current!=NULL;current=current->add)
{
i=current->data;
if(!visited[i])
{
// printf("hii");
n--;
visited[i]=true;
q.push(i);
}
}
}
printf("%d",n);
for(i=1;i<=j;i++)
if(!visited[i])
printf(" %d",i);
printf("\n");
}
int main()
{
// node *head[110];
int n,m,a,b,v,i;
while(scanf("%d",&n)!=EOF && n)
{
for(i=1;i<=n;i++)
head[i]=NULL;
while(scanf("%d",&a)!=EOF && a)
{
while(scanf("%d",&b)!=EOF && b)
{
create(&head[a],a,b);
}
}
/* node *current;
for(i=1;i<=n;i++)
{
if(head[i]==NULL)
{
printf("%d no edges\n",i);
continue;
}
current=head[i];
while(current!=NULL)
{
printf("%d ",current->data);
current=current->add;
}
printf("\n");
}*/
scanf("%d",&m);
while(m--)
{
scanf("%d",&v);
find_vertex(v,n);
}
}
return 0;
}
Code: Select all
3
2 3 0
3 1 2 0
0
3 1 2 3
8
2 3 4 8 0
3 1 6 0
4 3 5 7 0
5 2 3 6 8 0
6 1 3 4 5 0
7 1 3 5 6 0
8 2 3 4 5 0
0
8 1 2 3 4 5 6 7 8
4
1 2 4 0
2 3 0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