SGU 174
Appoint description:
Description
People of country T-land lived on the big plain many years ago. It is happened so that they started to quarrel, so they began to build walls to separate from each other.
One day they realized that walls surround some part of the country. Your task is to determine which wall was build first to surround a part of the T-land.
One day they realized that walls surround some part of the country. Your task is to determine which wall was build first to surround a part of the T-land.
Input
The first line of input contains one number M (1<=M<=200000) - number of walls. Each of the following M lines contains four integer numbers: Cartesian coordinates of two ends of each wall. Walls are rectilinear segments with positive length, two walls can cross only by ends, and walls can't coincide. All coordinates do not exceed 10^9 by its absolute values.
Output
Write the answer in the single line of output. If all the walls leave the territory opened, write 0.
Sample Input
Input
4 0 0 1 0 0 1 0 0 1 0 0 1 2 2 5 7
Output
3
用map建立映射,找点的下标
AC代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <map>
using namespace std;
int fa[400005];
map< pair<int,int>,int >M;
int Find_set(int x)
{
if(x==fa[x]) return x;
fa[x]=Find_set(fa[x]);
return fa[x];
}
int main()
{
int m,x,y;
int a,b,c,d;
while(scanf("%d",&m)!=EOF)
{
for(int i=1;i<=400000;i++)
fa[i]=i;
int cnt=1;
int flag=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(!flag)
{
if(!M.count(make_pair(a,b)))
M[make_pair(a,b)]=cnt++;
x=M[make_pair(a,b)];
if(!M.count(make_pair(c,d)))
M[make_pair(c,d)]=cnt++;
y=M[make_pair(c,d)];
int rx=Find_set(x);
int ry=Find_set(y);
if(rx==ry)
flag=i;
else
fa[rx]=ry;
}
}
printf("%d\n",flag);
}
return 0;
}