Problem 1354 Forests.
题意
- How many different opinions are represented in the input?
Two people hold the same opinion only if they hear exactly the same set of trees.
- 输入:
人数P,树的数目T(均小于100)
随后若干行i、j,表示第 i 个人听到了第 j 颗树倒下
i=j=0结束输入
- 输出:
问:关于“哪些树倒下了有几种不同的观点?”
输出不同观点的数量
思路
- 二维数组opinion[][],opinion[i]表示i的观点,opinion[i][j]=1表示第i个人的观点里树j倒下了
- 总观点数=人数,遍历opinion,出现相同的观点则观点数ans-1,并做标记,不重复检测这个已经相同的观点
代码
#include<cstdio>
#include<string.h>
using namespace std;
int opinion[100][100];
int flag[100];
int main(){
int p, t, i, j, ans;
while(scanf("%d%d", &p, &t)!=EOF){
memset(opinion, 0, sizeof(opinion));
memset(flag, 0, sizeof(flag));
scanf("%d%d", &i, &j);
while(i!=0 && j!=0){
opinion[i][j] = 1;
scanf("%d%d", &i, &j);
}
ans = p;
int temp;
for(i=1; i<p; i++){
if(flag[i])
continue;
for(j=i+1; j<=p; j++){
temp = 0;
for(int k=1; k<=t; k++){
if(opinion[i][k]!=opinion[j][k]){
temp = 1;
break;
}
}
if(temp==0){
ans--;
flag[j] = 1;
}
}
}
printf("%d\n", ans);
}
return 0;
}