BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解題報告

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   2014   div   

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4985

題目意思:有 n 個數,對於第 i 個數給出 σ(i) 的值。求出互不相交的迴圈的個數,並輸出每個迴圈包含的數字。

     還是不太懂吧?反正比賽的時候我也沒看懂 >__< !

     據說,這條題目用到了置換群 + 迴圈 的知識,黑書上 P246 有相應介紹。

     先來說明下這幅圖的意思,搞明白題意就發現這條題是很好做的。

     

     它表示:σ(1) = 2, σ(2) = 5, σ(3) = 4, σ(4) = 3, and σ(5) = 1

     首先要知道為什麼(1 2 5)和(3 4)要劃分開來。我們從數字 1 ~ n 遍曆,也就是上面的1 2 3 4 5 啦。那麼遍曆到 1 的時候,可以輸出 1 (設一個vis數組,開始時全部為0,輸出後vis[1] = 1),接著取σ(1) 也就是 2 啦,發現之前沒有輸出過(只輸出了 1 而已)那就繼續輸出 2 啦(vis[2] = 1),接著算出σ(2) = 5,輸出5(vis[5] = 1),然後發現 σ(5) = 1,但是vis[1] = 1已經輸出過,所以不輸出了。此時就發現1 2 5 是一個迴圈,就是說,通過σ(i)計算它只能在1 2 5 中取數。剩下的3 4 也是通過這樣算的。

    黑書上有一段是這樣介紹迴圈的:

    每個置換都可以寫成若干互不相交的迴圈的乘積,兩個迴圈(a1 a2 ...an) 和 (b1 b2 ...bn) 互不相交是指ai≠bj,i,j = 1, 2, ..., n。

    例如:

    

    是等於  (1 3 6)(2 5)(4)

    因為:1 —> 3 —> 6 —> 1 (迴圈了),2 —> 5 —> 2(迴圈了),4 —> 4 (自己迴圈自己)。

    

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5  6 const int maxn = 1e5 + 5; 7 int vis[maxn], a[maxn]; 8  9 int main()10 {11     int n;12     while (scanf("%d", &n) != EOF)13     {14         for (int i = 1; i <= n; i++)15             scanf("%d", &a[i]);16         memset(vis, 0, sizeof(vis));17         for (int i = 1; i <= n; i++)18         {19             if (vis[i])20                 continue;21             printf("(%d", i);  // 計算一個迴圈22             vis[i] = 1;23             int j = a[i];24             while (j != i)25             {26                 printf(" %d", j);27                 vis[j] = 1;28                 j = a[j];29             }30             printf(")");31         }32         printf("\n");33     }34     return 0;35 }

   

    注意:上面的初始狀態預設為 1 2 3 4...n 的

    對於一般情況的初始狀態(不一定是1 2 3 4...n),給出對應的目標狀態,要求分解為不相交的迴圈乘積的個數。例如初始狀態為 8 4 5 3 2 7,而目標狀態為 2 3 4 5 7 8,則可以分解為兩個迴圈的乘積,即(8 2 7)(4 3 5).代碼如下:

    

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5  6 const int maxn = 1e5 + 5; 7 int vis[maxn], a[maxn], b[maxn]; 8  9 int main()10 {11     int n;12     while (scanf("%d", &n) != EOF)13     {14         for (int i = 1; i <= n; i++)15             scanf("%d", &a[i]);16         int t;17         for (int i = 1; i <= n; i++)18         {19             scanf("%d", &t);20             b[a[i]] = t;21         }22     /*    for (int i = 1; i <= 8; i++)23             if (b[i])24                 printf("b[%d] = %d\n", i, b[i]);25     */26         memset(vis, 0, sizeof(vis));27         for (int i = 1; i <= n; i++)28         {29             int k = a[i];30             if (vis[k])31                 continue;32             printf("(%d", k);  // 計算一個迴圈33             vis[k] = 1;34             int j = b[k];35             while (j != k)36             {37                 printf(" %d", j);38                 vis[j] = 1;39                 j = b[j];40             }41             printf(")");42         }43         printf("\n");44 45     }46     return 0;47 }

    

BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解題報告

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.