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

            poj3687 Labeling Balls

            Labeling Balls


            Time Limit: 1000MS
            Memory Limit: 65536K
            Total Submissions: 7703
            Accepted: 2068

            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.

            Sample Input

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

            Sample Output

            1 2 3 4 -1 -1 2 1 3 4 1 3 2 4 

            Source

            POJ Founder Monthly Contest – 2008.08.31, windy7926778


            給n個球求編號,要求滿足m個關系
            關系 a,b表示  a的編號一定小于b的編號
            最后要求輸出所有編號對應的球號

            分析:
            請看這里
            http://imlazy.ycool.com/post.2144071.html

            最后一句話給了正確的貪心策略
                  小的頭部不一定排在前面,但是大的尾部一定排在后面

            開始一直中槍,題解中說的兩種情況都中

            最后終于發現是算法錯了

            唉,坑爹啊,這題目錯誤的算法樣例都過
            很丑的代碼
            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <ctime>
            #include 
            <cassert>
            #include 
            <iostream>
            #include 
            <sstream>
            #include 
            <fstream>
            #include 
            <map>
            #include 
            <set>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <algorithm>
            #include 
            <iomanip>
            using namespace std;
            #define maxn 210
            #define pp printf("here\n")
            bool mp[maxn][maxn],vis[maxn];
            int indre[maxn];
            bool q[maxn];
            int head,tail,n,m;
            struct point
            {
                
            int x,id;
            } ans[maxn];
            int num,num1,sec,se;
            void add(int x,int id)
            {
                se
            ++;
                ans[se].x
            =x;
                ans[se].id
            =id;
            }
            int cmp(point t1,point t2)
            {
                
            return t1.x<t2.x;
            }
            void topsort()
            {
                
            int i,j,tmp;
                
            bool flag;
                num
            =0;
                num1
            =0;
                sec
            =n;
                se
            =0;
                memset(q,
            0,sizeof(q));
                memset(vis,
            0,sizeof(vis));
                
            for(i=1; i<=n; i++)
                {
                    
            if (indre[i]==0)
                    {
                        vis[i]
            =1;
                        q[i]
            =1;
                        num
            ++;
                        num1
            ++;
                    }
                }
               
            // printf("%d\n",num1);
                while(num1)
                {
                    num1
            --;
                    tmp
            =n+1;
                    
            while(--tmp)
                    {
                        
            if(q[tmp]==1break;
                    }
                    q[tmp]
            =0;
                    add(tmp,sec);
                    sec
            --;
                    
            for(j=1; j<=n; j++)
                        
            if(mp[tmp][j]==1&&vis[j]==0)
                        {
                            indre[j]
            --;
                        }
                    
            for(j=1; j<=n; j++)
                        
            if(vis[j]==0&&indre[j]==0)
                        {
                            vis[j]
            =1;
                            q[j]
            =1;
                            num
            ++;//pp;
                            num1++;
                        }
                }
               
            // printf("%d\n",num);
                if(num!=n) printf("-1\n");
                
            else
                {
                    sort(ans
            +1,ans+n+1,cmp);
                    
            for(i=1; i<n; i++)
                        printf(
            "%d ",ans[i].id);
                    printf(
            "%d\n",ans[n].id);
                }
            }
            int main()
            {
                
            int x,y,t;
                scanf(
            "%d",&t);
                
            while(t--)
                {
                    scanf(
            "%d%d",&n,&m);
                    memset(mp,
            0,sizeof(mp));
                    memset(indre,
            0,sizeof(indre));
                    
            for(int i=1; i<=m; i++)
                    {
                        scanf(
            "%d%d",&x,&y);
                        
            if(!mp[y][x])
                        {
                            mp[y][x]
            =1;
                            indre[x]
            ++;
                        }
                    }
                    topsort();
                }
                
            return 0;
            }

            posted on 2012-07-13 15:34 jh818012 閱讀(178) 評論(0)  編輯 收藏 引用

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            国产69精品久久久久99| 国产农村妇女毛片精品久久| 久久91精品久久91综合| 国产精品亚洲美女久久久| 精品国产一区二区三区久久蜜臀| 人人妻久久人人澡人人爽人人精品| 精品久久久久久久久中文字幕| 久久99精品国产麻豆宅宅| 久久综合九色欧美综合狠狠 | 精品久久久久久综合日本| 日本一区精品久久久久影院| 久久亚洲AV无码精品色午夜| 国产精品久久久久久吹潮| 久久综合国产乱子伦精品免费| 久久国产精品免费| 久久久老熟女一区二区三区| 精品久久久无码人妻中文字幕| 久久亚洲AV无码西西人体| 久久超乳爆乳中文字幕| 久久亚洲sm情趣捆绑调教| 久久久久无码精品国产app| 久久久久久一区国产精品| 国产亚洲美女精品久久久久狼| 久久久久精品国产亚洲AV无码| 久久成人18免费网站| 久久亚洲高清观看| 韩国免费A级毛片久久| 丁香色欲久久久久久综合网| 久久精品国产AV一区二区三区| 久久久久噜噜噜亚洲熟女综合 | 色诱久久av| 久久国产精品无| 久久精品国产亚洲αv忘忧草 | 欧美性猛交xxxx免费看久久久| 97久久国产亚洲精品超碰热| 久久精品国产亚洲沈樵| 亚洲成色WWW久久网站| 狠狠色丁香久久婷婷综| 久久九九全国免费| 久久久国产乱子伦精品作者| 久久人妻少妇嫩草AV无码专区|