• UVA 12263 Rankings(拓扑排序)


    给出一个n个数的序列1,然后有m个改动(a, b),在序列2中a跟b在序列中的相对顺序改变。求符合题意的序列2。

    题中说道如果一个数的位置不确定,则输出‘?' ,仔细想想,这种情况是不会存在的,因为在给定的序列1中,所有数都会有相对顺序,因此无论怎么修改数对的相对顺序,结果总是确定的。

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<fstream>
    #include<sstream>
    #include<bitset>
    #include<vector>
    #include<string>
    #include<cstdio>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #define FF(i, a, b) for(int i=a; i<b; i++)
    #define FD(i, a, b) for(int i=a; i>=b; i--)
    #define REP(i, n) for(int i=0; i<n; i++)
    #define CLR(a, b) memset(a, b, sizeof(a))
    #define debug puts("**debug**")
    #define LL long long
    #define PB push_back
    #define eps 1e-10
    using namespace std;
    
    const int maxn = 555;
    int T, n, m, rank1[maxn], rank2[maxn], pos[maxn], in[maxn], g[maxn][maxn];
    
    bool topo()
    {
        int tot = 0;
        queue<int> q;
        REP(i, n) if(in[i] == 0) q.push(i);
        while(!q.empty())
        {
            int u = q.front(); q.pop();
            rank2[tot++] = u;
            REP(v, n) if(g[u][v])
            {
                in[v]--;
                if(in[v] == 0) q.push(v);
            }
        }
        return tot == n;
    }
    
    int main()
    {
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d", &n);
            int a, b;
            REP(i, n)
            {
                scanf("%d", &rank1[i]);rank1[i]--;
                pos[rank1[i]] = i;
            }
            CLR(g, 0); CLR(in, 0);
            REP(i, n) FF(j, i+1, n) g[rank1[i]][rank1[j]] = 1, in[rank1[j]]++;
            scanf("%d", &m);
            while(m--)
            {
                scanf("%d%d", &a, &b);a--;b--;
                swap(g[a][b], g[b][a]);
                if(pos[a] < pos[b]) in[a]++, in[b]--;
                else in[a]--, in[b]++;
            }
            if(topo()) REP(i, n) printf("%d%c", rank2[i]+1, i == n-1 ? '
    ' : ' ');
            else puts("IMPOSSIBLE");
        }
        return 0;
    }


  • 相关阅读:
    Linux学习笔记(20)linux exec
    【最佳实践】filezilla软件用bat自动化ftp传输文件
    Windows定时任务定时执行bat文件标准输出默认位置
    安装mysql odbc5.3.13
    如何查看windows server是否有web发布?
    C#串口通讯
    asp.net中通过post的方式导出文件操作。
    C#USB口通讯
    jquery UI的Widet
    asp.net中的常规认证方式枚举(涵盖mvc)(一)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3297189.html
Copyright © 2020-2023  润新知