Codeforces 223E ry Graph Theory network stream idea

Source: Internet
Author: User

Question: give you a bunch of coordinate points, and then tell you the connection between them. All sides are bidirectional and the graph is connected. Number of edges, points in all 10 W
Then start to ask, each question is all vertices of a polygon in the input graph, and then ask you how many points (including points on the boundary) exist in the polygon)
After reading it, I understand that the key is to use the stream idea. When a simple polygon is selected, the number of points inside is equal to the flow traffic and the sum of the flow traffic, the traffic is poor. How can this problem be defined freely?
Statistics cannot be violent.
You can use the polar order of coordinates to process a prefix and quickly obtain the answer.
For details, see the code.
It's a long time to figure out which side of the interior of a polygon is involved.
[Cpp]
# Include <cmath>
# Include <cstring>
# Include <cstdlib>
# Include <cstdio>
# Include <algorithm>
# Include <map>
# Include <set>
# Include <vector>
Using namespace std;
# Define MP make_pair
# Define PB push_back
Const int maxn = 100010;
Const double inf = 1e10;
Struct Point {
Double x, y;
Point (){};
Point (double sx, double sy): x (sx), y (sy ){}
} P [maxn];
Double operator/(const Point & B, const Point & ){
Return atan2 (B. y-a.y, B. x-a.x );
}
Bool operator <(const Point & a, const Point & B ){
If (a. x! = B. x)
Return a. x <B. x;
Return a. y <B. y;
}
Inline double cross (double x1, double y1, double x2, double y2 ){
Return x1 * y2-x2 * y1;
}
Inline double chacha (Point s, Point a, Point B ){
Return cross (a. x-s.x, a. y-s.y, B. x-s.x, B. y-s.y );
}
Struct Edge {
Double ang;
Int;
Edge (){}
Edge (double a, int t): ang (a), to (t ){};
Bool operator <(const Edge & cmp) const {
Return ang <cmp. ang;
}
};
Vector <Edge> edge [maxn];
 
Map <pair <int, int>, int> sum, flow;
 
Int all [maxn];
 
Int n, m;
 
Inline void add_edge (int u, int v)
{
Edge k;
K. to = v; k. ang = p [v]/p [u];
Edge [u]. PB (k );
Flow [MP (u, v)] = 0;
}
Bool vis [maxn];
Int dfs (int u, int f)
{
Vis [u] = true;
Int cnt = 1;
For (vector <Edge >:: iterator it = edge [u]. begin (); it! = Edge [u]. end (); it ++)
{
Int v = it->;
If (! Vis [v]) cnt + = dfs (v, u );
}
If (f)
{
Flow [MP (f, u)] + = cnt;
Flow [MP (u, f)]-= cnt;
}
Return cnt;
}
 
Int ss [maxn], tt [maxn];
 
Vector <int> cut;
Inline int gao (int a, int B, int c)
{
Double bb = p [B]/p [a], cc = p [c]/p [a];
If (bb <cc)
{
Return sum [MP (a, c)]-sum [MP (a, B)]-flow [MP (a, c)];
}
Else
{
Return all [a] + sum [MP (a, c)]-sum [MP (a, B)]-flow [MP (a, c)];
}
}
Void solve ()
{
Int ans = 0, num = cut. size (), I;
Double s = 0; p [0] = Point (0, 0 );
For (I = 0; I <num; I ++)
S + = chacha (p [0], p [cut [I = 0? Num-1: I-1], p [cut [I]);
If (s> 0) reverse (cut. begin (), cut. end (); // clockwise
For (I = 0; I <num; I ++)
{
Int tmp = gao (cut [I], cut [(I = 0? Num-1: i-1)], cut [(I + 1) % num]);
Ans + = tmp;
}
Printf ("% d \ n", ans + num );
}
Int main ()
{
Int u, v, q, k, I, j, leftmost, T, cir, c;
Scanf ("% d", & n, & m );
For (I = 1; I <= m; I ++) scanf ("% d", & ss [I], & tt [I]);
For (I = 1; I <= n; I ++) scanf ("% lf", & p [I]. x, & p [I]. y );
For (I = 1; I <= m; I ++)
{
Add_edge (ss [I], tt [I]);
Add_edge (tt [I], ss [I]);
}
P [T = n + 1] = Point (-inf, 0 );
For (leftmost = I = 1; I <= n; I ++)
If (p [I] <p [leftmost]) leftmost = I;
Add_edge (T, leftmost );
Add_edge (leftmost, T );
Dfs (T, 0 );
For (I = 1; I <= n + 1; I ++) sort (edge [I]. begin (), edge [I]. end ());
For (I = 1; I <= n + 1; I ++)
{
Int pre = I;
Sum [MP (I, I)] = 0;
For (vector <Edge >:: iterator it = edge [I]. begin (); it! = Edge [I]. end (); it ++)
{
All [I] + = flow [MP (I, it-> to)];
Sum [MP (I, it-> to)] = sum [MP (I, pre)] + flow [MP (I, it-> to)];
Pre = it->;
}
}
Scanf ("% d", & q );
While (q --)
{
Scanf ("% d", & k); cut. clear ();
For (I = 1; I <= k; I ++) scanf ("% d", & c), cut. PB (c );
Solve ();
}
Return 0;
}

 

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.