Codeforces 933 C. A colourful prospect (planar diagram, Euler formula) __ Computational geometry

Source: Internet
Author: User

Firecrackers scare Nian Monster but they ' re wayyyyy too Maybe Fireworks make a nice complement.

Little Tommy is watching a firework. As circular shapes spread across the sky, a splendid view unfolds on the night of lunar New year ' s Eve.

A wonder strikes Tommy. How many regions are formed by the circles on the sky? We Consider the sky as a flat plane. A region is a connected part of the plane with positive area, whose bound consists of parts of bounds of the circles and I S a curve or several curves without self-intersections, and that is does not contain the any curve the other than its boundaries. Note that exactly one of the regions extends infinitely.


The "a" of input contains one integer n (1≤n≤3), denoting the number of circles.

The following n lines each contains three space-separated integers x, y and R (-10≤x, y≤10, 1≤r≤10), describing A circle whose center is (x, y) and the radius is R. No two circles have the same x, Y and R at the same time.


Print a single integer-the number of regions on the plane.

examples Input

0 0 1
2 0 1 4 0 1

examples Output



Given the information of n circles in a plane, these circles are divided into several regions.

train of Thought

Long-lost template problem to pretend ~ good happy ~ But the template put in the school cry chirp 〒▽〒

How many regions of a circular split plane can be separated from the Euler formula of the planar graph?

General planar diagram Euler formula: f=e−v+c+1 f = e−v + C + 1 f=e-v+c+1

where E e E represents the number of sides, V V v represents the number of points, C c C represents the number of connected blocks, F f F represents the number of planar regions

We regard the arc as the edge, the intersection as the vertex, so it is easy to calculate the E,v,c E, V, c e,v,c ~

For e e E, it corresponds to the number of points per circle and (because these intersections split the circle into so many arcs)

For V v V, the enumeration is able to find the intersection point.

For C c C, we already have the side of the undirected graph, the number of connected blocks can be directly dfs/bfs or the collection of the calculation come out ~

Then the formula is the result, pay attention to the accuracy problem.

AC Code

#include <bits/stdc++.h> #define IO Ios::sync_with_stdio (false); \ cin.tie (0); \ cout.tie (0);
using namespace Std;
typedef __int64 LL;
typedef long double Ldb;
const int MAXN = 10;
Const LDB EPS = 1e-12;
int n;
    struct Point {Ldb x,y; Point () {} point (Ldb _x,ldb _y): X (_x), Y (_y) {} point operator + (const point &t) const {return Poi
    NT (X+T.X,Y+T.Y);
    Point operator-(const point &t) const {return point (X-T.X,Y-T.Y);
    The point operator * (const LDB &t) is const {return point (x*t,y*t);
    Point operator/(const LDB &t) Const {return point (x/t,y/t);
    BOOL operator < (const point &t) const {return fabs (x-t.x) < EPS? y<t.y:x<t.x;
    BOOL operator = = (Const point &t) const {return fabs (x-t.x) <eps && fabs (Y-T.Y) <eps;
    LDB len () const {return sqrt (x*x+y*y);
   Point Rot90 () const {return point (-y,x);

    struct Circle {point o;
    Ldb R; Circle () {} Circle (point _o,ldb _r): O (_o), R (_r) {} friend vector<point> operator & (const Circle &AMP;C1
        , const Circle &AMP;C2) {Ldb d= (C1.O-C2.O). Len ();
        if (D>c1.r+c2.r+eps | | d<fabs (C1.R-C2.R)-eps) return vector<point> ();
        Ldb dt= (C1.R*C1.R-C2.R*C2.R)/d,d1= (D+DT)/2;
        Point dir= (C2.O-C1.O)/d,pcrs=c1.o+dir*d1;
        DT=SQRT (Max (0.0L,C1.R*C1.R-D1*D1)), Dir=dir.rot90 ();
    Return vector<point> {PCRS+DIR*DT,PCRS-DIR*DT};
}} P[MAXN];

void Init () {for (int i=1; i<maxn; i++) fa[i] = I,rk[i] = 0;}
    int find_set (int x) {if (fa[x]!=x) fa[x] = Find_set (fa[x));
return fa[x];
    } void Union_set (int x,int y) {x = Find_set (x);
    y = Find_set (y);
    if (Rk[x]>rk[y]) fa[y] = x;
        else {fa[x] = y;
     if (Rk[x]==rk[y])       rk[y]++;
    int main () {IO;
    Init ();
    for (int i=1; i<=n; i++) cin>>p[i].o.x>>p[i].o.y>>p[i].r;
    int e = 0,v = 0;
    Vector<point> all;
        for (int i=1; i<=n; i++) {vector<point> tmp1;
            for (int j=1; j<=n; J + +) {if (i==j) continue;
            Vector<point> TMP2 = p[i] & P[j];
            if (Tmp2.size ()) Union_set (I,J);
            Tmp1.insert (Tmp1.end (), Tmp2.begin (), Tmp2.end ());
        All.insert (All.end (), Tmp2.begin (), Tmp2.end ());
        Sort (Tmp1.begin (), Tmp1.end ());
    E + = unique (Tmp1.begin (), Tmp1.end ())-Tmp1.begin ();
    Sort (All.begin (), All.end ());
    v = unique (All.begin (), All.end ())-All.begin ();
    Set<int> C;
    for (int i=1; i<=n; i++) C.insert (Find_set (i));
    Cout<<e-v+c.size () +1<<endl;
return 0; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.