SGU 369. Game

 

 

 

 

 

 

 

 

369. Game

Time limit per test: 0.75 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



Vasya loves his new game which is played on an infinite rectangular grid where K cells are initially black, all other cells are white. The move of the game is to find three black cells which are vertices of some rectangle with sides parallel to coordinate axis such that the fourth vertex of the rectangle is white. In this case you need to paint the fourth vertex black. Vasya asks you to write a program which calculates the number of black cells in the end of the game, i.e. when no more moves can be made.

Input
The first line contains an integer K (0 ≤ K≤ 2· 105). The next K lines contain two integers each — coordinates of black cells Xi and Yi (-109 ≤ XiYi ≤ 109).

Output
Output the answer to the task.

Example(s)
sample input
sample output
3
1 1
1 2
2 2
4

sample input
sample output
5
0 0
1 0
0 1
1 2
2 1
9




Online Contester Team © 2002 - 2010. All rights reserved.

 

 

 

 

总体思路比较简单,就是dfs,对于一个点,把跟它同一行和同一列的点都加入到这个团中,然后计算总点数

 

 

 

 

 

 

 

 

 

 

 

 

#include <iostream>
#include<map>
#include<vector>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct point{
    int x,y,index;
};
map<int,int> xmap,ymap;
map<int,int>::iterator it;
int tx,ty;
__int64 tot;
vector<point> a;
vector<int> px[210000],py[210000],tmpx,tmpy;
int flg[210000],flgx[210000],flgy[210000];
void add(int x,int y,int index)
{
    point p;
    p.x=x;
    p.y=y;
    p.index=index;
    a.push_back(p);
    it=xmap.find(x);
    if (it!=xmap.end())
        px[it->second].push_back(index);
    else
    {
        xmap.insert(make_pair(x,++tx));
        px[tx].push_back(index);
    }
    it=ymap.find(y);
    if (it!=ymap.end())
        py[it->second].push_back(index);
    else
    {
        ymap.insert(make_pair(y,++ty));
        py[ty].push_back(index);
    }
}
void dfs(int index)
{
    int r1,i;
    flg[index]=1;
    tmpx.push_back(a[index].x);
    tmpy.push_back(a[index].y);

    it=xmap.find(a[index].x);
    r1=it->second;
    if (flgx[r1]==0)
    {
        flgx[r1]=1;
        for(i=0;i<px[r1].size();i++)
            if (flg[px[r1][i]]==0)
                dfs(px[r1][i]);
    }

    it=ymap.find(a[index].y);
    r1=it->second;
    if (flgy[r1]==0)
    {
        flgy[r1]=1;
        for(i=0;i<py[r1].size();i++)
            if (flg[py[r1][i]]==0)
                dfs(py[r1][i]);
    }
}
void dog()
{
    int i;
    __int64 r1,r2;
    sort(tmpx.begin(),tmpx.end());
    for(i=1,r1=1;i<tmpx.size();i++)
        if (tmpx[i]!=tmpx[i-1]) r1++;
    sort(tmpy.begin(),tmpy.end());
    for(i=1,r2=1;i<tmpy.size();i++)
        if (tmpy[i]!=tmpy[i-1]) r2++;
    tot=tot+r1*r2;
}
int main()
{
    int i,x,y,n;
    point pt;
    while(scanf("%d",&n)!=EOF)
    {
        tx=ty=0;
        xmap.clear();
        ymap.clear();
        a.clear();
        a.push_back(pt);
        for(i=0;i<=n;i++)
        {
            px[i].clear();
            py[i].clear();
        }
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y,i);
        }
        for(i=0;i<=n;i++)
            flg[i]=flgx[i]=flgy[i]=0;
        tot=0;
        for(i=1;i<=n;i++)
            if (flg[i]==0)
            {
                tmpx.clear();
                tmpy.clear();
                dfs(i);
                dog();
            }
        printf("%I64d\n",tot);
    }
    return 0;
}

  

posted on 2015-09-01 23:33  oi111  阅读(498)  评论(0编辑  收藏  举报