SGU 320. The Influence of the Mafia
320. The Influence of the Mafia
Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
Memory limit: 65536 kilobytes
input: standard
output: standard
output: standard
The problem with zones of influence was finally solved during the recent Berland mafia meeting. Berland was represented as a rectangle of N x M squares. All squares were divided between the mafia leaders. Each leader got a connected region of squares, i.e. it is possible to get from any square of the region to any other by making moves only across the squares of this region. Four types of moves are allowed: up, down, left and right.
Each leader was assigned a number from 0 to 9, it is allowed for two different leaders to have the same number. The leaders decided to create a map of Berland where each square has the number of the leader who owes it. The secretary was able to assign leaders with numbers in such a way that no two bordering regions on the map have the same number. The valiant Berland police seized the map with the numbers. And now the chief of the police wants to know the degree of the mafia influence in Berland.
He introduced following definitions:
- A leader is called big if he possesses K or more squares.
- A square is called dangerous if it is controlled by a big leader or if there exists such big leader that it is impossible to get out of Berland (i.e. to get to a square on the border of the country) without visiting a square controlled by this leader. Allowed moves: up, down, left and right.
The chief of the police wants to know the number of dangerous squares in the country. He decided to ask his valiant programmers to solve this important task.
InputThe first line of the input contains integer numbers N, M and K (1≤ N, M≤ 500; 1≤ K≤ 250000). The map created by the secretary follows. The map is written in the form of Nlines with M numbers from 0 to 9 in each line. Numbers in a line are written one after another without spaces.
OutputWrite to the output the number of dangerous squares in the country.
Example(s)sample input
sample output
7 6 4 200320 011022 018100 018111 201191 020011 002020
14
NoteThere are two big leaders in the country (both are marked with the number 1, one has 4 squares, another has 9 squares). There is one more dangerous square (marked with the number 9) besides the regions in possession of big leaders. Note that squares marked with the number 8 are not dangerous, as they do not satisfy the definition of a dangerous square.
| Online Contester Team © 2002 - 2010. All rights reserved. |
一道好题,把原问题转化为图,然后用类似求割点的方法求解
#include <iostream>
#include<stdio.h>
#define NN 260000
using namespace std;
struct edge{
int v,next;
};
edge f[1100000];
int pt,n,m,num,u[NN],u2[NN],u3[NN],w[NN],h[NN],g[NN],flg[600][600],a[600][600],b[600][600],g2[NN];
int c[][2]={{0,1},{0,-1},{1,0},{-1,0}};
char ch[1000];
void addedge(int x,int y)
{
f[++pt].v=y;
f[pt].next=h[x];
h[x]=pt;
}
void dfs(int x,int y,int z)
{
int i;
flg[x][y]=1;
b[x][y]=z;
for(i=0;i<4;i++)
if (x+c[i][0]>=1&&x+c[i][0]<=n&&y+c[i][1]>=1&&y+c[i][1]<=m)
if (flg[x+c[i][0]][y+c[i][1]]==0&&a[x+c[i][0]][y+c[i][1]]==a[x][y])
dfs(x+c[i][0],y+c[i][1],z);
}
void dfs2(int index)
{
int i;
u2[index]=1;
u3[index]=1;
for(i=h[index];i!=-1;i=f[i].next)
if (u2[f[i].v]==0)
dfs2(f[i].v);
}
void dog(int index,int pre)
{
int i;
u[index]=1;
u2[index]=1;
g2[index]=g[index]=++pt;
for(i=h[index];i!=-1;i=f[i].next)
{
if (f[i].v==pre) continue;
if (u[f[i].v]==1)
{
if (g[f[i].v]<g2[index]) g2[index]=g[f[i].v];
}
else
{
dog(f[i].v,index);
g2[index]=min(g2[index],g2[f[i].v]);
if (g2[f[i].v]>=g[index]&&w[index]>=num)
dfs2(f[i].v);
}
}
u2[index]=0;
}
int main()
{
int i,j,tot,k,ans;
while(scanf("%d%d%d",&n,&m,&num)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%s",ch);
for(j=1;j<=m;j++)
a[i][j]=ch[j-1]-'0';
}
for(i=0;i<=n+1;i++)
for(j=0;j<=m+1;j++)
b[i][j]=flg[i][j]=0;
tot=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if (flg[i][j]==0)
dfs(i,j,++tot);
for(i=0;i<=tot;i++)
w[i]=u[i]=u2[i]=u3[i]=0;
for(i=0;i<=tot;i++)
h[i]=-1;
pt=0;
for(i=0;i<=n+1;i++)
for(j=0;j<=m+1;j++)
{
for(k=0;k<4;k++)
if (i+c[k][0]>=0&&i+c[k][0]<=n+1&&j+c[k][1]>=0&&j+c[k][1]<=m+1)
if (b[i+c[k][0]][j+c[k][1]]!=b[i][j])
addedge(b[i][j],b[i+c[k][0]][j+c[k][1]]);
w[b[i][j]]++;
}
w[0]=0;
pt=0;
dog(0,-1);
ans=0;
for(i=1;i<=tot;i++)
if (u3[i]==1||w[i]>=num) ans+=w[i];
printf("%d\n",ans);
}
return 0;
}
浙公网安备 33010602011771号