HZOJ Lianliankan

Examination room almost think positive solution, but I do not know too serious at the time, in the absence of falsification just feel can not be achieved on whether the ......

The last fight seems to be Dage said O (4 * 15 * n * m), the complexity of the evidence anyway, it will not become violent ...... T


 

answer:

For the test point 8,9,10: bfs Unicom each block 0, the block edge Unicom if a box appears tired of x, then the contribution to x * (x-1) / 2. Note remove both adjacent to and in the same contribution Unicom block. Due to the nature of these test points, figured out the answer is correct.

Extended to the general case, the above conclusion is not fully applicable.

Conclusion The above considerations will be what is wrong, when the two blocks belongs to a plurality of link block 0 will be re-counted, considering inclusion and exclusion. (Since not thought suitable implementation complexity of the examination room to stop here)

Unicom to consider each block number 0, for convenience, the same block is provided between two adjacent blocks there is a 0 Unicom.

0 is obtained for each first bfs Unicom block complexity $ O (nm) $ points belonging to the non-zero.

For each type of block, to obtain F1 [i] represents the number of blocks in the link i, f2 [i] [j] is represented by i, J (there may be other) number, f3 [] [] [], f4 [] [] [] [] Similarly.

Then you can even cut Qi Jia statistical answer.

However, the array and it can not so big, consider Hash_map.

Since the hash structure is not very good get, so I used the map, easier to understand, code complexity is also lower.

Therefore, the following code card can easily be constant.

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#define int LL
#define pair pair<int,int>
#define fir first
#define sec second
#define MP(a,b) make_pair(a,b)
#define LL long long
#define s1 (a.a<b.a)
#define s2 (a.b<b.b)
#define s3 (a.c<b.c)
#define s4 (a.d<b.d)
#define s5 (a.s<b.s)
#define d1 (a.a==b.a)
#define d2 (a.b==b.b)
#define d3 (a.c==b.c)
#define d4 (a.d==b.d)
#define d5 (a.s==b.s)
using namespace std;
struct node1{int a,s;friend bool operator < (node1 a,node1 b){return d1?s5:s1;}};
struct node2{int a,b,s;friend bool operator < (node2 a,node2 b){return d1?(d2?s5:s2):s1;}};
struct node3{int a,b,c,s;friend bool operator < (node3 a,node3 b){return d1?(d2?(d3?s5:s3):s2):s1;}};
struct node4{int a,b,c,d,s;friend bool operator < (node4 a,node4 b){return d1?(d2?(d3?(d4?s5:s4):s3):s2):s1;}};
map<node1,int>mp1;
map<node2,int>mp2;
map<node3,int>mp3;
map<node4,int>mp4;
int n,m,k,mp[1010][1010];
int bel [1010] [1010] cnt;
int t[1000010];
bool v[1010][1010];
you left [5] = {0,1, -1,0, 0};
int iy[5]={0,0, 0,1,-1};
vector<int> be[1010][1010];

bool is(int x,int y,int sor){return x>0&&x<=n&&y>0&&y<=m&&mp[x][y]==sor;}
bool ok(int x,int y){return x>0&&x<=n&&y>0&&y<=m&&mp[x][y]==0;}
void bfs (int nx, int)
{
	bel [nx] [] = cnt;
	queue<pair >q;q.push(MP(nx,ny));
	while(!q.empty())
	{
		int x=q.front().fir,y=q.front().sec;q.pop();
		for(int i=1;i<=4;i++)
		{	
			int tx=x+ix[i],ty=y+iy[i];
			if(ok(tx,ty)&&!bel[tx][ty])q.push(MP(tx,ty)),bel[tx][ty]=cnt;
			else if(tx>0&&tx<=n&&ty>0&&ty<=m&&mp[tx][ty])be[tx][ty].push_back(cnt);
		}
	}
}
signed main()
{
//	freopen("ex_link3.in","r",stdin);
//	freopen("in.txt","r",stdin);
//	freopen("1.out","w",stdout);
	
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		if(!mp[i][j]&&!bel[i][j])++cnt,bfs(i,j);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(mp[i][j]&&is(i+1,j,mp[i][j]))be[i][j].push_back(++cnt),be[i+1][j].push_back(cnt);
			if(mp[i][j]&&is(i,j+1,mp[i][j]))be[i][j].push_back(++cnt),be[i][j+1].push_back(cnt);
		}
		
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			sort(be[i][j].begin(),be[i][j].end());
			int t=unique(be[i][j].begin(),be[i][j].end())-be[i][j].begin();
			while(be[i][j].size()>t)be[i][j].pop_back();
		}
	
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		if(mp[i][j])
		{
			sort(be[i][j].begin(),be[i][j].end());int siz=be[i][j].size();
			if(siz>=1){for(int k=0;k<siz;k++)mp1[(node1){be[i][j][k],mp[i][j]}]++;}
			if(siz>=2){for(int k=0;k<siz;k++)for(int l=k+1;l<siz;l++)mp2[(node2){be[i][j][k],be[i][j][l],mp[i][j]}]++;}
			if(siz>=3){for(int k=0;k<siz;k++)for(int l=k+1;l<siz;l++)for(int r=l+1;r<siz;r++)
				mp3[(node3){be[i][j][k],be[i][j][l],be[i][j][r],mp[i][j]}]++;}
			if(siz>=4){mp4[(node4){be[i][j][0],be[i][j][1],be[i][j][2],be[i][j][3],mp[i][j]}]++;}
		}
	map<node1,int>::iterator it1;
	map<node2,int>::iterator it2;
	map<node3,int>::iterator it3;
	map<node4,int>::iterator it4;
	LL ans=0;
	for(it1=mp1.begin();it1!=mp1.end();it1++)ans+=it1->second*(it1->second-1)/2;
	for(it2=mp2.begin();it2!=mp2.end();it2++)ans-=it2->second*(it2->second-1)/2;
	for(it3=mp3.begin();it3!=mp3.end();it3++)ans+=it3->second*(it3->second-1)/2;
	for(it4=mp4.begin();it4!=mp4.end();it4++)ans-=it4->second*(it4->second-1)/2;
	printf("%lld\n",ans);
}
	

 

Guess you like

Origin www.cnblogs.com/Al-Ca/p/11695334.html