一眼望去……好麻煩的樣子……
只會一一對應啊怎麼破怎麼破【好愁人啊、、、
然後,想想,是不是不要用map 做呢……看着對面的Empty大神,我默默放下了這種想法……
可是還是不會啊。。。Map同志的一對多【臣妾做不到啊啊啊啊……
好吧,百度一下…… 非人類們都說用set 的 multiset<int >
然後我應該就會了……
先是建立 map<int , multiset<int>>
,考慮到下面會經常出現這個東西, 就用typedef 定義了一下,嗯,隨手定義成了 mp
然後寫了一個方法,也是本題的核心算法: 每次查詢, 輸出對應的map 中的符合的元素數,然後刪除另一個map中對應的數據。
然後基礎的部分就是map的一般算法。
AC Memory : 12492 KB Time : 1359 MS
代碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <iterator>
using namespace std;
typedef map<int,multiset<int> > mp;
void pop(mp &a,mp &b,int i)
{
printf("%d\n",a[i].size());
for(multiset<int>::iterator it = a[i].begin();it!=a[i].end();++it)
{
b[*it].erase(i);
}
a[i].clear();
}
int main()
{
int N,M,x,y;
scanf("%d%d",&N,&M);
while(N!=0 &&M!= 0)
{
mp a;
mp b;
for(int i = 0;i<N;++i)
{
scanf("%d%d",&x,&y);
a[x].insert(y);
b[y].insert(x);
}
for(int i = 0;i<M;i++)
{
scanf("%d%d",&x,&y);
if(x==0)
{
pop(a,b,y);
}
else
{
pop(b,a,y);
}
}
cout<<endl;
scanf("%d%d",&N,&M);
}
return 0;
}