hdu 4022(map一对多)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4022

STL应用,一开始用multimap写的,然后实现不了,于是就看了别人用map自定义了一个一对多。。。orz。。。

剩下的就简单了。。分别用两个这样的map容器装下 x为键,y为值,和y为键,x为值的两个容器。 然后当C==0就输出当键位d的容器大小,并且去掉另一个容器中出现的点。

同理C==1一样。

View Code
 1 #include<iostream>
 2 #include<map>
 3 #include<set>
 4 const int N=100010;
 5 using namespace std;
 6 int n,m;
 7 typedef map<int,multiset<int> >mp;
 8 multiset<int>::iterator iter;
 9 
10 int main(){
11     while(~scanf("%d%d",&n,&m)){
12         if(n==0&&m==0)break;
13         mp mp1,mp2;
14         int x,y,c,d,_count;
15         for(int i=0;i<n;i++){
16             scanf("%d%d",&x,&y);
17             mp1[x].insert(y);
18             mp2[y].insert(x);
19         }
20         for(int i=0;i<m;i++){
21             scanf("%d%d",&c,&d);
22             _count=0;
23             if(c==0){
24                 _count=mp1[d].size();
25                 for(iter=mp1[d].begin();iter!=mp1[d].end();iter++){
26                     mp2[*iter].erase(d);
27                 }
28                 mp1[d].clear();
29             }else if(c==1){
30                 _count=mp2[d].size();
31                 for(iter=mp2[d].begin();iter!=mp2[d].end();iter++){
32                     mp1[*iter].erase(d);
33                 }
34                 mp2[d].clear();
35             }
36             printf("%d\n",_count);
37         }
38         printf("\n");
39     }
40     return 0;
41 }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值