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 ...
随机推荐
- nginx服务器是怎么执行php脚本的?
简单的说: fastCGI是nginx和php之间的一个通信接口,该接口实际处理过程通过启动php-fpm进程来解 析php脚本,即php-fpm相 当于一个动态应用服务器,从而实现nginx动态解析 ...
- [WF] Quickstart Sample
[WF] Quickstart Sample 前言 Workflow Foundation(WF),总是给人一种很有用.可是却不知道怎么用的印象.这主要是因为前置的功课太多.要整合很多底层知识,才能完 ...
- JS基本概念 -- 数据类型(一)
ECMAScript中有5种简单数据类型(也成为基本数据类型): Undefined.Null.Boolean.Number.String; 1种复杂数据类型: Object,Object本质上是由一 ...
- 优化ABAP性能(摘录)
1.使用where语句不推荐Select * from zflight.Check : zflight-airln = ‘LF’ and zflight-fligh = ‘BW222’.Endsele ...
- mysql实时同步到mssql的解决方案
数据库在应用程序中是必不可少的部分,mysql是开源的,所以很多人它,mssql是微软的,用在windows平台上是非常方便的,所以也有很多人用它.现在问题来了,如何将这两个数据库同步,即数据内容保持 ...
- 聊聊GISer的职业发展
一.前言 去年写了一篇名为<GISer们还有机会屌丝逆袭吗?>的博文,希望能和广大GISer一起探讨地理信息产业留给小团队和个人的机会.文章发布后,很多GISer通过网络和我进行了交流,其 ...
- teambition的热血团队
一群热血年轻人开发了一套项目协作软件teambition,无意帮他们宣传,更多见网址www.teambition.com. 网站上简短的一段话,深深的打动了我.他们说teambition: 由热爱工作 ...
- The URL "filename" is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web
Sharepoint Error : The URL "filename" is invalid. It may refer to a nonexistent file or fo ...
- Ubuntu上安装Apache、MySql和PHP
参考文献: http://developer.51cto.com/art/201110/299303.htm
- Python学习二---字符串
一.字符串 1.1.字符串和转义字符 转义字符需要使用\来表示 1.2.字符串连接 print 字符串1 字符串2,打印出来的字符串直接连接在一起没有空格 print 字符串1,字符串2,打印出来的字 ...