这题用并查集来做,判断什么时候形成了环即判断什么时候加入的线段两个端点原先是属于同一集合的。对于一个点,有两个坐标x,y,不好做并查集操作,于是要用map来存储,即做成map<node,int>形式,每加入一条线段,如果没有出现过这一个/两个端点,则赋此条线段一个/两个端点一个类型,然后找他的两个端点是否原先在同一个集合即可。

代码:

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
#define N 400100 int fa[N]; struct node
{
int x,y;
bool operator < (node a) const{
if (x==a.x)
return y<a.y;
else
return x<a.x;
}
node (int _x,int _y):x(_x),y(_y){}
}; map<node,int> mp; void makeset(int n)
{
for(int i=;i<=*n;i++)
{
fa[i] = i;
}
} int findset(int x)
{
if(x != fa[x])
{
fa[x] = findset(fa[x]);
}
return fa[x];
} void unionset(int a,int b)
{
int x = findset(a);
int y = findset(b);
fa[x] = y;
} int main()
{
int m,i,j;
int a,b,c,d;
while(scanf("%d",&m)!=EOF)
{
int type = ;
int flag = ;
makeset(m);
for(i=;i<=m;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(flag == )
{
node ka = node(a,b);
node kb = node(c,d);
if(mp[ka]==)
mp[ka] = type++;
if(mp[kb]==)
mp[kb] = type++;
int x = mp[ka];
int y = mp[kb];
if(findset(x) == findset(y))
{
flag = i;
}
else
unionset(x,y);
}
}
cout<<flag<<endl;
}
return ;
}

记住这里要开两倍空间,因为每条线段有两个点。

SGU 174 Walls的更多相关文章

  1. SGU 174.wall

    题意: 判断给出的线段是否组成了多边形. Solution: 简单题,并查集+hash 这里用map实现 code #include <iostream> #include <cst ...

  2. SGU 乱乱开

    本解题报告 乱抄,乱写,随性随心,不喜多喷! SGU 142: 思路:一个string的字串不会超过2^20个,我们枚举出来就好了. 我出错点:数组RE #include<stdio.h> ...

  3. 今日SGU 5.26

    #include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl ...

  4. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

  5. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  6. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  7. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  8. 【SGU】495. Kids and Prizes

    http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...

  9. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

随机推荐

  1. 设置php下载文件的超时时间

    使用curl 可以使用curl自己实现一个curl_file_get_contents函数 //CURLOPT_FOLLOWLOCATION TRUE 时将会根据服务器返回 HTTP 头中的 &quo ...

  2. 自定义View_1_关于View,ViewGroup的测量和绘制流程

    自定义View(1) ------ 关于View,ViewGroup的测量和绘制流程 在Android当中,自定义控件属于比较高级的知识体系,今天我们就一起研究研究关于自定义View的那点事,看看它到 ...

  3. [WF] Quickstart Sample

    [WF] Quickstart Sample 前言 Workflow Foundation(WF),总是给人一种很有用.可是却不知道怎么用的印象.这主要是因为前置的功课太多.要整合很多底层知识,才能完 ...

  4. Vue自带的过滤器

    gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson05 一 过滤器写法 {{ message | Filter}} 二 ...

  5. ABAP - 3D Graphs with SAP

    在ABAP设计中,程序员经常需要用图形显示报表的统计结果,我们可以使用函数:GRAPH_MATRIX_3D来达到图形显示.GRAPH_MATRIX_3D函数参数很多,但只有三个参数必须需要输入:Tab ...

  6. Atitit. Xss 漏洞的原理and应用xss木马

    Atitit. Xss 漏洞的原理and应用xss木马 1. XSS漏洞1 2. XSS的用途2 2.1. 盗取cookie2 2.2. 刷新流量 刷分3 2.3. DOS 窃取隐私”.“假冒身份”. ...

  7. Android Studio 使用小技巧

    1.如何打开设置界面? File --> Settings 快捷键  Ctrl + Alt  + s 2.修改Java文件字体大小 ? File --> Settings --> E ...

  8. CentOS下安装实时检测网络带宽的小工具bmon

    首先下载rpmforge-release扩展的rpm包 32位操作系统:wget http://www.sudu.us/Tools/bmon/rpmforge-release-0.3.6-1.el5. ...

  9. 使用hibernate时出现 org.hibernate.HibernateException: Unable to get the default Bean Validation factory

    hibernate 在使用junit测试报错: org.hibernate.HibernateException: Unable to get the default Bean Validation ...

  10. Effective Java 68 Prefer executors and tasks to threads

    Principle The general mechanism for executing tasks is the executor service. If you think in terms o ...