亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Play on Words(有向圖歐拉路)

系統(tǒng) 1573 0
Time Limit: ?1000MS ? Memory Limit: ?10000K
Total Submissions: ?8571 ? Accepted: ?2997

Description

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.?

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.?

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.

Output

Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.?
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".?

Sample Input

    3

2

acm

ibm

3

acm

malform

mouse

2

ok

ok


  

Sample Output

    The door cannot be opened.

Ordering is possible.

The door cannot be opened.
    

題意:給出n個(gè)單詞,問(wèn)所有的單詞能否首尾相連(能相連的單詞的首和尾必須是相同的);

思路:一道判斷有向圖歐拉路的題目;
   可以將每個(gè)單詞的首和尾看作節(jié)點(diǎn),判斷圖的連通性可以用并查集,每輸入一個(gè)單詞將其首和尾加入集合中,最后任取一個(gè)節(jié)點(diǎn)判斷其他所有節(jié)點(diǎn)和該節(jié)點(diǎn)是否有共同的祖先,若是,則是連通的,否則不連通;
         在連通性的前提下,若所有節(jié)點(diǎn)的入讀等于出度 或者 一個(gè)節(jié)點(diǎn)的入度比出度大1 一個(gè)節(jié)點(diǎn)的入度比出度小1,說(shuō)明有歐拉路,否則沒(méi)有歐拉路;
    
