• <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è)計
            成長的路……源
            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個人分成2各組,每一個人有他所認(rèn)識的人。所分的組有四點(diǎn)要求:

            1、 每個人都必需屬于一個組。

            2、 每個組至少有一個人。

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

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

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

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

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

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

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

            5、 求出了所有的連通分量,對于第i個連通分量都把其節(jié)點(diǎn)分為兩個組s1is2i。這時要做的就是把所有的s1is2i分配到team1team2中去,使team1的總和與team2的總和差值最小。就可以用背包DP了。
            dp[i][j]
            表示第i個連通分量達(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
            同時記錄轉(zhuǎn)移路徑,差值j的范圍是-100~+100可以坐標(biāo)平移。
            最后在dp[m][i]m是最后一個連通塊數(shù))中找出值為true的最小i值。輸出答案。


            posted on 2008-08-08 00:51 飛飛 閱讀(3473) 評論(2)  編輯 收藏 引用 所屬分類: ACM/ICPC

            FeedBack:
            # re: POJ 1112 Team Them Up! 求補(bǔ)圖,連通分量,DP
            2008-09-04 08:47 | ssadwlll
            不愧是國防科技大學(xué)的大牛??!
              回復(fù)  更多評論
              
            # re: POJ 1112 Team Them Up! 求補(bǔ)圖,連通分量,DP
            2012-07-30 21:50 | Colleen
            nice  回復(fù)  更多評論
              
            人妻少妇久久中文字幕一区二区| 青青草原综合久久大伊人精品| 久久99精品久久久久久9蜜桃| 久久996热精品xxxx| 欧美一级久久久久久久大片| 国产激情久久久久影院小草| 久久精品国产亚洲7777| 99久久精品免费看国产一区二区三区 | 91久久香蕉国产熟女线看| 伊人久久亚洲综合影院| 久久久久亚洲AV无码麻豆| 手机看片久久高清国产日韩| 五月丁香综合激情六月久久| 久久久精品人妻无码专区不卡| 久久婷婷五月综合97色| 亚洲欧美久久久久9999| 久久精品无码一区二区三区| 99精品久久久久久久婷婷| 国产精品热久久无码av| 久久ww精品w免费人成| 伊人色综合久久天天网| 狠狠色综合网站久久久久久久| 久久久精品国产sm调教网站 | 久久精品国产亚洲AV电影 | 久久久久免费精品国产| 人妻精品久久无码区| 色播久久人人爽人人爽人人片AV| 久久国产成人午夜aⅴ影院| 亚洲成人精品久久| 色综合久久综合网观看| 久久国产精品一区二区| 99久久精品国内| 国产精品久久久久影院嫩草| 久久久久亚洲精品无码蜜桃| 亚洲综合日韩久久成人AV| 久久久久青草线蕉综合超碰| 日韩欧美亚洲综合久久| 久久国产欧美日韩精品免费| 久久久久这里只有精品| 2020久久精品亚洲热综合一本| 日本精品一区二区久久久|