拓扑排序

简单思路:

习惯构造一个邻接表(vector[]),将拓扑序大的(即入度小的)作为表头,拓扑序小的压入对应向量中,对于拓扑序相等的,用并查集合并(有需要的话),这样操作的必须为并查集的各个根。


知识:

1.信息不完整的表现是同时出现两个或两个以上的元素,其入度为0,且其根是自己(因为有可能是=合并后造成的情况)

2.出现冲突的表现是出现环了,这样就会造成拓扑排序的元素少于需要排序的元素(直观体现)。


示例代码:

ZOJ – 1060 Sorting It All Out

仅有大小关系的拓扑排序,但是需要确定哪一步排序完成或者哪一步冲突了,所以每次都要排序

#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
#include "map"
using namespace std;
typedef long long LL;
const int INF = 0xfffffff;

int n,m;
vector<int> G[30];  //将拓扑序大的作为表头,也就是说小的压入大的(习惯操作)
vector<int> out;
int in[30];         //入度

void init()
{
    for(int i = 0 ; i < 30 ; i++)
        G[i].clear();

    memset(in,0,sizeof(in));
}

bool find(int x,int y)              //本题中有重复输入,剔除
{
    for(int i = 0 ; i < G[x].size() ; i++)
    {
        if(G[x][i] == y)
            return 1;
    }
    return 0;
}

int top()
{
    int IN[30];

    memcpy(IN,in,sizeof(in));       //由于是每输一条命令判断一次,所以不能直接改变in数组

    int zhuangtai=0;
    out.clear();

    queue<int> lis;
    for(int i = 0 ; i < n ; i++)
        if(!IN[i])                  //将入度为0的压入
            lis.push(i);

    while(!lis.empty())
    {
        if(lis.size() > 1)
            zhuangtai = 1;

        int tmp = lis.front();
        lis.pop();

        out.push_back(tmp);

        for(int i = 0 ; i < G[tmp].size() ; i++)        //更新所有相关入度
        {
            if(--IN[G[tmp][i]] == 0)
                lis.push(G[tmp][i]);
        }
    }

    if(out.size() < n)                          //不必担心未完成输入时判断成冲突,因为所有未输入的入度全为0
        return 0;
    if(zhuangtai)
        return 1;
    return 2;
}


int main()
{
    ios ::sync_with_stdio(false);
    cin.tie(NULL);

    while(cin >> n >> m && (n||m))
    {
        int ok=1;
        int pos = -1;
        init();

        for(int i = 0 ; i < m ; i++)
        {
            char a,op,b;
            cin >> a >> op >> b;
            int x = a-'A',y = b-'A';

            if(ok != 1)
                continue;

            if(!find(y,x))
            {
                G[y].push_back(x);
                in[x]++;
            }

            ok = top();

            if(ok != 1)
                pos = i+1;
        }

        if(ok == 0)
            printf("Inconsistency found after %d relations.\n",pos);
        else if(ok == 1)
            printf("Sorted sequence cannot be determined.\n");
        else
        {
            printf("Sorted sequence determined after %d relations: ",pos);
            for(int i = out.size()-1 ; i >= 0  ; i--)
                printf("%c",out[i]+'A');
            printf(".\n");
        }
    }
    return 0;
}

hdu 1811 - Rank of Tetris(并查集+拓扑)

有等于关系的判断,信息不完整和冲突都在此题体现了

#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
using namespace std;
typedef long long LL;
const int INF = 0xfffffff;

int n,m;
int set[10010];

int find(int x)
{
    return set[x] != x ? set[x] = find(set[x]) : x;

}

void merger(int x,int y)
{
    ((x = find(x)) != (y = find(y))) && (set[x] = y);

}

vector<int> G[10010];
int in[10010];                  //计算入度
int a[20010],b[20010];
char op[20010];
bool isConflict = false , isUnsure = false;
int cnt;

void init()
{
    for(int i = 0 ; i < n ; i++)
        set[i]=i;

    for(int i = 0 ; i < n ; i++)
        G[i].clear();

    memset(in,0,sizeof(in));

   isConflict = false , isUnsure = false;
}

void top()
{
    queue<int> q;

    for(int i = 0 ; i < n ; i++)            //入度为0,且是根
    {
        int tmp = find(i);

        if(!in[tmp] && set[tmp] == i)       //很重要的判断,本题是将所有相等的看成一个点进行top,所以就不能操作那些不是根节点的点了
            q.push(tmp);
    }

    while(!q.empty())
    {
        if(q.size() > 1)
            isUnsure = true;

        int cur = q.front();
        q.pop();
        cnt--;

        for(int i = 0 ; i < G[cur].size() ; i++)
        {
            in[G[cur][i]]--;
            if(!in[G[cur][i]])      //由于一条邻接链里储存的都是根节点,故不必再判断是否为根节点
                q.push(G[cur][i]);
        }
    }
}

int main()
{
    while(cin >> n >> m)
    {
        init();
        cnt=n;

        for(int i = 0 ; i < m ; i++)
        {
            cin >> a[i]>> op[i] >> b[i];
            if(op[i] == '=')
            {
                cnt--;
                merger(a[i],b[i]);
            }
        }

        for(int i = 0 ; i < m ; i++)
        {
            if(op[i] == '=')
                continue;

            int x = find(a[i]);
            int y = find(b[i]);

            if(x == y)
            {
                isConflict = true;
                break;
            }

            if(op[i] == '<')
                G[y].push_back(x),in[x]++;
            else
                G[x].push_back(y),in[y]++;
        }

        top();

        if(cnt>0 || isConflict)
            cout << "CONFLICT" << endl;
        else if(isUnsure)
            cout << "UNCERTAIN" << endl;
        else
            cout << "OK" << endl;
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值