Ural 1966 Cycling Roads
Description
Input
Output
Sample Input
input | output |
---|---|
4 2 |
YES |
4 3 |
NO |
3 2 |
YES |
这道题主要是判相交,只要相交就把它压入并查集,一开始我是用了cnt去记录已经相交的节点,后来发现不行,因为新加如的一条线如果加进去了,它的另外一个端点也会加入,导致cnt记录的数值不准。于是用了另外一个数组c[i]去记录以i为根的所有子节点的个数。
在判断相交这里,一开始没有注意到新加入一条线段时,应该判断所有点是否在该线段上,如果端点在该线段上,则把它加入,加了OnSegment()判断之后就AC了。
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
#define maxn 205
struct point
{
double x,y;
point(double x = ,double y = ):x(x),y(y){}
}p[maxn]; struct Line
{
point a,b;
int pos1,pos2;
Line(){}
Line(point x,point y,int ppos1,int ppos2){ a = x; b = y; pos1 = ppos1; pos2 = ppos2;}
}line[maxn]; int n,m,cnt;
int par[maxn];
int c[maxn]; typedef point Vector;
Vector operator +(Vector A,Vector B){ return Vector(A.x+B.x, A.y+B.y); }
Vector operator -(Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }
Vector operator *(Vector A,double p) { return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,double p){ return Vector(A.x/p,A.y/p); }
const double eps = 1e-;
int dcmp(double x)
{
if(fabs(x) < eps) return ;
else return x < ? -:;
}
bool operator == (const point &a,const point &b)
{
return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ;
}
double dot(Vector A,Vector B){ return A.x*B.x + A.y*B.y; }
double cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x; } bool OnSegment(point p,Line l)
{
return dcmp(cross(l.a-p,l.b-p)) == && dcmp(dot(l.a-p,l.b-p)) < ;
}
bool SegmentProperIntersection(Line l1,Line l2)
{
point a1 = l1.a;
point a2 = l1.b;
point b1 = l2.a;
point b2 = l2.b;
double c1 = cross(a2-a1,b1-a1);
double c2 = cross(a2-a1,b2-a1);
double c3 = cross(b2-b1,a1-b1);
double c4 = cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2) < && dcmp(c3)*dcmp(c4) < ;
} void init()
{
for(int i = ; i <= n; i++)
c[i] = ;
for(int i = ; i < maxn;i++)
par[i] = i;
}
int Find(int x)
{
if(par[x] != x)
{
return par[x]=Find(par[x]);
}
else return x;
} void Merge(int a,int b)
{
int t1 = Find(a);
int t2 = Find(b);
if(t1 != t2)
{
par[t2] = t1;
c[t1] += c[t2];
//printf("%d %d merge\n",a,b);
//return 1;
}
//return 0;
} void input()
{
int x,y; for(int i = ; i <= n; i++)
{
double x,y;
scanf("%lf%lf",&x,&y);
p[i] = point(x,y);
}
for(int i = ; i < m; i++)
{
scanf("%d%d",&x,&y);
line[i] = Line(p[x],p[y],x,y);
for(int j = ; j <= n; j++)
{
if(OnSegment(p[j],line[i])) Merge(j,x);
}
Merge(x,y);
}
} void deal()
{
for(int i = ; i < m; i++)
{
for(int j = i + ; j < m; j++)
{
if(SegmentProperIntersection(line[i],line[j]))
{
Merge(line[j].pos1,line[i].pos1);
//Merge(line[j].pos2,line[i].pos1);
}
}
} } int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d%d",&n,&m) == )
{
init();
input();
deal();
if(c[Find()] == n) printf("YES\n");
else printf("NO\n");
} return ;
}
Ural 1966 Cycling Roads的更多相关文章
- URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集
F - Cycling Roads Description When Vova was in Shenzhen, he rented a bike and spent most of the ...
- URAL 1966 Cycling Roads 计算几何
Cycling Roads 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...
- URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)
意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...
- Cycling
Cycling Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- Ural 1004 Sightseeing Trip
Sightseeing Trip Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
随机推荐
- 网络通信实验(2)TCP/IP LWIP 简介
TCP/IP 简介 TCP/IP 中文名为传输控制协议/因特网互联协议,又名网络通讯协议,是 Internet 最基本的协议. Internet 国际互联网络的基础,由网络层的 IP 协议和传输层的 ...
- Python学习笔记6函数和代码复用
1.函数 (1)定义: (2)函数调用 (3)函数的参数传递 参数传递的两种方式:函数调用时,参数可以按照位置或名称的方式传递 (4)函数的返回值 (5)局部变量和全局变量 (6)lambda函数 2 ...
- Servlet+JSP例子
前面两节已经学习了什么是Servlet,Servlet接口函数是哪些.怎么运行.Servlet生命周期是什么? 以及Servlet中的模式匹配URL,web.xml配置和HttpServlet.怎么 ...
- C# 两个datatable中的数据快速比较返回交集或差集[z]
最基本的写法无非是写多层foreach循环,数据量多了,循环的次数是乘积增长的. 这里推荐使用Except()差集.Intersect()交集,具体性能没有进行对比. 如果两个datatable的字段 ...
- 关于Android UI 优化
之前项目为了同时兼容tv和手机端的UI,使用了百分比布局来动态计算控件的宽高,这种适配方案只关心屏幕的宽高(分辨率),与屏幕的像素密度无关. 在新的项目里也使用了这种方案.但是由于项目的运行硬件计算能 ...
- Module 的语法
模块功能主要由两个命令构成:export和import.export命令用于规定模块的对外接口,import命令用于输入其他模块提供的功能. 一个模块就是一个独立的文件.该文件内部的所有变量,外部无法 ...
- vue中v-model 与 v-bind:value
之前一直认为,v-model相当于下方代码的语法糖,如下: <h1>{{inputValue}}</h1> <input type="text" :v ...
- python、java读数据
python从txt文档中读数据有个特别神奇的函数 可以把txt文档中的数据直接读取成python数组 java用Scanner类读数据比较方便
- nodejs之使用express框架连接mysql数据库,返回jsonapi数据
var express = require('express');var router = express.Router();var url = require('url');var mysql = ...
- python的语法小结
break 与continue的区别: 1.break是直接中断全部循环 2.continue则是在只不执行此次所循环的东西,其它循环依旧执行,比方说只是跳过第4次循环,第5次循环照常进行. \n 表 ...