这题用并查集来做,判断什么时候形成了环即判断什么时候加入的线段两个端点原先是属于同一集合的。对于一个点,有两个坐标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. AI - Ideas

    Idea: Comprehend code and re-factory code to more readable. The AI program can comprehend source cod ...

  2. eclipse导入svn项目,项目却没有svn的标记

    现象: eclipse(已经装有svn插件)导入svn项目,项目没有svn的标记. 原因: 1.可能是由于你的svn eclipse插件,也就是subclipse,与svn的客户端版本不匹配. 解决 ...

  3. VisualStudio中解决方案

    在VS中创建一个项目通常会生成一个解决方案文件(.sln)和一个隐藏的解决方案用户选项文件(.suo). 解决方案文件是一个文本文件,包含以下信息: 将被加载的所有项目以构成完整解决方案的项目清单 解 ...

  4. Android studio 启动时出现Android studio was unable to create a local connection in order

    在进入后adb无法连接,并且报错 Internal HTTP server disabled: Cannot start internal HTTP server. Git …… 各种百度没有答案,最 ...

  5. 安卓开发_浅谈Android动画(一)

    动画效果,针对图片实现 现在学习四种基本的简单动画效果 一.Tween Animation共同属性 1.Duration:动画持续时间(毫秒单位) 2.fillAfter:设置为true,动画转化在动 ...

  6. 【读书笔记】iOS-忽略编译警告

    一,忽略编译警告的命令. -w   禁止掉所有的编译警告. -Wno-unused-variable  只禁止掉未使用的变量的编译警告. 二,忽略编译警告的方法. targets--->Buil ...

  7. OC数组排序

    NSArray *array = @[@"tailong", @"kaersasi", @"airuiliya", @"yingl ...

  8. Spring(四)Bean注入方试

    一.构造方法注入 定义:通过构造函数来完成依赖关系的设定 优缺点: 在构造对象的同时,完成依赖关系的建立 如果关联的对象很多,那和不得不在构造方法上加入过多的参数 基中有index:如果指定索引从0开 ...

  9. FQ技术

    什么是技术,就是翻_墙技术啦~ 关于GoogleFQ的网页,点击图片就能进入该网页浏览相关的FQ的设置 hosts所在文件夹: Windows 系统hosts位于 C:\Windows\System3 ...

  10. ORACLE 创建与使用视图

    一.what(什么是视图?) 1.视图是一种数据库对象,是从一个或者多个数据表或视图中导出的虚表,视图所对应的数据并不真正地存储在视图中,而是存储在所引用的数据表中,视图的结构和数据是对数据表进行查询 ...