标签:拓扑排序 c++
<span style="font-size:24px;">#include<cstdio> #include<cstring> using namespace std; const int maxn = 500 + 10; int map[maxn][maxn]; int d[maxn]; int n,m; void toposort() { for(int i = 1;i <= n;i++) { //逐个输出n个元素 for(int j = 1;j <= n;j++) { //寻找第i个元素 if(d[j] == 0) { //前面无元素,输出来 d[j]--; if(i != n) printf("%d ",j); else printf("%d\n",j); //输出此元素后,其后的能确定在其后的元素的度减一 //也即确定在其后的元素的前面的元素个数减少了1 for(int k = 1;k <= n;k++) { if(map[j][k]) { d[k]--; } } break; } } } } int main() { while(~scanf("%d %d",&n,&m)) { memset(map,0,sizeof(map)); memset(d,0,sizeof(d)); int x,y; for(int i = 0;i < m;i++) { scanf("%d %d",&x,&y); if(!map[x][y]) { map[x][y] = 1; //x 胜 y ,也就是x 在 y 前面 d[y]++; //统计有几个在y前面的 } } toposort(); } return 0; }</span>
<span style="font-size:24px;">附:</span>
<span style="font-size:24px;"></span><div class="hiddable" id="vj_description" style="font-family: 'times new roman'; font-size: 17px; color: rgb(34, 34, 34); background-color: rgb(204, 232, 207);"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><div class="panel_content">有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。 </div><div class="panel_bottom"> </div></div></div><div class="hiddable" id="vj_input" style="font-family: 'times new roman'; font-size: 17px; color: rgb(34, 34, 34); background-color: rgb(204, 232, 207);"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><div class="panel_content">输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。 </div><div class="panel_bottom"> </div></div></div><div class="hiddable" id="vj_output" style="font-family: 'times new roman'; font-size: 17px; color: rgb(34, 34, 34); background-color: rgb(204, 232, 207);"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Output</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><div class="panel_content">给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。 其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。 </div><div class="panel_bottom"> </div></div></div><div class="hiddable" id="vj_sampleInput" style="font-family: 'times new roman'; font-size: 17px; color: rgb(34, 34, 34); background-color: rgb(204, 232, 207);"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Sample Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><div class="panel_content"><pre style="white-space: pre-wrap; word-wrap: break-word;"><div style="font-family: 'Courier New', Courier, monospace;"> 4 3 1 2 2 3 4 3 </div>
Sample Output
1 2 4 3
标签:拓扑排序 c++
原文地址:http://blog.csdn.net/mrshang110/article/details/45503249