HDOJ 4451 Dressing

Dressing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2484    Accepted Submission(s): 1076

Problem Description

Wangpeng has N clothes, M pants and K shoes so theoretically he can have N×M×K different combinations of dressing.

One day he wears his pants Nike, shoes Adiwang to go to school happily. When he opens the door, his mom asks him to come back and switch the dressing. Mom thinks that pants-shoes pair is disharmonious because Adiwang is much better than Nike. After being asked
to switch again and again Wangpeng figure out all the pairs mom thinks disharmonious. They can be only clothes-pants pairs or pants-shoes pairs.

Please calculate the number of different combinations of dressing under mom’s restriction.

Input

There are multiple test cases.

For each case, the first line contains 3 integers N,M,K(1≤N,M,K≤1000) indicating the number of clothes, pants and shoes.

Second line contains only one integer P(0≤P≤2000000) indicating the number of pairs which mom thinks disharmonious.

Next P lines each line will be one of the two forms“clothes x pants y” or “pants y shoes z”.

The first form indicates pair of x-th clothes and y-th pants is disharmonious(1≤x≤N,1 ≤y≤M), and second form indicates pair of y-th pants and z-th shoes is disharmonious(1≤y≤M,1≤z≤K).

Input ends with “0 0 0”.

It is guaranteed that all the pairs are different.

Output

For each case, output the answer in one line.

Sample Input

2 2 2
0
2 2 2
1
clothes 1 pants 1
2 2 2
2
clothes 1 pants 1
pants 1 shoes 1
0 0 0

Sample Output

8
6
5

Source

2012 Asia JinHua Regional Contest

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

int N,M,K;
int p;

int guanxi1[1200][1200],guanxi2[1200];

int main()
{
    while(scanf("%d%d%d",&N,&M,&K)!=EOF)
    {
        if(N==0&&M==0&&K==0) break;
        scanf("%d",&p);
        memset(guanxi1,0,sizeof(guanxi1));
        for(int i=1;i<=M;i++)
            guanxi2[i]=K;
        while(p--)
        {
            int u,v;
            char obj1[20],obj2[20];
            scanf("%s %d %s %d",obj1,&u,obj2,&v);

            if(obj1[0]=='p'&&obj2[0]=='s')
                guanxi2[u]--;
            else
            {
                guanxi1[u][v]=1;
            }
        }

        int ans=0;

        for(int i=1;i<=N;i++)
        {
            for(int j=1;j<=M;j++)
            {
                if(guanxi1[i][j]) continue;
                ans+=guanxi2[j];
            }
        }

        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-02 15:57:04

HDOJ 4451 Dressing的相关文章

hdu 4451 Dressing 衣服裤子鞋 简单容斥

Dressing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3735    Accepted Submission(s): 1681 Problem Description Wangpeng has N clothes, M pants and K shoes so theoretically he can have N×M×K

【HDU 4451 Dressing】水题,组合数

有衣服.裤子.鞋数量分别为n,m,k,给出p对不和谐的衣-裤或裤-鞋搭配,问一共有多少种和谐的衣裤鞋的搭配. 全部的组合有Cn1Cm1Ck1种. 设p对中有p1对衣-裤,p2对裤-鞋,则不和谐的搭配共有p1*Ck1+p2*Cn1种,但有被重复计算两次的搭配共p3对,它们引用了同一裤.设裤 i 在p1被引用 li 次,在p2被引用 ri 次,则p3=∑(1*Cli1Cri1).所以答案为n*m*k-p1*k-p2*n+p3 1 #include <cstdio> 2 #include <c

HDU 4451 Dressing

先从衣服处理到裤子,在从裤子处理到鞋子 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <map> #include <stri

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]