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 11 contains three space-separated integers SS, LL and NN. S is the number of animal species, LL is the number of pairs of species that are friends with each other, and NN is the number of animals that entered the waiting line.
  • Line i+2i+2, for 0i<S0≤i<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+2i+S+2, for 0i<L0≤i<L, contains two space-separated species names AA and BB describing that AA and BB are friends with each other.
  • Line S+L+2S+L+2 represents the check-in list, and it contains NN space-separated species names: for all 1kN1≤k≤N, the kth word is the name of the species of the animal that entered the line in kth position.

Limits

  • 1S2001≤S≤200;
  • 0L100000≤L≤10000;
  • 1N1000001≤N≤100000.

Output
The output should contain a single line containing NN words w0,,wN1w_0,…,w_{N−1}, separated by spaces: the list w0,,wN1w_0,…,w_{N−1} 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章