sgu 174 Walls hash+并查集

38 篇文章 0 订阅

       每个顶点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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值