【题目链接】 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2589

【题目大意】

  给出一些圆,问这些圆可以把平面分为几个部分。

【题解】

  我们发现圆交图一定是个平面图,因此可以用平面图欧拉公式R=E-V+2
  但是我们发现有些圆并不相交,因此每个图需要单独完全计算,
  我们计算每个封闭图形的平面数,他们的和+1便是答案,
  考虑单独的封闭图形有R=E-V+1,在下图中:

      

  我们发现当蓝色的圆加入图中之后,他为平面增加的点数是4,增加的边数是8,
  其中属于蓝色的圆弧的边数为4,其余四条增加的边源于红色和黄色圆弧上点的增加,
  所以我们发现对于一个圆来说,它为平面贡献的边数为其与其余圆的交点数,
  至于封闭平面图形点的计算,我们在搜索中用set来去重即可。

【代码】

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
double eps=1e-8;
int sgn(double x) {
if(x<-eps)return -1;
if(x>eps)return 1;
return 0;
}
struct vec{
double x,y;
vec(double x=0,double y=0):x(x),y(y){}
vec operator + (vec v){return vec(x+v.x,y+v.y);}
vec operator - (vec v){return vec(x-v.x,y-v.y);}
vec operator * (double v){return vec(x*v,y*v);}
vec operator / (double v){return vec(x/v,y/v);}
bool operator < (const vec &rhs)const{
if(sgn(x-rhs.x)!=0)return x<rhs.x;
else if(sgn(y-rhs.y)!=0)return y<rhs.y;
else return false;
}
bool operator ==(const vec &rhs)const{
return sgn(x-rhs.x)==0&&sgn(y-rhs.y)==0;
}
double operator *(vec v){return x*v.x+y*v.y;}
double len(){return hypot(x,y);}
double len_sqr(){return x*x+y*y;}
double angle(){return atan2(y,x);}
//逆时针旋转
vec rotate(double c){return vec(x*cos(c)-y*sin(c),x*sin(c)+y*cos(c));}
vec trunc(double l){return (*this)*l/len();}
vec rot90(){return vec(-y,x);}
};
struct circle{
vec c;double r;
circle(vec c=vec(0,0),double r=0):c(c),r(r){}
vec point(const double &a)const{
return vec(c.x+cos(a)*r,c.y+sin(a)*r);
}
};
//圆圆相交
int circle_circle_intersection(circle a,circle b,vec &p1,vec &p2) {
double d=(a.c-b.c).len();
if(sgn(d)==0)return 0;
if(sgn(a.r+b.r-d)<0||sgn(fabs(a.r-b.r)-d)>0)return false;//相离|内含
double an=(b.c-a.c).angle();
double da=acos((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d));
p1=a.point(an-da);
p2=a.point(an+da);
if(p1==p2)return 1;
else return 2;
}
const int N=60;
vector<circle> cir;
vector<int> G[N];
set<vec> dfs_save,set_p[N];
set<vec>::iterator it;
int v[N],E,T,n;
void dfs(int x){
v[x]=1;
for(it=set_p[x].begin();it!=set_p[x].end();it++)dfs_save.insert(*it);
E+=(int)set_p[x].size();
for(int i=0;i<G[x].size();i++)if(!v[G[x][i]])dfs(G[x][i]);
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n); cir.clear();
for(int i=0;i<n;i++)G[i].clear(),set_p[i].clear();
memset(v,0,sizeof(v));
for(int i=0;i<n;i++){
double x,y,r;
scanf("%lf%lf%lf",&x,&y,&r);
cir.push_back(circle(vec(x,y),r));
}
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++){
vec a,b;
int u=circle_circle_intersection(cir[i],cir[j],a,b);
if(u){
G[i].push_back(j); G[j].push_back(i);
set_p[i].insert(a); set_p[j].insert(a);
set_p[i].insert(b); set_p[j].insert(b);
}
}int ans=0;
for(int i=0;i<n;i++){
if(!v[i]){
dfs_save.clear(); E=0;
dfs(i); ans+=E-(int)dfs_save.size()+1;
}
}printf("%d\n",ans+1);
}return 0;
}

