Swapping Places

Animals are waiting in a line, in a quarantine zone, before they can enter a hunting-free area, where they will find an easier life.

When entering the quarantine zone, animals have to check in with a guard. The guard writes down the animal species, and then the animal is allowed to join the end of the line, in the last position. At the other end of the line, animals have to check out: when the animal in the first position in the line is finally allowed to enter the hunting-free area, another guard writes down the animal species. Thus, each guard maintains a list of animal species, written down in the chronological order in which the animals have checked in or out. A total of N animals, representing S species, have checked in (and, therefore, checked out).

However, animals may enter the waiting line and leave it in different orders. Indeed, some animal species are friends with each other, and thus two animals from such species, if they occupy adjacent places in the line may accept to switch their places.

You have a list of those pairs of animal species that may accept to switch their places when being in adjacent positions in the line: this list contains L pairs. You were handed out the check-in list maintained by the first guard. Depending on which animals decided to switch places, several check-out lists might be possible. Among all those possible lists, which one comes first in alphabetical order?

Input
The input consists of the following lines:

  • Line 1 1 1 contains three space-separated integers S S S, L L L and N N N. S is the number of animal species, L L L is the number of pairs of species that are friends with each other, and N N N is the number of animals that entered the waiting line.
  • Line i + 2 i+2 i+2, for 0 ≤ i < S 0≤i<S 0i<S, contains the name of one of the represented species: this name is made of a single word, with uppercase letters between “A” and “Z”, and contains between 1 and 20 letters.
  • Line i + S + 2 i+S+2 i+S+2, for 0 ≤ i < L 0≤i<L 0i<L, contains two space-separated species names A A A and B B B describing that A A A and B B B are friends with each other.
  • Line S + L + 2 S+L+2 S+L+2 represents the check-in list, and it contains N N N space-separated species names: for all 1 ≤ k ≤ N 1≤k≤N 1kN, the kth word is the name of the species of the animal that entered the line in kth position.

Limits

  • 1 ≤ S ≤ 200 1≤S≤200 1S200;
  • 0 ≤ L ≤ 10000 0≤L≤10000 0L10000;
  • 1 ≤ N ≤ 100000 1≤N≤100000 1N100000.

Output
The output should contain a single line containing N N N words w 0 , … , w N − 1 w_0,…,w_{N−1} w0,,wN1, separated by spaces: the list w 0 , … , w N − 1 w_0,…,w_{N−1} w0,,wN1 must be, among all the possible check-out lists, the one that comes first in alphabetical order.

Example

input
3 2 6
ANTILOPE
CAT
ANT
CAT ANTILOPE
ANTILOPE ANT
ANT CAT CAT ANTILOPE CAT ANT
output
ANT ANTILOPE CAT CAT CAT ANT

Note
Sample Explanation

The six possible orderings at check-out, sorted in (increasing) alphabetical order, are:

ANT ANTILOPE CAT CAT CAT ANT
ANT CAT ANTILOPE CAT CAT ANT
ANT CAT CAT ANTILOPE CAT ANT
ANT CAT CAT CAT ANT ANTILOPE
ANT CAT CAT CAT ANTILOPE ANT
ANTILOPE ANT CAT CAT CAT ANT

如果两个动物之间没有关联或者种类相同则两者的相对位置不变,而拓扑排序正好满足这种性质,如果两个连一条有向边,拓扑排序中二者的相对顺序就不会发生改变,对于每个动物,与后面最近的种类不同且没有关系的动物连一条有向边,然后按字典序优先进行拓扑排序即可。

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 1e5 + 10, M = 205;
string a[M];
int seq[N], deg[N];
map<string, int> rnk;
bool b[M][M];
vi nxt[N];
int rec[M];
int s, l, n;

int main() {
    //std::ios::sync_with_stdio(false);
    s = qr(), l = qr(), n = qr();
    repi(i, 1, s)cin >> a[i];
    sort(a + 1, a + 1 + s);
    repi(i, 1, s)rnk[a[i]] = i;
    repi(i, 1, l) {
        string s1, s2;
        cin >> s1 >> s2;
        b[rnk[s1]][rnk[s2]] = true;
        b[rnk[s2]][rnk[s1]] = true;
    }
    repi(i, 1, n) {
        string x;
        cin >> x, seq[i] = rnk[x];
    }
    repd(i, n, 1) {
        repi(j, 1, s)if (rec[j] && !b[j][seq[i]])nxt[i].pb(rec[j]), deg[rec[j]]++;
        rec[seq[i]] = i;
    }
    priority_queue<pii > q;
    repi(i, 1, n)if (!deg[i])q.push({-seq[i], i});
    vi ans;
    while (!q.empty()) {
        pii t = q.top();
        t.fi = -t.fi;
        q.pop();
        ans.pb(t.fi);
        for (auto it:nxt[t.se])if (!--deg[it])q.push({-seq[it], it});
    }
    for (auto it:ans)cout << a[it] << ' ';
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值