天天看点

J. Kitchen Plates

https://codeforc.es/gym/102219/problem/J

思路:连边,拓扑排序;

#include<algorithm>
#include<set>
#include<cmath>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<map>
#include<stack>
#include<bits/stdc++.h>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-8
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

const int maxn=1e6+9;
const int mod=1e9+7;

inline ll read()
{
    ll f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9')
    {
        if(ss=='-')f=-1;ss=getchar();
    }
    while(ss>='0'&&ss<='9')
    {
        x=x*10+ss-'0';ss=getchar();
    }    return f*x;
}

ll power(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=ans*x;
        x=x*x;
        n>>=1;
    }
    return ans;
}

queue<int>q;
vector<int>g[maxn],ans;
int d[maxn];
int main()
{
    //FAST_IO;

    mem(d,0);
    for(int i=0;i<5;i++)
    {
        string s;
        cin>>s;
        if(s[1]=='>')
        {
            int u=s[0]-'A';
            int v=s[2]-'A';
            g[u].pb(v);
            d[v]++;
        }
        else
        {
            int u=s[0]-'A';
            int v=s[2]-'A';
            g[v].pb(u);
            d[u]++;
        }
    }
    for(int i=0;i<5;i++) if(d[i]==0) q.push(i);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        ans.pb(u);
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            d[v]--;
            if(d[v]==0) q.push(v);
        }
    }
    if(ans.size()!=5) cout<<"impossible"<<endl;
    else
    {
        for(int i=4;i>=0;i--) printf("%c",ans[i]+'A');
        cout<<endl;
    }
    return 0;
}