1002 Bombing The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
本题的思路就是将点映射一下,一次加入map,坐标值变成映射值,这样在利用静态链表进行处理,因为有可能横着炸或者竖着炸,所以结构体里加了一个标记is,防止重复统计,下面的应该很好理解了。
#include <iostream>
#include <string.h>
#include <map>
#include <stdio.h>
using namespace std;
map<int,int> mappp1,mappp2;
int n1,n2,k,k1;
int get1(int x){
if (mappp1.find(x)==mappp1.end()){
mappp1.insert(make_pair(x,n1));
n1++;
return n1-1;
}else return mappp1[x];
}
int get2(int x){
if (mappp2.find(x)==mappp2.end()){
mappp2.insert(make_pair(x,n2));
n2++;
return n2-1;
}else return mappp2[x];
}
int eHd[100005],eHd1[100005];
struct{
int v,next;
bool is;
}edge[100005],edge1[100005];
void add_edge(int u,int v){
edge[k].v=v;
edge[k].is=1;
edge[k].next=eHd[u];
eHd[u]=k++;
edge1[k1].v=u;
edge1[k1].is=1;
edge1[k1].next=eHd1[v];
eHd1[v]=k1++;
}
void init(){
memset(eHd1,-1,sizeof(eHd1));
memset(eHd,-1,sizeof(eHd));
k=0;
k1=0;
n1=0;
n2=0;
mappp1.clear();
mappp2.clear();
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF&&n+m){
init();
for(int i=0;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add_edge(get1(x),get2(y));
}
int a,b,out;
for(int mm=0;mm<m;mm++){
out=0;
scanf("%d%d",&a,&b);
if(a==0){
for(int i=eHd[get1(b)];i!=-1;i=edge[i].next){
if(edge[i].is){
out++;
edge[i].is=0;
edge1[i].is=0;
}
}
}
else{
for(int i=eHd1[get2(b)];i!=-1;i=edge1[i].next){
if(edge1[i].is){
out++;
edge[i].is=0;
edge1[i].is=0;
}
}
}
printf("%d\n",out);
}
printf("\n");
}
return 0;
}