#include <iostream> #include <queue> #include <time.h> #include <windows.h> using namespace std; const int need = 3125010; //足够10000 * 10000 个位 const int col=10000; unsigned int G[need],isV[need]; //图G , 访问标记isV int n,k; int cnt; priority_queue<int> topK; int dir[4]; // up ,down, left ,right int dfs(int cur) { cnt=0; queue<int> q; q.push(cur); isV[cur/32]|=(1<<(cur%32)); while(!q.empty()) { int c=q.front(); q.pop(); cnt++; memset(dir,-1,sizeof(dir)); if(c-col>=0) { dir[0]=c-col; } if(c+col<col*col) { dir[1]=c+col; } if(c-1>=0&&c/col==(c-1)/col) { dir[2]=c-1; } if(c+1<col*col&&c/col==(c+1)/col) { dir[3]=c+1; } for(int i=0;i<4;++i) { if(dir[i]==-1) { continue; } unsigned int a=isV[dir[i]/32] ; unsigned int b=(1<<(dir[i]%32)); unsigned int c=G[dir[i]/32]; unsigned int d=a&b; unsigned int e=c&b; if( (d ==0 ) && (e !=0 ) ) { isV[dir[i]/32]|=(1<<(dir[i]%32)); q.push(dir[i]); } } } return cnt; } void dfsG() { int ret; for(int i=0;i<col*col;++i) { unsigned int a=isV[i/32] ; unsigned int b=(1<<(i%32)); unsigned int c=G[i/32]; unsigned int d=a&b; unsigned int e=c&b; if( (d==0) && (e!=0) ) { ret=dfs(i); topK.push(ret); } } } int main() { memset(G,0,sizeof(G)); memset(isV,0,sizeof(isV)); cin>>n>>k; int a,b,p; for(int i=0;i<n;++i) { cin>>a>>b; --a;--b; p=a*col+b; G[p/32]|=(1<<(p%32)); } //测试时间 int st=clock(); dfsG(); int sum=0; for(int j=0;j<k&&!topK.empty();++j) { sum+=topK.top(); topK.pop(); } cout<<sum<<endl; int ed=clock(); cout<<"时间:"<<((double)ed-st)/1000<<"s."<<endl; system("pause"); return 0; } /* 3 1 1 1 2 1 5 6 */ 和Castle一样, 现在是一个地图里洒落了一些食物, 求各个相邻区域的面积.