PAT甲級1126 Eulerian Path (25 分)

什么是歐拉路徑?

歐拉路徑是無向連通圖中的一條路徑留晚,該路徑經(jīng)過圖的每一條邊且僅經(jīng)過一次酵紫。如果路徑起點(diǎn)和終點(diǎn)相同,則稱“歐拉回路”错维。具有歐拉回路的圖稱“歐拉圖”奖地。

如何判斷圖中是否存在歐拉路徑?

由歐拉路徑的定義可知赋焕,若圖中存在歐拉路徑参歹,則該圖必是一個(gè)連通圖 (1),其次隆判,圖中度數(shù)為奇數(shù)的點(diǎn)的個(gè)數(shù)必須為0或2 (2)犬庇,若度數(shù)為奇數(shù)的點(diǎn)的個(gè)數(shù)為0則是歐拉回路,若個(gè)數(shù)為2則是非歐拉回路的歐拉路徑在此題中稱為"Semi-Eulerian"侨嘀,其余情況均不是歐拉路徑臭挽。

原題

1126 Eulerian Path (25 分)

In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard Euler while solving the famous Seven Bridges of Konigsberg problem in 1736. It has been proven that connected graphs with all vertices of even degree have an Eulerian circuit, and such graphs are called Eulerian. If there are exactly two vertices of odd degree, all Eulerian paths start at one of them and end at the other. A graph that has an Eulerian path but not an Eulerian circuit is called semi-Eulerian. (Cited from https://en.wikipedia.org/wiki/Eulerian_path)
Given an undirected graph, you are supposed to tell if it is Eulerian, semi-Eulerian, or non-Eulerian.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N (≤ 500), and M, which are the total number of vertices, and the number of edges, respectively. Then M lines follow, each describes an edge by giving the two ends of the edge (the vertices are numbered from 1 to N).

Output Specification:

For each test case, first print in a line the degrees of the vertices in ascending order of their indices. Then in the next line print your conclusion about the graph -- either Eulerian, Semi-Eulerian, or Non-Eulerian. Note that all the numbers in the first line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input 1:

7 12
5 7
1 2
1 3
2 3
2 4
3 4
5 2
7 6
6 3
4 5
6 4
5 6

Sample Output 1:

2 4 4 4 4 4 2
Eulerian

Sample Input 2:

6 10
1 2
1 3
2 3
2 4
3 4
5 2
6 3
4 5
6 4
5 6

Sample Output 2:

2 4 4 4 3 3
Semi-Eulerian

Sample Input 3:

5 8
1 2
2 5
5 4
4 1
1 3
3 2
3 4
5 3

Sample Output 3:

3 3 4 3 3
Non-Eulerian

AC代碼(C++)

#include <iostream>
#include <vector>
using namespace std;
int N, M, v1, v2, visited[505], degrees[505], judge = 1, cnt;
vector<int>graph[505];
void dfs(int d){
    visited[d] = 1;
    for(int i = 0; i < graph[d].size(); i++){
        if(!visited[graph[d][i]])dfs(graph[d][i]);
    }
}
int main(){
    scanf("%d%d", &N, &M);
    for(int i = 0; i < M; i++){
        scanf("%d%d", &v1, &v2);
        graph[v1].push_back(v2);
        graph[v2].push_back(v1);
        degrees[v1]++;
        degrees[v2]++;
    }
    dfs(1);
    for(int i = 1; i <= N; i++)
        if(!visited[i]){
            judge = 0;
            break;
        }
    for(int i = 1; i <= N; i++)
        if(i == 1)printf("%d", degrees[i]);
        else printf(" %d", degrees[i]);
    printf("\n");
    if(judge){
        for(int i = 1; i <= N; i++)
            if(degrees[i] % 2 == 1)cnt++;
        if(cnt == 0)printf("Eulerian\n");
        else if(cnt == 2) printf("Semi-Eulerian\n");
        else printf("Non-Eulerian\n");
    }else printf("Non-Eulerian\n");
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市咬腕,隨后出現(xiàn)的幾起案子欢峰,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纽帖,死亡現(xiàn)場離奇詭異宠漩,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)抛计,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門哄孤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人吹截,你說我怎么就攤上這事瘦陈。” “怎么了波俄?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵晨逝,是天一觀的道長。 經(jīng)常有香客問我懦铺,道長捉貌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任冬念,我火速辦了婚禮趁窃,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘急前。我一直安慰自己醒陆,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布裆针。 她就那樣靜靜地躺著刨摩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪世吨。 梳的紋絲不亂的頭發(fā)上澡刹,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音耘婚,去河邊找鬼罢浇。 笑死,一個(gè)胖子當(dāng)著我的面吹牛沐祷,可吹牛的內(nèi)容都是我干的嚷闭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼戈轿,長吁一口氣:“原來是場噩夢啊……” “哼凌受!你這毒婦竟也來了阵子?” 一聲冷哼從身側(cè)響起思杯,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后色乾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體誊册,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年暖璧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了案怯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡澎办,死狀恐怖嘲碱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情局蚀,我是刑警寧澤麦锯,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站琅绅,受9級特大地震影響扶欣,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜千扶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一料祠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧澎羞,春花似錦髓绽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至摆碉,卻和暖如春塘匣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背巷帝。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工忌卤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人楞泼。 一個(gè)月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓驰徊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親堕阔。 傳聞我的和親對象是個(gè)殘疾皇子棍厂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

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