青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Google code jam 2008 R1A - Milkshakes

Problem

You own a milkshake shop. There are N different flavors that you can prepare, and each flavor can be prepared "malted" or "unmalted". So, you can make 2N different types of milkshakes.

Each of your customers has a set of milkshake types that they like, and they will be satisfied if you have at least one of those types prepared. At most one of the types a customer likes will be a "malted" flavor.

You want to make N batches of milkshakes, so that:

  • There is exactly one batch for each flavor of milkshake, and it is either malted or unmalted.
  • For each customer, you make at least one milkshake type that they like.
  • The minimum possible number of batches are malted.

Find whether it is possible to satisfy all your customers given these constraints, and if it is, what milkshake types you should make.

If it is possible to satisfy all your customers, there will be only one answer which minimizes the number of malted batches.

Input

  • One line containing an integer C, the number of test cases in the input file.

For each test case, there will be:

  • One line containing the integer N, the number of milkshake flavors.
  • One line containing the integer M, the number of customers.
  • M lines, one for each customer, each containing:
    • An integer T >= 1, the number of milkshake types the customer likes, followed by
    • T pairs of integers "X Y", one for each type the customer likes, where X is the milkshake flavor between 1 and N inclusive, and Y is either 0 to indicate unmalted, or 1 to indicated malted. Note that:
      • No pair will occur more than once for a single customer.
      • Each customer will have at least one flavor that they like (T >= 1).
      • Each customer will like at most one malted flavor. (At most one pair for each customer has Y = 1).
    All of these numbers are separated by single spaces.

Output

  • C lines, one for each test case in the order they occur in the input file, each containing the string "Case #X: " where X is the number of the test case, starting from 1, followed by:
    • The string "IMPOSSIBLE", if the customers' preferences cannot be satisfied; OR
    • N space-separated integers, one for each flavor from 1 to N, which are 0 if the corresponding flavor should be prepared unmalted, and 1 if it should be malted.

Limits

Small dataset

C = 100
1 <= N <= 10
1 <= M <= 100

Large dataset

C = 5
1 <= N <= 2000
1 <= M <= 2000

The sum of all the T values for the customers in a test case will not exceed 3000.

Sample


Input
 

Output
 
2
5
3
1 1 1
2 1 0 2 0
1 5 0
1
2
1 1 0
1 1 1
Case #1: 1 0 0 0 0
Case #2: IMPOSSIBLE

In the first case, you must make flavor #1 malted, to satisfy the first customer. Every other flavor can be unmalted. The second customer is satisfied by getting flavor #2 unmalted, and the third customer is satisfied by getting flavor #5 unmalted.

In the second case, there is only one flavor. One of your customers wants it malted and one wants it unmalted. You cannot satisfy them both.

Analysis
On the surface, this problem appears to require solving the classic problem "Satisfiability," the canonical example of an NP-complete problem. The customers represent clauses, the milkshake flavors represent variables, and malted and unmalted flavors represent whether the variable is negated.

We are not evil enough to have chosen a problem that hard! The restriction that makes this problem easier is that the customers can only like at most one malted flavor (or equivalently, the clauses can only have at most one negated variable.)

Using the following steps, we can quickly find whether a solution exists, and if so, what the solution is.

  1. Start with every flavor unmalted and consider the customers one by one.
  2. If there is an unsatisfied customer who only likes unmalted flavors, and all those flavors have been made malted, then no solution is possible.
  3. If there is an unsatisfied customer who has one favorite malted flavor, then we must make that flavor malted. We do this, then go back to step 2.
  4. If there are no unsatisfied customers, then we already have a valid solution and can leave the remaining flavors unmalted.

Notice that whenever we made a flavor malted, we were forced to do so. Therefore, the solution we got must have the minimum possible number of malted flavors.

With clever data structures, the above algorithm can be implemented to run in linear time.

More information:

The Satisfiability problem - Horn clauses


Source Code
#include <iostream>

using namespace std;

#define Rep(i,n) for (int i(0),_n(n); i<_n; ++i)

struct Flavor{
    
int X;
    
char Y;
}
;

struct Customer{
    
int T;
    Flavor
* F;
    Customer() 
{
        F 
= NULL;
    }

    
~Customer() {
        
if(NULL!=F) {
            delete[] F;
            F 
= NULL;
        }

    }

    
void Init(int t) {
        T 
= t;
        F 
= new Flavor[T];
    }

    
void SetFlavor(int i, int X, int Y) {
        F[i].X 
= X;
        F[i].Y 
= Y;
    }

    
int GetFlavorX(int i) {
        
return F[i].X;
    }

    
int GetFlavorY(int i) {
        
return F[i].Y;
    }

    
bool IsSatisfied() {
        
return T==0;
    }

    
void Satisfy() {
        T
=0;
        
if(NULL!=F) {
            delete[] F;
            F 
= NULL;
        }

    }

    
bool IsSatisfing(int i, int *f) {
        
return f[F[i].X]==F[i].Y;
    }

    
void SetMalted(int i, int *f) {
        f[F[i].X] 
= 1;
    }

}
;

