树上逃离-最短路
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, k, p;
int h[N], e[N], ne[N], idx;
int b[N];
string path[N], res[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void bfs()
{
queue<int> q;
q.push(0);
path[0] = "0";
while(q.size())
{
int t = q.front();
q.pop();
int idx = 0;
for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if(!b[j])
{
q.push(j);
path[j] = path[t] + "->" + to_string(j);
if(h[j] == -1)
{
idx = 1;
res[p ++] = path[j];
}
}
}
if(idx)
{
sort(res, res + p);
cout << res[0] << endl;
return;
}
}
cout << "NULL" << endl;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while(m --)
{
int a, b;
cin >> a >> b;
add(a, b);
}
cin >> k;
while(k --)
{
int x;
cin >> x;
b[x] = 1;
}
bfs();
return 0;
}