hdoj5154

題目:

Problem Description
In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We number the processes from 1 to n. However there are some dependencies between some processes. When there exists a dependencies (a, b), it means process b must be finished before process a. By knowing all the m dependencies, Harry wants to know if the computer can finish all the n processes.
Input
There are several test cases, you should process to the end of file.For each test case, there are two numbers n m on the first line, indicates the number processes and the number of dependencies. 1≤n≤100,1≤m≤10000
The next following m lines, each line contains two numbers a b, indicates a dependencies (a, b). 1≤a,b≤n
Output
Output one line for each test case. If the computer can finish all the process print "YES" (Without quotes).Else print "NO" (Without quotes).
Sample Input
3 2
3 1
2 1
3 3
3 2
2 1
1 3
Sample Output
YES
NO

根據(jù)題目意思暮芭,可以知道此題為拓?fù)渑判颉?br> 此題會(huì)出現(xiàn)一種情況:環(huán)耸别。因?yàn)橥負(fù)渑判蛞蟠藞D是有向無(wú)環(huán)圖,因此如果有環(huán),肯定不能拓?fù)渑判颍ㄝ敵鯪O),如果能用拓?fù)渑判颍瑒t輸出YES.

由于拓?fù)渑判蛎看味颊业饺攵葹?的點(diǎn),然后入隊(duì)列維護(hù)。如果此有向圖有環(huán)峭范,必然存在入度無(wú)法減為0的點(diǎn),因此可以統(tǒng)計(jì)最終出隊(duì)列的個(gè)數(shù)是否為n.

參考代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
const int MAVE = 10000+10;
const int MAXV = 100+10;
using namespace std;
struct edge {
    int next,to;
} e[MAVE];
int head[MAXV],tot;
void init() {
    memset(head,-1,sizeof(head));
    tot = 0;
}
void add_edge(int a,int b) {
    e[tot].next = head[a];
    e[tot].to = b;
    head[a] = tot++;
}
int n,m;
int din[MAXV];
bool topo() {
    queue<int> que;
    for (int i = 1;i <= n;++i) {
        if (din[i] == 0) que.push(i);
    }
    int cnt = 0;
    while (!que.empty()) {
        int u = que.front();
        que.pop();
        for (int i = head[u];i != -1;i = e[i].next) {
            int v = e[i].to;
            --din[v];
            if (din[v] == 0) {
                que.push(v);
            }
        }
        cnt++;
    }
    if (cnt == n) {
        return true;
    }
    else return false;
}
int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        init();
        memset(din,0,sizeof(din));
        int a,b;
        for (int i = 1;i <= m;++i) {
            scanf("%d%d", &a, &b);
            add_edge(a,b);
            din[b]++;
        }
        if (topo()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瘪贱,一起剝皮案震驚了整個(gè)濱河市纱控,隨后出現(xiàn)的幾起案子辆毡,更是在濱河造成了極大的恐慌,老刑警劉巖甜害,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舶掖,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡尔店,警方通過(guò)查閱死者的電腦和手機(jī)眨攘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)嚣州,“玉大人鲫售,你說(shuō)我怎么就攤上這事「秒龋” “怎么了情竹?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)匀哄。 經(jīng)常有香客問我秦效,道長(zhǎng),這世上最難降的妖魔是什么涎嚼? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任阱州,我火速辦了婚禮,結(jié)果婚禮上法梯,老公的妹妹穿的比我還像新娘苔货。我一直安慰自己,他們只是感情好立哑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布蒲赂。 她就那樣靜靜地躺著,像睡著了一般刁憋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上木蹬,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天至耻,我揣著相機(jī)與錄音,去河邊找鬼镊叁。 笑死尘颓,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的晦譬。 我是一名探鬼主播疤苹,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼敛腌!你這毒婦竟也來(lái)了卧土?” 一聲冷哼從身側(cè)響起惫皱,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎尤莺,沒想到半個(gè)月后旅敷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡颤霎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年媳谁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片友酱。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡晴音,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出缔杉,到底是詐尸還是另有隱情锤躁,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布壮吩,位于F島的核電站进苍,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏鸭叙。R本人自食惡果不足惜觉啊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望沈贝。 院中可真熱鬧杠人,春花似錦、人聲如沸宋下。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)学歧。三九已至罩引,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間枝笨,已是汗流浹背袁铐。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留横浑,地道東北人剔桨。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像徙融,于是被迫代替她去往敵國(guó)和親洒缀。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

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