標籤:拓撲排序 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 31 22 34 3 </div>
Sample Output
1 2 4 3
HDU 1285 確定比賽名次