HDU 1665-different Digits (geometry + Euler theorem)

Source: Internet
Author: User
Tags new set

Test instructions: On a plane, given a closed stroke graph consisting of n points (4 <= n <= 300) (the first endpoint coincides with the nth endpoint), the graphic is divided into several parts.

Euler's theorem needs to be used;

Euler's theorem: the number of vertices in the graph is V, the number of sides (the number of edges in the three dimensions) is E, and the number of polygons is F, then v + f-e = 2.

If the number of polygons is asked, only the number of vertices V and the number of sides e, that is, can be obtained the answer F = e + 2-v.

(Note: Here the number of sides of E, refers to a single line, if the middle is separated by points, it is counted as a number of lines rather than a line)

The specific treatment is as follows:

1, the first enumeration of any two lines to draw the line, to see if a new intersection has been generated, which is deposited into a new set of points;

2, will be produced by the intersection of the weight (may appear three and more straight line in the case of a point);

3, the number of sides of the initial must be n-1 (because the beginning and end of the coincident), and then the new intersection of statistics to break the new edge;

4, according to the number of vertices and the number of edges to calculate the answer can be.

To apply a geometry template, the code is as follows (some notes indicate function functions):

#pragmaComment (linker, "/stack:102400000, 102400000")#include<cstdio>#include<cstring>#include<cctype>#include<cstdlib>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<Set>#include<map>#include<deque>#include<queue>#include<stack>#include<list>#defineFin freopen ("In.txt", "R", stdin)#defineFout freopen ("OUT.txt", "w", stdout)#definePR (x) cout << #x << ":" << x << ""#definePRLN (x) cout << #x << ":" << x << Endl#defineMin (A, B) a < b? A:b#defineMax (A, B) a < b? B:atypedefLong Longll;typedef unsignedLong LongLlu;Const intInt_inf =0x3f3f3f3f;Const intInt_m_inf =0x7f7f7f7f;Constll ll_inf =0x3f3f3f3f3f3f3f3f;Constll ll_m_inf =0x7f7f7f7f7f7f7f7f;Const DoublePI = ACOs (-1.0);Const intDr[] = {0,0, -1,1, -1, -1,1,1};Const intDc[] = {-1,1,0,0, -1,1, -1,1};Const intMOD =10000;using namespacestd;#defineNdebug#include<cassert>Const intMAXN = -+Ten;Const intMaxt =10000+Ten;structpoint{Doublex, y; Point (Doublex =0,Doubley =0): X (x), Y (y) {}};typedef point Vector; Vectoroperator+ (vector A, vector B) {returnVector (a.x + b.x, A.Y +b.y);} Vectoroperator-(Point A, point B) {returnVector (a.x-b.x, A.Y-b.y);} Vectoroperator* (Vector A,DoubleP) {returnVector (a.x * p, A.Y *p);} Vectoroperator/(Vector A,DoubleP) {returnVector (a.x/p, A.Y/p);}BOOL operator< (ConstPoint &a,ConstPoint &b) {    returna.x < b.x | | (a.x = = b.x && A.y <b.y);}Const DoubleEPS = 1e-8;intDCMP (Doublex) {    if(Fabs (x) < EPS)return 0; Else  returnX <0? -1:1;}BOOL operator== (ConstPoint &a,ConstPoint &b) {    returnDCMP (a.x-b.x) = =0&& dcmp (a.y-b.y) = =0;}DoubleDot (vector A, vector B) {returna.x * b.x + a.y *b.y;}DoubleCross (vector A, vector B) {returna.x * b.y-b.x *a.y;}//To find the intersection of two segments, the first segment starts with P, the vector is V, and the second is Q and W (the intersection is guaranteed to be called)Point getlineintersection (point P, Vector v, point Q, vector w) {vector u= P-Q; Doublet = Cross (w, u)/Cross (V, W); returnP + V *t;}//determine if there is an intersection between the A1A2 and the b1b2 two line segmentsBOOLSegmentproperintersection (Point &a1, point &a2, point &b1, Point &B2) {    DoubleC1 = Cross (A2-A1, B1-A1); DoubleC2 = Cross (a2-a1, B2-A1); DoubleC3 = Cross (B2-B1, A1-B1); DoubleC4 = Cross (b2-b1, A2-B1); returnDCMP (C1) * DCMP (C2) <0&& dcmp (C3) * DCMP (C4) <0;}//determine if P is on the A1A2 segment (not at the end)BOOLonsegment (Point P, point A1, point A2) {returnDCMP (Cross (a1-p, a2-p)) = =0&& dcmp (Dot (A1-p, a2-p)) <0;}  Point P[MAXN]; //the input pointPoint V[MAXN * MAXN];//the points that are entered, and the points at which the segments intersect//Euler's theorem: the number of vertices of the plan is V, the number of edges (the number of angles in the three dimensions) is E, and the number of polygons is F, then v + f-e = 2//the answer is F = e + 2-vintMain () {intN, Kase =0;  while(SCANF ("%d", &n) = =1&&N) {         for(inti =0; I < n; ++i) {scanf ("%LF%LF", &p[i].x, &p[i].y); V[i]=P[i]; }        intc = N-1, E = n-1; //enumerates each of the two segments, checks for intersections, and joins C arrays if they intersect         for(inti =0; I < n-1; ++i) for(intj = i +1; J < N-1; ++j)if(Segmentproperintersection (P[i], p[i +1], P[j], P[j +1])) V[c+ +] = getlineintersection (P[i], p[i +1]-p[i], p[j], P[j +1] -P[j]); //remove vertices that intersect repeatedlySort (V, v +c); C= Unique (V, v + C)-v; //if the newly added point splits the original segment, the number of edges increases         for(inti =0; I < C; ++i) for(intj =0; J < N-1; ++j)if(Onsegment (V[i], p[j], P[j +1]))  ++e; printf ("Case %d:there is%d pieces.\n", ++kase, E +2-c); }    return 0;}

HDU 1665-different Digits (geometry + Euler theorem)

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: info-contact@alibabacloud.com 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.