ZOJ 2589 Circles(平面图欧拉公式)的更多相关文章

  1. P7295-[USACO21JAN]Paint by Letters P【平面图欧拉公式】

    正题 题目链接:https://www.luogu.com.cn/problem/P7295 题目大意 给出\(n*m\)的网格,每个格子上有字母,相同字母的四联通相邻格子为连通,每次询问一个子矩阵求 ...

  2. POJ--2284--That Nice Euler Circuit【平面图欧拉公式】

    链接:id=2284">http://poj.org/problem?id=2284 题意:一个自己主动绘图的机器在纸上(无限大)绘图,笔尖从不离开纸,有n个指令,每一个指令是一个坐标 ...

  3. zoj 2589 Matrix Searching 二维线段树

    题目链接 给一个n*n的矩阵, 给q个查询, 每次给出x1, y1, x2, y2, 求这个矩阵中的最小值. 代码基本上和上一题相同... #include<bits/stdc++.h> ...

  4. ACM计算几何题目推荐

    //第一期 计算几何题的特点与做题要领: 1.大部分不会很难,少部分题目思路很巧妙 2.做计算几何题目,模板很重要,模板必须高度可靠. 3.要注意代码的组织,因为计算几何的题目很容易上两百行代码,里面 ...

  5. ZOJ 1608 Two Circles and a Rectangle

    Give you two circles and a rectangle, your task is to judge wheather the two circles can be put into ...

  6. poj2284 That Nice Euler Circuit(欧拉公式)

    题目链接:poj2284 That Nice Euler Circuit 欧拉公式:如果G是一个阶为n,边数为m且含有r个区域的连通平面图,则有恒等式:n-m+r=2. 欧拉公式的推广: 对于具有k( ...

  7. POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)

                                                          That Nice Euler Circuit Time Limit: 3000MS   M ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. UVa 10213 (欧拉公式+Java大数) How Many Pieces of Land ?

    题意: 一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 分析: 首先紫书上的公式是错的,不过根据书上提供的思路很容易稍加修改得到正确答案! 然后推公式吧,这里用到平面图的欧拉公 ...

随机推荐

  1. Spring 路由地址的基本使用

    1.下面是spring的使用基本框架连接 https://www.cnblogs.com/HD/p/4103239.html

  2. 【HNOI】 期望面积

    [题目描述]给定n个点,求这n个点组成凸包的期望面积.保证任意三点不共线. [数据范围]n<=100. 首先我们知道凸包面积的计算为所有在凸包上相邻的点的叉积和,那么我们可以枚举两个点,然后求出 ...

  3. Angular2.0 基础:双向数据绑定 [(ngModel)]

    在属性绑定中,值从模型到屏幕上的目标属性 (property). 通过把属性名括在方括号中来标记出目标属性,[]. 这是从模型到视图的单向数据绑定. 而在事件绑定中,值是从屏幕上的目标属性 到 mod ...

  4. copy_from_user分析

    前言 copy_from_user函数的目的是从用户空间拷贝数据到内核空间,失败返回没有被拷贝的字节数,成功返回0.它内部的实现当然不仅仅拷贝数据,还需要考虑到传入的用户空间地址是否有效,比如地址是不 ...

  5. mips64高精度时钟引起ktime_get时间不准,导致饿狗故障原因分析【转】

    转自:http://blog.csdn.net/chenyu105/article/details/7720162 重点关注关中断的情况.临时做了一个版本,在CPU 0上监控所有非0 CPU的时钟中断 ...

  6. win10安装提示“我们无法创建新的分区”

    今日于笔记本安装win10时突然出现提示:我们无法创建新的分区.网上搜了不少建议,尝试了都无果. 由于我的笔记本是固态硬盘与机械硬盘混合,所以情况可能更加特殊. 最后成功的方法是: 1. 先将Win1 ...

  7. 解决Myeclipse编译不生成.class文件问题

    1.Project --> clean...  如果该操作无效,请执行2. 2.Preferences -->Java -->Compliler -->Building --& ...

  8. mysql大法

    mysql大法 MySQL 安装方式 1.rpm(yum) 2.源码包 3.通用二进制 企业中版本选择 5.6 5.7 选择 GA 6个月到1年之间的------------------------- ...

  9. [PAT] 1146 Topological Order(25 分)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  10. Windows7 + OSG3.6 + VS2017 + Qt5.11

    一.准备工作 下载需要的材料: 1. OSG稳定版源代码, 3.6.3版本 2. 第三方库,选择VS2017对应的版本  https://download.osgvisual.org/3rdParty ...