SGU 174 Walls
这题用并查集来做,判断什么时候形成了环即判断什么时候加入的线段两个端点原先是属于同一集合的。对于一个点,有两个坐标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的更多相关文章
- SGU 174.wall
题意: 判断给出的线段是否组成了多边形. Solution: 简单题,并查集+hash 这里用map实现 code #include <iostream> #include <cst ...
- SGU 乱乱开
本解题报告 乱抄,乱写,随性随心,不喜多喷! SGU 142: 思路:一个string的字串不会超过2^20个,我们枚举出来就好了. 我出错点:数组RE #include<stdio.h> ...
- 今日SGU 5.26
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- 【SGU】495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...
- 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 ...
随机推荐
- UUID(uuid)js 生成
全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度 ...
- 上载EXCEL到SAP系统的方法之一
TEXT_CONVERT_XLS_TO_SAP实例 使用:gui_upload去上传excel数据,每次都出现乱码,不管中文英文都乱码. 至今不知道gui_upload是否支持excel文件上传,. ...
- [ javascript html Dom image 对象事件加载方式 ] 对象事件加载方式
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
- MacBook Air 使用技巧
1.Finder 相当于资源管理器 Launchpad 相当于桌面 Safari 浏览器 2.快捷键 * cmd+shift+3: 捕获整 ...
- mybatis3.3 + struts2.3.24 + mysql5.1.22开发环境搭建及相关说明
一.新建Web工程,并在lib目录下添加jar包 主要jar包:struts2相关包,mybatis3.3相关包,mysql-connector-java-5.1.22-bin.jar, gson-2 ...
- SAM4E单片机之旅——16、NAND Flash读写
这次大概介绍了一下NAND Flash,以及在ASF中使用它的方法. 一. 接线 这个开发板搭载了一个256 MB,8位的NAND Flash(MT29F2G08ABAEA).引脚接线如下: 偷个懒, ...
- 删除表空间时,遇到了ORA-14404错误
Oracle中删除表空间时,遇到了ORA-14404错误. 错误信息如下: SQL> DROP TABLESPACE PART1 INCLUDING CONTENTS AND DATAF ...
- Effective Java 17 Design and document for inheritance or else prohibit it
Principles The class must document its self-use of overridable methods. A class may have to provide ...
- springMVC请求流程详解
SpringMVC框架是一个基于请求驱动的Web框架,并且使用了'前端控制器'模型来进行设计,再根据'请求映射规则'分发给相应的页面控制器进行处理. (一)整体流程 具体步骤: 1. 首先用户发送请 ...
- sql 把特定数据排在最前面
感谢www.baidu.com/p/dongfanghong_1 sql大神,简单的语法运用起来简直活了. 第一法] select * from table where name='D' UNION ...