import java util ArrayList import java util Arrays import java util Sc

  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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.lang.Math;
class point {
int mark=0; //метка занятости
int [] range=new int [1000]; //расстояние //может возникнуть ошибка
int cap=0; //число инцидентных ребер
ArrayList<Integer> list = new ArrayList<Integer>();
}
//кольцевой буфер
class Queue {
int size,head,tail;
int[] data;
Queue(int size) {
data = new int [this.size = size];
}
void add(int value) {
if (++tail == size)
tail = 0;
data[tail] = value;
}
int extract() {
if (++head == size)
head = 0;
return data[head];
}
}
public class EqDist {
public static void main(String[] arg) {
Scanner sc = new Scanner(System.in);
int i=0,j=0,n=sc.nextInt(),m=sc.nextInt(),first,second,range=1,prints=0,error=0;
point [] graph=new point[n];
int [] unsorted=new int [n];
for (i=0;i<n;i++) {
graph[i]=new point();
graph[i].mark=0;
unsorted[i]=i;
}
Queue q=new Queue(n+1);
//список инцидентности
for(i=0;i<m;i++){
first=sc.nextInt(); second=sc.nextInt();
graph[first].cap++;
graph[first].list.add(second);
graph[second].cap++;
graph[second].list.add(first);
}
//опорные вершины
int k=sc.nextInt();
int [] ref=new int[k];
for(i=0;i<k;i++)
ref[i]=sc.nextInt();
graph[ref[0]].mark=1;
graph[ref[0]].range[0]=-2;
bfs1(graph,q,ref[0],range,0);
for(i=1;i<k;i++) {
if(graph[ref[i]].range[0]==-2) {
error = 1;
break;
}
graph[ref[i]].range[i]=-2;
bfs1(graph,q,ref[i],range,i);
}
/*if(error==0)
for(i=0;i<n;i++) {
if (graph[i].range != -1) {
System.out.print(i+" ");
prints++;
}
}*/
if(error==0)
for(i=0;i<n;i++){
range=graph[i].range[0];
if(range==0)
continue;
for(j=0;j<k;j++) {
if(range==graph[i].range[j])
continue;
else {
break;
}
}
if(j==k) {
prints++;
System.out.print(i + " ");
}
}
if(prints==0)
System.out.print("-");
}
public static void bfs1(point [] graph, Queue q,int Vertex,int range,int ArrRange) {
int i, v, size;
for (i = 0; i < graph[Vertex].cap; i++) {
v=graph[Vertex].list.get(i);
if(graph[v].range[ArrRange]==0 || graph[v].range[ArrRange]>range) {
graph[v].range[ArrRange] = range;
q.add(v);
graph[v].mark = 1;
}
}
while (q.head != q.tail) {
v=q.extract();
range=graph[v].range[ArrRange]+1;
bfs1(graph, q, v, range,ArrRange);
}
}
}