sgu 174 分类: sgu 2015-03...


题目大意就是给你N条线段,然后按照顺序添加线段,求出当添加入第几条线段时,已添加的某些线段组成了一个回路。


离散化 + 并查集 .



#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>

#define INF 1<<30

const int MAXN = 200005 ,SIZE = 2000002 ;

struct node{int x1,x2,y1,y2;}mp[MAXN] = {0};
long long  gather[MAXN << 1] = {0} , *p;
struct idnode{int ind1,ind2;}id[MAXN] = {0};
int fa[MAXN << 1] = {0};

inline int find_fa(const int x)
{
    if(fa[x] == x) return x;
    else
    {
        fa[x] = find_fa(fa[x]);  return fa[x];
    }
}


int main()
{
    int n , ans = 0 ;

#ifndef ONLINE_JUDGE    
    freopen("sgu174.in","r",stdin);
    freopen("sgu174.out","w",stdout);
#endif  

    scanf("%d",&n);

    for(int i = 1; i <= n; i++)
    {
        scanf("%d%d%d%d",&mp[i].x1,&mp[i].y1,&mp[i].x2,&mp[i].y2);

        gather[(i<<1)-1] = (long long) mp[i].x1 * SIZE + mp[i].y1;
        gather[i<<1] = (long long) mp[i].x2 * SIZE + mp[i].y2;
    }
    std::sort(gather + 1, gather + (n<<1) + 1);

    p = std::unique(gather + 1,gather + (n<<1) + 1);

    for(int i = 1 ;i <= p - (gather + 1); i++)fa[i] = i;

    for(int i = 1; i <= n; i++)
    {
        id[i].ind1 = (std::lower_bound(gather + 1 , p , (long long)mp[i].x1 * SIZE + mp[i].y1) - (gather));
        id[i].ind2 = (std::lower_bound(gather + 1 , p , (long long)mp[i].x2 * SIZE + mp[i].y2) - (gather));

        int u = find_fa(id[i].ind1) , v = find_fa(id[i].ind2);

        if(u == v){ans = i; break;}
        else  fa[std::max(u,v)] = std::min(u,v);
    }

    printf("%d\n",ans);

#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif  
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/dashgua/p/4723096.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值