int main()
{
    
int C;
    FILE 
*fp = fopen("A.out""w");
    scanf(
"%d"&C);
    Rep(c, C) 
{
        
int N;
        scanf(
"%d"&N);
        
int* f = new int[N+1];
        Rep(i ,N
+1{
            f[i]
=0;
        }

        
int M;
        scanf(
"%d"&M);
        Customer
* customer = new Customer[M];
        Rep(m, M) 
{
            
int T;
            scanf(
"%d"&T);
            customer[m].Init(T);
            Rep(t, T) 
{
                
int X, Y;
                scanf(
"%d%d"&X,&Y);
                customer[m].SetFlavor(t,X,Y);
            }

        }

        
bool findSolution = true;
        
int m = 0;
        
while(m<M) {
            
if(customer[m].IsSatisfied()) {
                m
++;
                
continue;
            }

            
bool malted = false;
            
int idx;
            
bool satisfied = false;
            Rep(t, customer[m].T) 
{
                
if(customer[m].GetFlavorY(t)==1{
                    malted 
= true;
                    idx 
= t;
                }

                
if(customer[m].IsSatisfing(t,f)) {
                    satisfied 
= true;
                }

            }

            
if(!satisfied) {
                
if(malted) {
                    customer[m].SetMalted(idx,f);
                    customer[m].Satisfy();
                    m
=0;
                }
 else {
                    findSolution 
= false;
                    
break;
                }

            }
 else {
                m
++;
            }

        }

        fprintf(fp,
"Case #%d: ", c+1);
        
if(findSolution) {
            Rep(i ,N) 
{
                fprintf(fp,
"%d ", f[i+1]);
            }

            fprintf(fp,
"\n");
        }
 else {
            fprintf(fp,
"IMPOSSIBLE\n");
        }

        delete[] customer;
        delete[] f;

    }

    fclose(fp);
}

posted on 2009-08-12 21:17 Chauncey 閱讀(422) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


導航

<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

統計

常用鏈接

留言簿

隨筆檔案(4)

文章檔案(3)

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产精品成人一区二区三区吃奶 | 国语自产偷拍精品视频偷| 久久久久久久尹人综合网亚洲 | 9国产精品视频| 久久精品人人| 亚洲欧洲av一区二区| 欧美激情在线| 欧美激情欧美狂野欧美精品| 国产一区二区三区久久悠悠色av| 在线视频精品一区| 日韩天堂av| 欧美激情1区2区3区| 六月婷婷一区| 一区二区在线观看视频| 性欧美激情精品| 午夜免费日韩视频| 国产精品swag| 亚洲视频在线免费观看| 亚洲午夜在线视频| 欧美日韩中文在线| 一本色道久久88亚洲综合88| 日韩手机在线导航| 欧美理论在线播放| 日韩网站在线看片你懂的| 日韩亚洲欧美一区二区三区| 欧美成人午夜影院| 91久久精品一区| 亚洲破处大片| 欧美日韩国产小视频在线观看| 亚洲国产高潮在线观看| 亚洲人成毛片在线播放| 免费一区视频| 亚洲日本欧美在线| 亚洲视频免费观看| 欧美日韩在线一区二区| 亚洲一区二区三区中文字幕在线| 亚洲欧美另类国产| 国产亚洲欧美一区二区| 久久国产精品久久精品国产| 美女图片一区二区| 亚洲日本电影| 欧美日韩一区二区高清| 亚洲天堂网站在线观看视频| 久久精品91久久香蕉加勒比| 国产最新精品精品你懂的| 久久婷婷国产综合精品青草| 欧美激情综合色| 日韩午夜在线| 国产精品一区二区在线| 久久精品国产99国产精品| 亚洲成人自拍视频| 亚洲女人小视频在线观看| 国产日韩精品在线观看| 久久一日本道色综合久久| 日韩视频免费观看| 久久精品国产99精品国产亚洲性色 | 欧美二区乱c少妇| 日韩午夜激情av| 久久国产色av| 99精品国产高清一区二区| 国产精品男人爽免费视频1| 久久精品国产99国产精品| 亚洲国产精品久久91精品| 午夜视频一区二区| 91久久在线| 国产欧美一区二区三区另类精品| 久色成人在线| 亚洲欧美日韩区| 最新69国产成人精品视频免费| 性欧美精品高清| 亚洲精品中文字幕在线| 国产欧美日韩综合一区在线播放| 麻豆精品视频在线| 午夜精品久久99蜜桃的功能介绍| 亚洲第一精品夜夜躁人人爽| 欧美一区国产二区| 99视频有精品| 亚洲第一精品在线| 国产亚洲免费的视频看| 欧美图区在线视频| 蜜臀久久99精品久久久画质超高清 | 伊人夜夜躁av伊人久久| 国产精品久久久对白| 欧美二区在线看| 久久丁香综合五月国产三级网站| 一本色道久久88亚洲综合88| 亚洲大胆人体视频| 久久乐国产精品| 欧美一区二区精品| 亚洲在线播放| 一本色道久久综合一区| 91久久国产自产拍夜夜嗨| 国语对白精品一区二区| 国产精品自拍一区| 欧美午夜女人视频在线| 欧美肥婆bbw| 免费欧美日韩| 久久综合成人精品亚洲另类欧美| 欧美在线播放高清精品| 亚洲欧美另类久久久精品2019| 99国内精品久久| 99国产一区| 99re热这里只有精品免费视频| 亚洲国产乱码最新视频| 欧美激情偷拍| 免费亚洲一区二区| 欧美r片在线| 蜜臀久久99精品久久久久久9 | 久热精品视频| 久久久久免费视频| 久久久一区二区三区| 久久久久9999亚洲精品| 久久嫩草精品久久久久| 久久久亚洲一区| 噜噜噜91成人网| 欧美激情精品久久久| 亚洲第一视频| 亚洲欧洲视频| 一本色道久久综合亚洲精品不卡| 日韩视频在线一区二区三区| 日韩亚洲欧美高清| 亚洲一区二区视频在线观看| 亚洲欧美日韩精品综合在线观看| 小处雏高清一区二区三区| 久久av资源网站| 久久综合色播五月| 欧美激情视频一区二区三区在线播放 | 久久亚洲一区二区| 欧美成人精品h版在线观看| 欧美理论视频| 国产精品久久亚洲7777| 国产一区二区三区四区老人| 在线成人欧美| 一本久久a久久免费精品不卡| 亚洲网在线观看| 欧美一区二区三区日韩视频| 久久日韩粉嫩一区二区三区| 欧美成人一区二区三区在线观看| 亚洲破处大片| 午夜精品国产更新| 女人香蕉久久**毛片精品| 欧美体内谢she精2性欧美| 国产亚洲一区在线| 亚洲精品久久久久久久久久久久| 亚洲一区二三| 免费亚洲电影在线| 9人人澡人人爽人人精品| 性刺激综合网| 欧美精品久久天天躁| 国产精品日韩在线一区| 亚洲福利视频二区| 亚洲欧美国产精品专区久久| 毛片av中文字幕一区二区| 日韩视频在线一区二区三区| 久久久91精品| 国产精品乱人伦一区二区| 亚洲国产精品第一区二区三区| 亚洲欧美日韩直播| 亚洲第一福利社区| 性做久久久久久久免费看| 欧美日韩三级一区二区| 尤物精品在线| 欧美在线亚洲一区| 亚洲精品乱码久久久久久| 久久久噜噜噜久久中文字免| 欧美性久久久| 99国产精品99久久久久久粉嫩| 久久久夜精品| 亚洲一区二区欧美| 欧美巨乳在线| 亚洲国产日韩一级| 久久九九精品99国产精品| 一本色道久久88综合亚洲精品ⅰ | 欧美女人交a| 亚洲国产高清自拍| 久久米奇亚洲| 欧美一级专区免费大片| 国产精品久久久久久久久久免费 | 亚洲黄色一区二区三区| 久久久www成人免费毛片麻豆| 国产精品欧美在线| 一区二区三区精品| 亚洲国产影院| 久久综合五月| 在线播放日韩欧美| 狂野欧美性猛交xxxx巴西| 性色一区二区三区| 国产欧美91| 欧美亚洲一区| 亚洲欧美日韩网| 国产日韩欧美高清| 久久成人免费| 午夜伦欧美伦电影理论片| 国产精品午夜av在线| 午夜亚洲精品| 亚洲欧美日韩精品久久奇米色影视| 欧美日韩亚洲一区三区| 在线性视频日韩欧美| 亚洲美女性视频| 国产精品v片在线观看不卡|