-
Notifications
You must be signed in to change notification settings - Fork 0
/
280 - Vertex.cpp
53 lines (50 loc) · 1.09 KB
/
280 - Vertex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
#include<iostream>
#define oo 0xffff
using namespace std;
int main()
{
int n,u,v,m;
int i,j,k;
int graph[101][101];
while(cin>>n)
{
if(!n)
break;
memset(graph,0,sizeof(graph));
while(cin>>u)
{
if(!u)
break;
while(cin>>v)
{
if(!v)
break;
graph[u][v]=1;
}
}
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(graph[i][j]==0)
graph[i][j]=oo;
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]);
cin>>m;
while(m--)
{
int x;
cin>>x;
int Ans[101], At=0;
for(i=1; i<=n; i++)
if(graph[x][i] == oo)
Ans[At++] = i;
printf("%d", At);
for(i=0; i<At; i++)
printf(" %d", Ans[i]);
puts("");
}
}
return 0;
}