• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            alpc60 ACM/ICPC程序設(shè)計(jì)
            成長(zhǎng)的路……源
            posts - 20,comments - 42,trackbacks - 0
             

            Team Them Up!

            Time Limit: 1000MS

             

            Memory Limit: 10000K

            Total Submissions: 1440

             

            Accepted: 358

             

            Special Judge

            Description

            Your task is to divide a number of persons into two teams, in such a way, that:

            everyone belongs to one of the teams;

            every team has at least one member;

            every person in the team knows every other person in his team;

            teams are as close in their sizes as possible.

            This task may have many solutions. You are to find and output any solution, or to report that the solution does not exist.

            Input

            For simplicity, all persons are assigned a unique integer identifier from 1 to N.

            The first line in the input file contains a single integer number N (2 <= N <= 100) - the total number of persons to divide into teams, followed by N lines - one line per person in ascending order of their identifiers. Each line contains the list of distinct numbers Aij (1 <= Aij <= N, Aij != i) separated by spaces. The list represents identifiers of persons that ith person knows. The list is terminated by 0.

            Output

            If the solution to the problem does not exist, then write a single message "No solution" (without quotes) to the output file. Otherwise write a solution on two lines. On the first line of the output file write the number of persons in the first team, followed by the identifiers of persons in the first team, placing one space before each identifier. On the second line describe the second team in the same way. You may write teams and identifiers of persons in a team in any order.

            Sample Input

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

            Sample Output

            3 1 3 5
            2 2 4

            Source

            Northeastern Europe 2001

             

             

            題目大意:把n個(gè)人分成2各組,每一個(gè)人有他所認(rèn)識(shí)的人。所分的組有四點(diǎn)要求:

            1、 每個(gè)人都必需屬于一個(gè)組。

            2、 每個(gè)組至少有一個(gè)人。

            3、 每個(gè)組里面的每個(gè)人必需互相認(rèn)識(shí)。

            4、 兩個(gè)組的成員應(yīng)盡量接近。

            首先分析這道題目,題目給出的是一個(gè)有向圖,即如果有A認(rèn)識(shí)B,但不一定有B認(rèn)識(shí)A。但是在所分配的組里面,任意兩個(gè)人都要互相認(rèn)識(shí)。

            1、 先讀入數(shù)據(jù)建立有向圖,然后對(duì)這個(gè)有向圖進(jìn)行處理,如果兩個(gè)點(diǎn)之間的邊是單向邊,就認(rèn)為兩個(gè)點(diǎn)之間無(wú)邊(因?yàn)檫@兩個(gè)人不互相認(rèn)識(shí)),對(duì)于兩個(gè)點(diǎn)間的雙向邊,即建立一條無(wú)向邊(這兩個(gè)人互相認(rèn)識(shí)),這樣就可以把一個(gè)有向圖轉(zhuǎn)化為一個(gè)無(wú)向圖。

            2、 將這個(gè)無(wú)向圖轉(zhuǎn)化為它的反圖。即有邊的把邊刪去,無(wú)邊的添上一條邊。(其實(shí)12步在程序?qū)崿F(xiàn)時(shí)可以一次完成)。

            3、 對(duì)轉(zhuǎn)換后的反圖求極大連通分量。想想就會(huì)明白剛才為什么要求反圖,可以看到在這個(gè)反圖中的不同的連通分量中的兩個(gè)人都是互相認(rèn)識(shí)的!!!接下來(lái)很關(guān)鍵,那些互不認(rèn)識(shí)的人就在一個(gè)連通分量里面。

            4、 在做DFS求連通分量的時(shí)候,同時(shí)對(duì)連通分量中的點(diǎn)染色(01),如果一個(gè)點(diǎn)顏色為0,那么所有和它相鄰的點(diǎn)與它的顏色應(yīng)該不同(標(biāo)記為1)。這是因?yàn)榉磮D中相鄰的兩個(gè)人都是不認(rèn)識(shí)的(回顧剛才反圖的作用)。這樣DFS結(jié)束后,就可以根據(jù)顏色把連通分量中的點(diǎn)分成兩個(gè)組s0s1,在不同兩個(gè)組的人是不可能分到一個(gè)team內(nèi)的。到這里要做一個(gè)特判,對(duì)于s0s1組里的任意兩個(gè)人p,q如果反圖中存在一條邊(p,q),說(shuō)明無(wú)解,輸出"No solution"

            5、 求出了所有的連通分量,對(duì)于第i個(gè)連通分量都把其節(jié)點(diǎn)分為兩個(gè)組s1is2i。這時(shí)要做的就是把所有的s1is2i分配到team1team2中去,使team1的總和與team2的總和差值最小。就可以用背包DP了。
            dp[i][j]
            表示第i個(gè)連通分量達(dá)到j的差值,true為可達(dá),false為不可達(dá)。
            狀態(tài)轉(zhuǎn)移方程:
            dp[i][j+si0-si1]=true if dp[i-1][j] == true
            dp[i][j-si0+si1]=true if dp[i-1][j] == true
            同時(shí)記錄轉(zhuǎn)移路徑,差值j的范圍是-100~+100可以坐標(biāo)平移。
            最后在dp[m][i]m是最后一個(gè)連通塊數(shù))中找出值為true的最小i值。輸出答案。


            posted on 2008-08-08 00:51 飛飛 閱讀(3453) 評(píng)論(2)  編輯 收藏 引用 所屬分類(lèi): ACM/ICPC

            FeedBack:
            # re: POJ 1112 Team Them Up! 求補(bǔ)圖,連通分量,DP
            2008-09-04 08:47 | ssadwlll
            不愧是國(guó)防科技大學(xué)的大牛!!
              回復(fù)  更多評(píng)論
              
            # re: POJ 1112 Team Them Up! 求補(bǔ)圖,連通分量,DP
            2012-07-30 21:50 | Colleen
            国产精品伦理久久久久久| 99久久香蕉国产线看观香| 精品久久久久久久国产潘金莲 | www.久久精品| 中文字幕久久精品无码| 色婷婷综合久久久久中文字幕| 国产精品丝袜久久久久久不卡 | 国产日韩久久久精品影院首页| 97久久香蕉国产线看观看| 欧美一区二区三区久久综| 欧洲成人午夜精品无码区久久| 日本欧美久久久久免费播放网| 午夜人妻久久久久久久久| 日韩乱码人妻无码中文字幕久久 | 国产精品美女久久久久久2018| 亚洲精品乱码久久久久久中文字幕| 无码人妻久久一区二区三区蜜桃| 国产精品久久久久久久app| 一本综合久久国产二区| 久久99精品国产麻豆宅宅| 久久精品国产精品亚洲精品| 亚洲中文久久精品无码ww16| 久久久久99精品成人片试看| 国产综合久久久久| 亚洲伊人久久大香线蕉苏妲己| 久久噜噜电影你懂的| 久久se精品一区精品二区国产| 色偷偷88欧美精品久久久| 久久久久人妻一区精品性色av| 久久精品国产精品青草| 久久久久亚洲AV成人网| 欧美久久久久久| 99精品国产在热久久无毒不卡 | 久久精品国产久精国产思思| 99久久超碰中文字幕伊人| 国产一区二区精品久久岳| 中文字幕无码av激情不卡久久| 亚洲第一极品精品无码久久| 麻豆精品久久久一区二区| 久久久久国产成人精品亚洲午夜| 无码国内精品久久综合88|