本题要求输出所有拓扑排序的序列。

还好本题的数据量不是非常大。限制在26个大写英文字母,故此能够使用递归法输出。

这个递归输出所有解在Leetcode非常多这种题目的,不小心的话,还是非常难调试的。

整体考了递归和拓扑排序,还有推断能否够拓扑排序-就是是否图有环。

考了三大知识点。难度还是有的。由于数据量不大,故此推断环能够使用一般递归方法。递归仅仅须要注意细节就好了。

#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
const int ALPHA = 26;
bool gra[ALPHA][ALPHA];
int id = 0, len = 0;
const int MAX_B = 512;
char buf[MAX_B]; char getFromBuf()
{
if (id >= len)
{
len = fread(buf, 1, MAX_B, stdin);
id = 0;
}
return buf[id++];
} struct Subset
{
int p, r;
}; Subset subs[ALPHA]; int findParent(int u)
{
if (u != subs[u].p) subs[u].p = findParent(subs[u].p);
return subs[u].p;
} void unionTwo(int x, int y)
{
int xroot = findParent(x);
int yroot = findParent(y);
if (subs[xroot].r < subs[yroot].r) subs[xroot].p = yroot;
else
{
subs[yroot].p = xroot;
if (subs[yroot].r == subs[xroot].r) subs[xroot].r++;
}
} void initSubs()
{
for (int i = 0; i < ALPHA; i++)
{
subs[i].p = i;
subs[i].r = 0;
}
} bool isCycle()
{
initSubs();
for (int i = 0; i < ALPHA; i++)
{
for (int j = 0; j < ALPHA; j++)
{
if (i == j || !gra[i][j]) continue;
int ip = findParent(i);
int jp = findParent(j);
if (ip == jp) return true;
unionTwo(i, j);
}
}
return false;
} int rs[ALPHA];
bool vis[ALPHA];
void printAllTopologicalOrders(int N, int rsid = 0)
{
if (rsid == N) //递归究竟了,打印结果
{
if (rsid > 0) putchar(rs[0]+'A');
for (int i = 1; i < rsid; i++)
{
putchar(' ');
putchar(rs[i]+'A');
}
putchar('\n');
return ;
}
for (int i = 0; i < ALPHA; i++)
{
if (vis[i]) continue;//这里要推断,漏了这句。好难dubg啊。 要细致用脑去模拟过程才干发现!! ! if (!gra[i][i]) continue; //没有这个点,跳过
int j = 0;
for ( ; j < ALPHA; j++)//检查i是否有入度
{
if (i != j && !vis[j] && gra[j][i]) break;
}
if (j == ALPHA) //找到入度为零的点i了
{
vis[i] = true;
rs[rsid] = i;
printAllTopologicalOrders(N, rsid+1);
vis[i] = false;
}
}
} int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int N = 0;
memset(gra, 0, sizeof(gra));
char a = getFromBuf();
while ((a == '\n' || a == ' ') && len) a = getFromBuf();
while (a != '\n' && len)
{
if ('A' <= a && a <= 'Z')
{
gra[a-'A'][a-'A'] = 1;//代表有点
N++;
}
a = getFromBuf();
} while ((a == '\n' || a == ' ') && len) a = getFromBuf();
while (a != '\n' && len)
{
int u = a - 'A';
a = getFromBuf(); a = getFromBuf();
int v = a - 'A'; gra[u][v] = 1; a = getFromBuf();
if (a == '\n') break;
a = getFromBuf();
}
fill(vis, vis+ALPHA, false);
if (isCycle()) puts("NO");
else
{
printAllTopologicalOrders(N);
}
if (T) putchar('\n');
}
return 0;
}

UVa 872 - Ordering 输出全拓扑排序的更多相关文章

  1. UVA - 10305 Ordering Tasks(拓扑排序)

    题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...

  2. UVa 10305 Ordering Tasks【拓扑排序】

    题意:给出n件事情,m个二元组关系,求它们的拓扑序列 用的队列来做 #include<iostream> #include<cstdio> #include<cstrin ...

  3. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  4. UVa 1572 Self-Assembly (构造+拓扑排序。。。。。)

    题意:给定n个带标号的正方形,标号要么是一个大写字母加一个+或-,要么是00, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排 ...

  5. UVA10305:Ordering Tasks(拓扑排序)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  6. UVA - 12263 Rankings 模拟(拓扑排序)

    题意:1~n这n个数,给你一个初始的顺序,再告诉你那两个数的大小关系发生了变化,求变化后的 顺序,不存在则输出IMPOSSIBLE 思路:这题很遗憾没在比赛的时候过掉,结束后加了一行就AC了.题目真的 ...

  7. UVA-10305 Ordering Tasks (拓扑排序)

    题目大意:给出n个点,m条关系,按关系的从小到大排序. 题目分析:拓扑排序的模板题,套模板. kahn算法: 伪代码: Kahn算法: 摘一段维基百科上关于Kahn算法的伪码描述: L← Empty ...

  8. UVA 10305:Ordering Tasks(拓扑排序)

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  9. hdu 2647 (拓扑排序 邻接表建图的模板) Reward

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...

随机推荐

  1. Keil的c语言编译器

    我曾经通过查看反汇编代码对KEILC编译器进行了测试,大概有这么一下内容,也得出一些结论. (1)全局变量:如果程序中定义了全局变量,而且初始值不是0.此时,在程序调到main()函数执行前,除了要进 ...

  2. Java 之 网络编程

    1.OSI模型 a.全称:开放系统互联 b.七层模型:应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 c.注意:OSI模型 是所谓的 理论标准 2.TCP/IP模型 a.四层模型:应用层. ...

  3. Socket实现简单的聊天通信

    最近学习了Socket后,感觉Socket挺好玩的,在博客中看到socket在实时聊天功能的很强大,于是乎就做了一个简单的聊天功能,今天贴出来,能够与大家一起共享,有不对之处,能够给予指出,谢谢! 服 ...

  4. 推荐几款jQuery表格插件

    平时项目中,会碰到很多表格元素,这里推荐几款jQuery表格插件. Stackable.js 通常在小屏幕上,表格的表形形式不大好,因为用户会缩放平移,或者就是表格太小,导致数据不可见.Stackab ...

  5. hdu 4435

    一道枚举+搜索题: 很容易看出这道题目要求尽量不在大的城市里面建加油站: 所以从最大的城市开始枚举! 代码: #include<cstdio> #include<cmath> ...

  6. 【BZOJ 3110】 [Zjoi2013]K大数查询(整体二分)

    [题目] Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到 ...

  7. 【BZOJ 2154】Crash的数字表格 (莫比乌斯+分块)

    2154: Crash的数字表格 Description 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b)表示能 ...

  8. Android开源项目发现---ProgressBar 篇(持续更新)

    1. SmoothProgressBar 水平进度条 项目地址:https://github.com/castorflex/SmoothProgressBar Demo地址:https://play. ...

  9. Boostrap 模态框 水平垂直居中问题

            var editorB = new UE.ui.Editor({ initialFrameHeight: 350, initialFrameWidth: 600 });         ...

  10. 2.linux下Makefile编写规范

    转自陈皓 (CSDN) 概述—— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 profession ...