VJ_Dressing_思维
VJ_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
Inputcopy | Outputcopy |
---|---|
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 | 8 6 5 |
// VJ_Dressing_思维
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1111;
int ab[N],bc[N];
char s1[N],s2[N];
signed main()
{
int a,b,c,n,x,y,ans;
while( ~scanf("%lld%lld%lld",&a,&b,&c ) && ( a+b+c ) )
{
ans=a*b*c;
memset( ab,0,sizeof( ab ) );
memset( bc,0,sizeof( bc ) );
scanf("%lld",&n );
while( n-- )
{
scanf("%s %lld %s %lld",s1,&x,s2,&y );
if( s1[0]=='c' ) { ab[y]++; ans+=bc[y]-c; } // y_is_b
else { bc[x]++; ans+=ab[x]-a; } // x_is_b
}
printf("%lld\n",ans );
}
return 0;
}
// 01 推导公式
// 抽象实例化 暴力确定实例的结果 由果导因 猜测公式
// a(3) b(4) c(5)
// n(5)
// ab12 //
// ab22
// bc23 //
// bc24
// bc25
// 2*3 重复
// 02 降低时间复杂度
// 保存+遍历 时间复杂度太高
// 解决: 有没有一边读取一边计算的方法?
// 最终的去重 是cnt_b1 cnt_b2的数量积
// 那么每次cnt++ 实际上就是对cnt'进行了去重 (cnt cnt'是对立面)