来源:牛客网
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
After planting a new tree in (3,2), its size is 2.
输入描述:
as described above.
.
输出描述:
Output N lines, each line a integer indicating the total size of supercalifragilisticexpialidocious areas after each operation.
思路:
如果在一个地图上种树让你判断是否成环不太容易,但是,若把所有的树种好,依次去伐树,那么问题就简单不少,这个题目中,与边界相邻的没树的地方不算在绿化区,因此可以增加最外围一层巧妙地标记所有的这种区域。种好所有树后就可以依次砍树,如果旁边有边界区,就去bfs。
#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 4e6+7;
bool mp[2001][2001];
bool mk[2001][2001];
int sz;
int Next[4][2] = {1,0,-1,0,0,1,0,-1};
void bfs(int x,int y)
{
queue <pair<int,int> > q;
q.push(make_pair(x,y));
mk[x][y] = 1;
while(q.size())
{
pair <int,int> p = q.front();
q.pop();
x = p.first,y=p.second;
for(int i = 0; i < 4; i++)
{
int x_ = x+Next[i][0];
int y_ = y+Next[i][1];
if(x_ < 0 || y_ < 0 || x_ >= 2000 || y_ >= 2000)
{
continue;
}
if(mp[x_][y_] || mk[x_][y_]) continue;
mk[x_][y_] = 1;
sz--;
q.push(make_pair(x_,y_));
}
}
}
int qx[100005],qy[100005];
int ans[100005];
int main()
{
int n;
scanf("%d",&n);
int m = n;
int i = 0;
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
i++;
x+=1000;
y+=1000;
qx[i] = x;
qy[i] = y;
mp[x][y] = 1;
}
sz = 2000*2000 - n-1;
mk[0][0] = 1;
bfs(0,0);
ans[n] = sz;
for(int i = n; i >= 1; i--)
{
int x = qx[i];
int y = qy[i];
sz++;
int yes = 0;
for(int k = 0; k < 4; k++)
{
int x_ = x+Next[k][0];
int y_ = y+Next[k][1];
if(mp[x_][y_]) continue;
if(mk[x_][y_]) yes = 1;
}
if(yes) sz--,bfs(x,y);
ans[i-1] = sz;
mp[x][y] = 0;
}
for(int i = 1; i <= n; i++)
{
printf("%d\n",ans[i]);
}
return 0;
}
7180




被折叠的 条评论
为什么被折叠?



