Gym - 102219J Kitchen Plates拓扑排序

题意:给你五行比较,求输出从小到大的排序

思路:裸的拓扑排序,正常弄就行(其实拓扑就讲究入度为0就存进去的问题)

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define N 200005
#define INF 0x3f3f3f3f
#define M 1000000007
#define fori for(i=0;i<n;i++)
#define fori1 for(i=1;i<=n;i++)
ll a[N] = { 0 };
ll degree[N] = { 0 };
vector <ll> spt;//从小到大排序好
vector <ll> G[5];//制造邻接表
ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
void TopSort(vector <ll> G[], ll degree[]) 
{
    ll i, j, k;
    queue <ll> Q;
    for (i = 0; i < 5; i++)
        if (!degree[i])
            Q.push(i);
    while (!Q.empty()) {
        ll u = Q.front();
        Q.pop();
        spt.push_back(u);//存入vector中
        for (i = 0; i < G[u].size(); i++) {
            degree[G[u][i]]--;
            if (!degree[G[u][i]])//入度为0的话
                Q.push(G[u][i]);
        }
    }
}
int main()
{

    ll i, j, k;
    ll n, t;
    ll sum = 0, ret = 0;
    ll ans = 0;
    ll flag = 1;
    string s[5];
    for (i = 0; i < 5; i++)
    {
        cin >> s[i];
        ll x = s[i][0] - 'A', y = s[i][2] - 'A';
        if (s[i][1] != '<')
            swap(x, y);
        degree[y]++;
        G[x].push_back(y);//创建邻接表
    }
    TopSort(G, degree);
    if (spt.size() < 5)
        cout << "impossible" << endl;
    else
    {
        for (i = 0; i < spt.size(); i++)
            printf("%c", spt[i] + 'A');
        cout << endl;
    }



}

 

posted @ 2020-04-04 11:41  ch_hui  阅读(146)  评论(0编辑  收藏  举报