sgu 174 分类: sgu 2015-03-07 23:36 34人阅读 评论(0) 收藏


题目大意就是给你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;
}

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

posted @ 2015-03-07 23:36  <Dash>  阅读(136)  评论(0编辑  收藏  举报