• <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>

            pku 3687 Labeling Balls 逆向拓?fù)?!注?/a>

            英文太短,直接貼

            Description

            Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that:

            1. No two balls share the same label.
            2. The labeling satisfies several constrains like "The ball labeled with a is lighter than the one labeled with b".

            Can you help windy to find a solution?


            Input

            The first line of input is the number of test case. The first line of each test case contains two integers, N (1 ≤ N ≤ 200) and M (0 ≤ M ≤ 40,000). The next M line each contain two integers a and b indicating the ball labeled with a must be lighter than the one labeled with b. (1 ≤ a, bN) There is a blank line before each test case.

            Output

            For each test case output on a single line the balls' weights from label 1 to label N. If several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight for label 2, then with the smallest weight for label 3 and so on... If no solution exists, output -1 instead.

            然后這題是一個裸的拓?fù)渑判颍禽敵鲇悬c忽悠人,要求輸出標(biāo)簽代表的球的重量,按照標(biāo)簽號排序,而且對于重復(fù)情況需要標(biāo)簽ID小的球的質(zhì)量盡量小。
            這里有一個錯誤折騰死人,如果按照正向拓?fù)洌o標(biāo)簽號小的標(biāo)簽分配給質(zhì)量小的球是不對的,應(yīng)為對于拓?fù)湫騺碚f,很多鏈?zhǔn)瞧叫械?,給鏈頭元素最小值的貪心策略并不能保證全局最小,如下圖兩條平行鏈,如果給3分配了較小質(zhì)量的球,1就得不到最小質(zhì)量的球,導(dǎo)致結(jié)果不滿組最優(yōu),但是如果逆向拓?fù)?,給鏈頭標(biāo)簽最大的分配最重的物體一定能保證正向拓?fù)湫蜃钚?/span>
            4 2 5
            3 1
             1 # include <cstdio>
             2 # include <queue>
             3 # include <vector>
             4 # include <cstring>
             5 using namespace std;
             6 priority_queue<int,vector<int>,less<int> >q;
             7 int g[205],degree[205];
             8 int nxt[50000],v[50000],c,n,m;
             9 int ans[205],num;
            10 int main()
            11 {
            12     int testcase;
            13     scanf("%d",&testcase);
            14     while(testcase--)
            15     {
            16         memset(g,-1,sizeof(g));
            17         memset(degree,0,sizeof(degree));
            18         c=num=0;
            19     while(!q.empty()) q.pop();
            20         scanf("%d%d",&n,&m);
            21         while(m--)
            22         {
            23             int a,b;
            24             scanf("%d%d",&a,&b);
            25             v[c]=a;
            26             nxt[c]=g[b];
            27             g[b]=c++;
            28         degree[a]++;
            29         }
            30         for(int i=1;i<=n;i++)
            31             if(!degree[i])
            32                 q.push(i);
            33     num=n;
            34         while(!q.empty())
            35         {
            36             int top=q.top();
            37             q.pop();
            38             ans[top]=num--;
            39             for(int p=g[top];p!=-1;p=nxt[p])
            40             {
            41                 degree[v[p]]--;
            42                 if(!degree[v[p]])
            43                     q.push(v[p]);
            44             }
            45         }
            46         if(num>0)
            47             printf("-1\n");
            48         else
            49         {
            50             printf("%d",ans[1]);
            51             for(int i=2;i<=n;i++)
            52           printf(" %d",ans[i]);
            53             printf("\n");
            54         }
            55     }
            56     return 0;
            57 }

            posted on 2010-10-22 02:24 yzhw 閱讀(153) 評論(0)  編輯 收藏 引用 所屬分類: graph

            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計

            公告

            統(tǒng)計系統(tǒng)

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            狠狠色丁香婷综合久久| 伊人久久成人成综合网222| 午夜福利91久久福利| 激情五月综合综合久久69| 夜夜亚洲天天久久| 国产精品视频久久| 97精品国产91久久久久久| 久久久av波多野一区二区| 亚洲精品国产字幕久久不卡| 久久精品视频一| 亚洲乱码中文字幕久久孕妇黑人| 日批日出水久久亚洲精品tv| 久久人人爽人人澡人人高潮AV| 四虎久久影院| 人妻精品久久无码区| 国产精品视频久久久| 亚洲午夜精品久久久久久人妖| 大香网伊人久久综合网2020| 久久99精品久久久久久9蜜桃 | 久久亚洲精品无码播放| 久久无码国产| 日韩精品久久久肉伦网站| 成人免费网站久久久| 久久国产成人午夜AV影院| 香蕉久久夜色精品国产尤物| 日韩AV无码久久一区二区 | 热RE99久久精品国产66热| 久久国产劲爆AV内射—百度| 久久综合香蕉国产蜜臀AV| 亚洲国产天堂久久综合网站| 思思久久好好热精品国产| 国产韩国精品一区二区三区久久 | 日韩人妻无码一区二区三区久久| 久久国产成人精品麻豆| 一本久久a久久精品综合香蕉 | 久久久久亚洲国产| 久久青青草原精品国产| 久久精品国产一区二区三区不卡| 国产亚洲精品久久久久秋霞| 99久久精品免费看国产免费| 亚洲国产精品无码久久一线|