Dressing
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2674 accepted submission (s): 1179
Problem descriptionwangpeng 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.
Inputthere 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.
Outputfor each case, output the answer in one line.
Sample Input
2 2 202 2 21clothes 1 pants 12 2 22clothes 1 pants 1pants 1 shoes 10 0 0
Sample output
865
Source2012 Asia Jinhua Regional Contest
One of the questions that I did with my teammates this morning was not read before;
Idea: first record the unharmonious clothes and trousers and shoes, and then enumerate the clothes and trousers. If the clothes and trousers can be matched, check the shoes, and then accumulate the matching options.
AC code:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#include <stack>using namespace std;int a[1010][1010], b[1010][1010], c[1010];int main(){int N, M, K, P;char str1[10], str2[10];int t1, t2;while(scanf("%d %d %d", &N, &M, &K)==3, N || M || K){memset(a, 0, sizeof(a));memset(b, 0, sizeof(b));memset(c, 0, sizeof(c));scanf("%d", &P);while(P--) { scanf("%s %d %s %d", &str1, &t1, &str2, &t2); if(strcmp(str1,"clothes")==0) { a[t1][t2]=1; } else { b[t1][t2]=1; c[t1]++; } } int ans=0; for(int i=1;i<=N;i++) for(int j=1;j<=M;j++) if(a[i][j]==0) ans+=(K-c[j]); printf("%d\n",ans);}return 0;}
HDU-4451-Dressing (2012 Jinhua division J)