【拓撲排序】POJ_2367_Genealogical tree

Genealogical tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5027 Accepted: 3317 Special Judge

Description
The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a hundred of children. Martians have got used to this and their style of life seems to them natural.
And in the Planetary Council the confusing genealogical system leads to some embarrassment. There meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. However, the maintenance of this order really is not a trivial task. Not always Martian knows all of his parents (and there's nothing to tell about his grandparents!). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal.
Your task is to write a program, which would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.

Input
The first line of the standard input contains an only number N, 1 <= N <= 100 — a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council are enumerated with the natural numbers from 1 up to N. Further, there are exactly N lines, moreover, the I-th line contains a list of I-th member's children. The list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. The list of children may be empty. The list (even if it is empty) ends with 0.

Output
The standard output should contain in its only line a sequence of speakers' numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard output any of them. At least one such sequence always exists.

Sample Input
5
0
4 5 1 0
1 0
5 3 0
3 0

Sample Output
2 4 5 3 1

Source
Ural State University Internal Contest October'2000 Junior Session

題意:
有n個節(jié)點僚碎,形成父子關(guān)系,求一個從老到小的合理的順序君仆。

思路:
拓撲排序伶唯×梗可得n個節(jié)點形成一個有向無環(huán)圖,每次取出其中一個入度為0的節(jié)點(無其他節(jié)點指向其)绪励,最終得到的順序既是題目所求铐炫。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn = 100;
int buf[maxn + 5][maxn + 5];
int cnt[maxn + 5];
int ans[maxn + 5];

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        memset(buf, 0, sizeof(buf));
        memset(cnt, 0, sizeof(cnt));
        int num;
        for (int i = 1; i <= n; ++i) {
            while (scanf("%d", &num) && num) {
                buf[i][num] = 1;
                ++cnt[num];
            }
        }
        int top = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (cnt[j] == 0) {
                    ans[top++] = j;
                    cnt[j] = -1;
                    for (int k = 1; k <= n; ++k) {
                        if (buf[j][k] == 1)
                            --cnt[k];
                    }
                    break;
                }
            }
        }
        for (int i = 0; i < top - 1; ++i)
            printf("%d ", ans[i]);
        printf("%d\n", ans[top - 1]);
    }
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末黎烈,一起剝皮案震驚了整個濱河市习柠,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌照棋,老刑警劉巖资溃,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異烈炭,居然都是意外死亡溶锭,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門符隙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來趴捅,“玉大人垫毙,你說我怎么就攤上這事∽な郏” “怎么了露久?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長欺栗。 經(jīng)常有香客問我,道長征峦,這世上最難降的妖魔是什么迟几? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮栏笆,結(jié)果婚禮上类腮,老公的妹妹穿的比我還像新娘。我一直安慰自己蛉加,他們只是感情好蚜枢,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著针饥,像睡著了一般厂抽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上丁眼,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天筷凤,我揣著相機與錄音,去河邊找鬼苞七。 笑死藐守,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的蹂风。 我是一名探鬼主播卢厂,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼惠啄!你這毒婦竟也來了慎恒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤礁阁,失蹤者是張志新(化名)和其女友劉穎巧号,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體姥闭,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡丹鸿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了棚品。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片靠欢。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡廊敌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出门怪,到底是詐尸還是另有隱情骡澈,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布掷空,位于F島的核電站肋殴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏坦弟。R本人自食惡果不足惜护锤,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望酿傍。 院中可真熱鬧烙懦,春花似錦、人聲如沸赤炒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽莺褒。三九已至掩缓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間癣朗,已是汗流浹背拾因。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留旷余,地道東北人绢记。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像正卧,于是被迫代替她去往敵國和親蠢熄。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容