1242
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <hash_map>
 
using namespace std;
 
// Datatypes
typedef vector<int> vi; 
typedef vector<string> vs;
typedef vector<vi> vvi; 
typedef pair<int,int> ii; 
typedef long long ll;
typedef long double ld;
 
// Define
#define sz(a)             int((a).size()) 
#define pb                push_back 
#define all(c)            (c).begin(),(c).end() 
#define present(c,x)      ((c).find(x) != (c).end()) 
#define cpresent(c,x)     (find(all(c),x) != (c).end()) 
#define FOR(_i,_n)        for(int (_i) = 0; (_i) < (_n); (_i)++)
#define mp                make_pair
 
// Constants
const double eps = 1e-8; 
const double PI = 3.1415926535897932384626433832795;
 
#define INPUT_FILE "xo.in"
#define OUTPUT_FILE "xo.out"
 
#define WHITE 0
#define GREY  1
#define BLACK 2
 
vector< vector<int> > g;
vector< vector<int> > gt;
vector< int > color;
vector< int > vset;
set< int > res;
 
void dfs1(int v)
{
    color[v] = BLACK;
    res.insert(v);
 
    for(int i = 0; i < sz(g[v]); i++)
    {
        if(color[g[v][i]] == WHITE )
        {
            dfs1(g[v][i]);
        } 
    } 
}
 
void dfs2(int v)
{
    color[v] = BLACK;
    res.insert(v);
 
    for(int i = 0; i < sz(gt[v]); i++)
    {
        if(color[gt[v][i]] == WHITE )
        {
            dfs2(gt[v][i]);
        } 
    } 
}
 
int main(void)
{
#ifdef ALEX
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#else
    //freopen(INPUT_FILE, "r", stdin);
    //freopen(OUTPUT_FILE, "w", stdout);
#endif
 
    int N;
    string s;
    istringstream instream;
 
    cin >> N;
    g.resize(N);
    gt.resize(N);
    color.resize(N);
 
    getline(cin, s);
    for(;;)
    {
        int u,v;
        getline(cin, s);
 
        if(s == "BLOOD") break;
        instream.clear();     
        instream.str(s);
        instream >> u >> v;
 
        g[v-1].pb(u-1);
        gt[u-1].pb(v-1);
    }
 
    while(getline(cin, s))
    {
        int v;
        instream.clear();     
        instream.str(s);
        instream >> v;
        vset.pb(v-1);
    }
 
    for(int i = 0; i < sz(vset); i++)
    {
        dfs1(vset[i]);
    }
 
    color.clear();
    color.resize(N);
    for(int i = 0; i < sz(vset); i++)
    {
        dfs2(vset[i]);
    }
 
    for(int i = 0; i < N; i++)
    {
        if(res.find(i) == res.end()) cout << i+1 << " ";
    }
 
    if(sz(res) == N) cout << 0;
 
    return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.