歐拉圖_Watchcow

source

Description

Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she's done.

If she were a more observant cow, she might be able to just walk each of M (1 <= M <= 50,000) bidirectional trails numbered 1..M between N (2 <= N <= 10,000) fields numbered 1..N on the farm once and be confident that she's seen everything she needs to see. But since she isn't, she wants to make sure she walks down each trail exactly twice. It's also important that her two trips along each trail be in opposite directions, so that she doesn't miss the same thing twice.

A pair of fields might be connected by more than one trail. Find a path that Bessie can follow which will meet her requirements. Such a path is guaranteed to exist.
Input

  • Line 1: Two integers, N and M.
  • Lines 2..M+1: Two integers denoting a pair of fields connected by a path.
    Output
  • Lines 1..2M+1: A list of fields she passes through, one per line, beginning and ending with the barn at field 1. If more than one solution is possible, output any solution.

Sample Input

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

Sample Output

1
2
3
4
2
1
4
3
2
4
1

題意:打印歐拉回路拜轨,這道題有Special judge允青,答案不唯一
題解:圖的表示可以用鏈式前向星;遍歷用DFS

#include <cstdio>
#include <string.h>
using namespace std;
const int V = 10010;
const int E = 100010;
int head[V],cnt;
struct Edge{

    int to;
    int next;
    bool vis;
};
Edge edge[E];
void addEdge(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    edge[cnt].vis=false;
    head[u]=cnt++;
}
void DFS(int s)
{
    for(int i=head[s];~i;i=edge[i].next)
    {
        if(!edge[i].vis)
        {
            edge[i].vis=true;
            DFS(edge[i].to);
            printf("%d\n",s);
        }
    }
}
int main(){

    int n,m;
    scanf("%d%d",&n,&m);
    cnt=0;
    memset(head, -1, sizeof(head));
    int u,v;
    for(int i=0; i<m; i++){
        scanf("%d%d",&u,&v);
        addEdge(u, v);
        addEdge(v, u);
    }
    printf("1\n");
    DFS(1);
    return 0;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市皆串,隨后出現的幾起案子眉枕,更是在濱河造成了極大的恐慌,老刑警劉巖速挑,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件姥宝,死亡現場離奇詭異,居然都是意外死亡套么,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門省咨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來玷室,“玉大人穷缤,你說我怎么就攤上這事∩鹣睿” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵囊陡,是天一觀的道長掀亥。 經常有香客問我搪花,道長,這世上最難降的妖魔是什么撮竿? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任幢踏,我火速辦了婚禮,結果婚禮上僚匆,老公的妹妹穿的比我還像新娘搭幻。我一直安慰自己,他們只是感情好檀蹋,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著攻臀,像睡著了一般焕数。 火紅的嫁衣襯著肌膚如雪纱昧。 梳的紋絲不亂的頭發(fā)上刨啸,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音识脆,去河邊找鬼设联。 笑死,一個胖子當著我的面吹牛灼捂,可吹牛的內容都是我干的离例。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼悉稠,長吁一口氣:“原來是場噩夢啊……” “哼宫蛆!你這毒婦竟也來了?” 一聲冷哼從身側響起的猛,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎卦尊,沒想到半個月后叛拷,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡岂却,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年忿薇,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片躏哩。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡署浩,死狀恐怖,靈堂內的尸體忽然破棺而出扫尺,到底是詐尸還是另有隱情瑰抵,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布器联,位于F島的核電站二汛,受9級特大地震影響,放射性物質發(fā)生泄漏拨拓。R本人自食惡果不足惜肴颊,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望渣磷。 院中可真熱鬧婿着,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至丘侠,卻和暖如春徒欣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蜗字。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工打肝, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人挪捕。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓粗梭,卻偏偏與公主長得像,于是被迫代替她去往敵國和親级零。 傳聞我的和親對象是個殘疾皇子断医,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內容