POJ2149

易水人去,明月如霜。

Description

If a tree falls in the forest, and there's nobody there to hear, does it make a sound? This classic conundrum was coined by George Berkeley (1685-1753), the Bishop and influential Irish philosopher whose primary philosophical achievement is the advancement of what has come to be called subjective idealism. He wrote a number of works, of which the most widely-read are Treatise Concerning the Principles of Human Knowledge (1710) and Three Dialogues between Hylas and Philonous (1713) (Philonous, the "lover of the mind," representing Berkeley himself).

Input

A forest contains T trees numbered from 1 to T and P people numbered from 1 to P. Standard input consists of a line containing P and T followed by several lines, containing a pair of integers i and j, indicating that person i has heard tree j fall.

Output

People may have different opinions as to which trees, according to Berkeley, have made a sound. Output 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. You may assume that P < 100 and T < 100.

Sample Input

3 4
1 2
3 3
1 3
2 2
3 2
2 4

Sample Output

2
哈希,求集合的大小

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct tree {
       int dep,s;
}a[2000],b[2000];
bool cmp(tree a,tree b) {
     return (a.s<b.s || (a.s==b.s && a.dep<b.dep));
}
int main() {
    string s;
    int n,cnt,i,top,t,d[2000];
    cin >> n;
    while (n--) {
          cin >> s;
          cnt=1;
          memset(d,0,sizeof(d));
          top=1;
          d[top]=1;
          a[cnt].dep=1;
          a[cnt].s=1;
          for (i=0;i<s.length();i++)
              if (s[i]=='0') {
                             a[++cnt].dep=a[d[top]].dep+1;
                             a[cnt].s=1;
                             d[++top]=cnt;
              }
              else {
                   t=a[d[top]].s;
                   a[d[--top]].s+=t;
              }
          cin >> s;
          cnt=1;
          memset(d,0,sizeof(d));
          top=1;
          d[top]=1;
          b[cnt].dep=1;
          b[cnt].s=1;
          for (i=0;i<s.length();i++)
              if (s[i]=='0') {
                             b[++cnt].dep=b[d[top]].dep+1;
                             b[cnt].s=1;
                             d[++top]=cnt;
              }
              else {
                   t=b[d[top]].s;
                   b[d[--top]].s+=t;
              }
          sort(a+1,a+cnt+1,cmp);
          sort(b+1,b+cnt+1,cmp);
          bool flag=true;
          for (i=1;i<=cnt;i++)
              if (a[i].s!=b[i].s || a[i].dep!=b[i].dep) flag=false;
          if (flag) printf("same/n");
          else printf("different/n");

    }
    //system("pause");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值