当前位置: 首页 > 工具软件 > Places > 使用案例 >

Swapping Places

越霖
2023-12-01

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;
}
 类似资料:

相关阅读

相关文章

相关问答