因?yàn)槭终`,貢獻(xiàn)一次WA;
        
            1
        
         #include<stdio.h>


        
            2
        
         #include<
        
          string
        
        .h>


        
            3
        
        
            4
        
        
          int
        
        
          set
        
        [
        
          30
        
        ],indegree[
        
          1010
        
        ],outdegree[
        
          1010
        
        ],vis[
        
          26
        
        
          ];


        
        
            5
        
        
          int
        
        
           count;


        
        
            6
        
        
            7
        
        
          void
        
        
           init()


        
        
            8
        
        
          {


        
        
            9
        
        
          for
        
        (
        
          int
        
         i = 
        
          0
        
        ; i < 
        
          26
        
        ; i++
        
          )


        
        
           10
        
        
          set
        
        [i] =
        
           i;


        
        
           11
        
        
          }


        
        
           12
        
        
           13
        
        
          int
        
         find(
        
          int
        
        
           x)


        
        
           14
        
        
          {


        
        
           15
        
        
          if
        
        (
        
          set
        
        [x] !=
        
           x)


        
        
           16
        
        
          set
        
        [x] = find(
        
          set
        
        
          [x]);


        
        
           17
        
        
          return
        
        
          set
        
        
          [x];


        
        
           18
        
        
          }


        
        
           19
        
        
           20
        
        
          int
        
        
           check()


        
        
           21
        
        
          {


        
        
           22
        
        
          int
        
        
           x,i;


        
        
           23
        
        
          int
        
         flag = 
        
          0
        
        
          ;


        
        
           24
        
        
          for
        
        (i = 
        
          0
        
        ; i < 
        
          26
        
        ; i++
        
          )


        
        
           25
        
        
              {


        
        
           26
        
        
          if
        
        
          (vis[i])


        
        
           27
        
        
                  {


        
        
           28
        
        
          if
        
        (flag == 
        
          0
        
        
          )


        
        
           29
        
        
                      {


        
        
           30
        
                         x =
        
           find(i);


        
        
           31
        
                         flag = 
        
          1
        
        
          ;


        
        
           32
        
        
                      }


        
        
           33
        
        
          else
        
        
           34
        
        
                      {


        
        
           35
        
        
          if
        
        (find(i) !=
        
           x)


        
        
           36
        
        
          break
        
        
          ;


        
        
           37
        
        
                      }


        
        
           38
        
        
                  }


        
        
           39
        
        
              }


        
        
           40
        
        
          if
        
        (i < 
        
          26
        
        
          )


        
        
           41
        
        
          return
        
        
          0
        
        ;
        
          //
        
        
          圖是不連通的,直接返回;
        
        
           42
        
        
           43
        
        
          int
        
         c1 = 
        
          0
        
        , c2 = 
        
          0
        
        , c3 = 
        
          0
        
        
          ;


        
        
           44
        
        
          for
        
        (
        
          int
        
         i = 
        
          0
        
        ; i < 
        
          26
        
        ; i++
        
          )


        
        
           45
        
        
              {


        
        
           46
        
        
          if
        
        
          (vis[i])


        
        
           47
        
        
                  {


        
        
           48
        
        
          if
        
        (indegree[i] ==
        
           outdegree[i])


        
        
           49
        
                         c1++
        
          ;


        
        
           50
        
        
          else
        
        
          if
        
        (indegree[i]-outdegree[i] == 
        
          1
        
        
          )


        
        
           51
        
                         c2++
        
          ;


        
        
           52
        
        
          else
        
        
          if
        
        (outdegree[i]-indegree[i] == 
        
          1
        
        
          )


        
        
           53
        
                         c3++
        
          ;


        
        
           54
        
        
                  }


        
        
           55
        
        
              }


        
        
           56
        
        
          if
        
        ((c2 == 
        
          1
        
         && c3 == 
        
          1
        
         && c1 == count-
        
          2
        
        ) ||(c1 ==
        
           count))


        
        
           57
        
        
          return
        
        
          1
        
        
          ;


        
        
           58
        
        
          else
        
        
          return
        
        
          0
        
        
          ;


        
        
           59
        
        
          }


        
        
           60
        
        
           61
        
        
          int
        
        
           main()


        
        
           62
        
        
          {


        
        
           63
        
        
          int
        
        
           test,n;


        
        
           64
        
        
          char
        
         s[
        
          1010
        
        
          ];


        
        
           65
        
             scanf(
        
          "
        
        
          %d
        
        
          "
        
        ,&
        
          test);


        
        
           66
        
        
          while
        
        (test--
        
          )


        
        
           67
        
        
              {


        
        
           68
        
                 memset(indegree,
        
          0
        
        ,
        
          sizeof
        
        
          (indegree));


        
        
           69
        
                 memset(outdegree,
        
          0
        
        ,
        
          sizeof
        
        
          (outdegree));


        
        
           70
        
                 memset(vis,
        
          0
        
        ,
        
          sizeof
        
        
          (vis));


        
        
           71
        
        
                  init();


        
        
           72
        
                 count = 
        
          0
        
        
          ;


        
        
           73
        
        
           74
        
                 scanf(
        
          "
        
        
          %d
        
        
          "
        
        ,&
        
          n);


        
        
           75
        
        
          for
        
        (
        
          int
        
         i = 
        
          1
        
        ; i <= n; i++
        
          )


        
        
           76
        
        
                  {


        
        
           77
        
                     scanf(
        
          "
        
        
          %s
        
        
          "
        
        
          ,s);


        
        
           78
        
        
          int
        
         len =
        
           strlen(s);


        
        
           79
        
        
          int
        
         u = s[
        
          0
        
        ]-
        
          '
        
        
          a
        
        
          '
        
        
          ;


        
        
           80
        
        
          if
        
        (!
        
          vis[u])


        
        
           81
        
        
                      {


        
        
           82
        
                         vis[u] = 
        
          1
        
        
          ;


        
        
           83
        
                         count++
        
          ;


        
        
           84
        
        
                      }


        
        
           85
        
        
          int
        
         v = s[len-
        
          1
        
        ]-
        
          '
        
        
          a
        
        
          '
        
        
          ;


        
        
           86
        
        
          if
        
        (!
        
          vis[v])


        
        
           87
        
        
                      {


        
        
           88
        
                         vis[v] = 
        
          1
        
        
          ;


        
        
           89
        
                         count++
        
          ;


        
        
           90
        
        
                      }


        
        
           91
        
        
           92
        
                     indegree[u]++
        
          ;


        
        
           93
        
                     outdegree[v]++
        
          ;


        
        
           94
        
        
          int
        
         tu =
        
           find(u);


        
        
           95
        
        
          int
        
         tv =
        
           find(v);


        
        
           96
        
        
          if
        
        (tu !=
        
           tv)


        
        
           97
        
        
          set
        
        [tu] =
        
           tv;


        
        
           98
        
        
                  }


        
        
           99
        
        
          100
        
        
          if
        
        
          (check())


        
        
          101
        
                     printf(
        
          "
        
        
          Ordering is possible.\n
        
        
          "
        
        
          );


        
        
          102
        
        
          else
        
         printf(
        
          "
        
        
          The door cannot be opened.\n
        
        
          "
        
        
          );


        
        
          103
        
        
              }


        
        
          104
        
        
          return
        
        
          0
        
        
          ;


        
        
          105
        
         }
      
View Code
  

歐拉路 圖G,若存在一條路,經(jīng)過(guò)G中每條邊有且僅有一次,稱(chēng)這條路為歐拉 路, 如果存在一條回路經(jīng)過(guò)G每條邊有且僅有一次,

稱(chēng)這條回路為歐拉回路。具有歐拉回路的圖成為歐拉圖。

?

判斷 歐拉路 是否存在的方法

有向圖 :圖連通,有一個(gè)頂點(diǎn)出度大入度1,有一個(gè)頂點(diǎn)入度大出度1,其余都是出度=入度。

無(wú)向圖 :圖連通,只有兩個(gè)頂點(diǎn)是奇數(shù)度,其余都是偶數(shù)度的。

?

判斷 歐拉回路 是否存在的方法

有向圖 :圖連通,所有的頂點(diǎn)出度=入度。

無(wú)向圖 :圖連通,所有頂點(diǎn)都是偶數(shù)度。

其中判斷圖的連通性用并查集。

Play on Words(有向圖歐拉路)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国产欧美亚洲另类第一页 | 天天射天天干天天插 | 国产欧美高清 | 国产一级免费视频 | 日韩男人天堂 | 欧美精品国产日韩综合在线 | 亚洲欧洲日产国码二区首页 | 99re热精品视频国产免费 | 在线看片不卡 | 色八a级在线观看 | 国产一区二区三区免费在线观看 | 国产综合亚洲欧美日韩一区二区 | 日本免费的一级绿象 | 久草视频资源在线 | 欧美美妇性较大毛片 | 国产日韩精品欧美在线ccc | 日本在线视 | 暗香影院午夜国产精品 | 色综合啪啪| xxxx免费国产在线视频 | 欧美精品成人a多人在线观看 | 天天做夜夜做久久做狠狠 | 亚洲国产一区二区三区四区 | 公主恋人在线观看 | 国产在线一区二区三区在线 | 免费一级欧美大片在线观看 | 男人深夜影院 | 国产精品视频在线播放 | 亚洲一区二区三区网站 | 国产精品九九视频 | 免费深夜视频 | 久久剧场 | 亚洲第一区在线 | 久久99国产精品久久欧美 | 激情五月宗合网 | 成人毛片一区二区三区 | www.色av.com| 久久这里只精品热在线8 | 日本老年人精品久久中文字幕 | 成人在线观看网站 | 五月婷婷亚洲综合 |