每个顶点hash一下,最多也就200000*2个点..每条线段连接的两个直线合并,枚举到第i条直线时,如果两段的点已经是同一个几何的了,答案就是i..无解就输出0
#include <iostream>
#include <cstdio>
#include <map>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
struct node
{
int x,y;
node(int a,int b)
{
x=a; y=b;
}
node()
{
}
bool operator<(const node& p)const
{
if (x!=p.x) return x<p.x;
return y<p.y;
}
bool operator==(const node& p)const
{
return (x==p.x && y==p.y);
}
};
const int maxn=400000+400;
int n,m;
int x1,y1,p,q,k1,k2,x2,y2;
int cnt;
bool vis;
int fa[maxn];
int find(int x)
{
if (x==fa[x]) return x;
else return fa[x]=find(fa[x]);
}
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for (int i=1; i<=(n<<1); i++)
fa[i]=i;
cnt=0;
map<node,int> hash;
int ans=0;
for (int i=1; i<=n; i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if (ans) continue;
if (hash.find(node(x1,y1))==hash.end())
{
cnt++;
hash[node(x1,y1)]=cnt;
k1=cnt;
}
else k1=hash[node(x1,y1)];
if (hash.find(node(x2,y2))==hash.end())
{
cnt++;
hash[node(x2,y2)]=cnt;
k2=cnt;
}
else k2=hash[node(x2,y2)];
p=find(k1);
q=find(k2);
if (p==q) ans=i;
else
{
fa[p]=q;
}
}
printf("%d\n",ans);
return 0;
}