HDU4451——Dressing

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
 
/*
像容斥原理,总和减去分别再加上重复减去的
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1005;
int p1[MAX], p2[MAX];
int main()
{
    int n, m , k, p;
    int t1, t2;
    char s1[10], s2[10];
    while(~scanf("%d%d%d", &n, &m, &k)){
        memset(p1, 0, sizeof(p1));
        memset(p2, 0, sizeof(p2));
        if(n == 0 && m == 0 && k == 0) break;
        scanf("%d", &p);
        int sum = m * k * n;
        for(int i = 1; i <= p ; i++){
            scanf("%s%d%s%d", s1, &t1, s2, &t2);
            if(s1[0] == 'c'){
                 p1[t2] ++;
                sum -= k;
            }
            else if(s1[0] == 'p' ){
                p2[t1] ++; 
                sum -= n;
            }
        }
        int sum1 = 0;
        for(int i = 1; i <= m; i++){
              sum1 += p1[i] * p2[i];
        }
        sum += sum1;
        printf("%d
",sum);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zero-begin/p/4655770.html