Sub I, I love it

Link: https: //ac.nowcoder.com/acm/contest/3570/I
Source: Cattle-off network

Title Description

Recently ZSC and his girlfriend NULL fight. ZSC is a straight man because he did not know how to do, so he sought the help of Leizi brother's love child. "Compared to Leizi, I need a girlfriend" / doge.
Contradiction roads like a line, correction together, more and more chaotic, the contradiction can be seen as a NULL and ZSC-dimensional Cartesian coordinate system, there are N line segments in the plane, the coordinates of two end points of the line segment are pi_x, pi_y and qi_x, qi_y. When the two segments have at least one common point, they are considered to be contiguous. They often have an associated, so that two segments connected together by line segments are also linked.
Love child of every prayer Leizi brother can eliminate a line and all the segments connected to it, since the love of a child Leizi brother particularly busy, and now he to you for help. "Can you help me, I want to know my to pray at least a few times in order to eliminate all segments. " Learned algorithms you can help Leizi brother obtained the least number of it?
Input Description:
input of the first line is an integer N (1≤N≤4 * 103), represents the number of segments, followed by N rows, each row four integers pi_x, pi_y and qi_x, qi_y showing the two segments endpoint coordinates.
Description Output: output requires a minimum number of prayers.

示例1
输入

复制
4
0 0 6 0
6 -4 6 4
0 4 4 4
2 2 2 6
输出
2

Explanation
Here Insert Picture Description

示例2
输入

复制
3
0 1 1 0
0 2 2 0
0 3 3 0
输出

复制
3
说明

Here Insert Picture Description

示例3
输入

复制
29
1 8 2 9
2 9 11 9
11 9 9 8
9 8 1 8
1 6 2 7
2 7 2 8
3 6 5 8
5 6 6 7
6 7 6 8
7 6 9 8
7 7 8 8
1 3 1 5
1 5 3 5
3 5 3 3
3 3 1 3
2 1 2 3
0 2 2 2
2 2 4 2
1 -1 2 1
3 -1 2 1
5 3 5 5
5 5 7 5
7 5 7 3
7 3 5 3
6 1 6 3
4 2 6 2
6 2 8 2
5 -1 6 1
7 -1 6 1
输出

复制
2

Explanation

Here Insert Picture Description

Ideas are as follows

Meaning of the questions: Title given endpoint n straight lines, every time through prayer, you can put a line segment which intersects are removed, we need to ask how many times we can pray, all the segments are eliminated, then we will consider two question: how to determine straight lines intersect, how once all the straight line intersecting both eliminated. For the first question I did not understand the big brother to the solution to a problem, but for the second question, we can clearly see the disjoint-set can be solved by determining the intersection of the line, the line of intersection of all as a collection of so there are several collections we need to pray several times
and check set summary portal

Solution to a problem as follows (with the posted big brother code Dramas)

#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define db double
#define gcd __gcd
#define pb push_back
#define lowbit(x) (x & (-x))
#define PII  pair<int, int>
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << endl
#define rep(i, a, b) for(__typeof(b) i = a; i <= (b); i++)
#define Rep(i, a, b) for(__typeof(a) i = a; i >= (b); i--)
#define Mod(a,b) a<b?a:a%b+b
template<class T> inline T qmin(T a, T b) { return a < b ? a : b; }
template<class T> inline T qmax(T a, T b) { return a > b ? a : b; }
typedef long long ll;
typedef unsigned long long ull;
const db PI = acos(-1);
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 1;
const int maxn = (int)1e5 + 5;//remember to modify it, No RE&nbs***bsp;MLE
const ll INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
#define cross(p1, p2, p3) ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y))
#define cross0p(p1, p2, p3) sign(cross(p1,p2,p3))
const db eps = 1e-9;
inline int sign(db a) { return a < -eps ? -1: a > eps; }
inline int cmp(db a, db b) { return sign(a-b); }
struct P{
    db x, y;
    P() {}
    P(db _x, db _y): x(_x), y(_y) {}
    P operator+(P p) { return {x + p.x, y + p.y}; }
    P operator-(P p) { return {x - p.x, y - p.y}; }
    P operator*(db d) { return {x * d, y * d}; }
    P operator/(db d) { return {x / d, y / d}; }
    bool operator<(P p)const {
        int c = cmp(x, p.x);
        if(c) return c == -1;
        return cmp(y, p.y) == -1;
    }
    bool operator==(P o) const {
        return cmp(x, o.x) == 0 && cmp(y, o.y) == 0;
    }
    db distTo(P p) { return (*this - p).abs(); }
    db abs() { return sqrt(abs2()); }
    db abs2() { return x * x + y * y; }
    db dot(P p) { return x * p.x + y * p.y; }
    db det(P p) { return x * p.y - y * p.x; }
};
bool intersect(db l1, db r1, db l2, db r2)
{
    if(l1 > r1)swap(l1, r1); if(l2 > r2) swap(l2, r2);
    return !( cmp(r1, l2) == -1 || cmp(r2, l1) == -1);
}
bool isSS(P p1, P p2, P q1, P q2){
    return intersect(p1.x, p2.x, q1.x, q2.x) && intersect(p1.y, p2.y, q1.y, q2.y) && cross0p(p1, p2, q1) * cross0p(p1, p2, q2) <= 0 && cross0p(q1, q2, p1) * cross0p(q1, q2, p2) <= 0;
}
//并查集开始
int f[maxn];
int find(int x)     //递归查找x元素的父节点
{
    if(f[x] == x)
        return x;
    return f[x] = find(f[x]);   //注意这里在查找父节点的时候 已经把节点的路径进行了压缩(进行了优化)
}
void Union(int x,int y)
{
    int fx  = find(x),fy = find(y);
    if(fx != fy) f[fx] = fy;        //x,y是是有关系的,但是他们的父节点不同所以要 把其中的一个赋接待你作为子节点 拼接到临一个父节点上
}                                   //这样x、 y 就有了相同的父节点,就属于同一个集合了
int main()
{
    int n;
    P p[10005], q[10005];
    scanf("%d", &n);
    for(int i = 1 ; i <= n ; i++){
        f[i] = i;           //并查集元素的初始化
        scanf("%lf %lf %lf %lf", &p[i].x, &p[i].y, &q[i].x, &q[i].y);
    }
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            if(isSS(p[i], q[i], p[j], q[j]))Union(i, j);//如果为真i,j 是有关系的,所以他们应该在同一个集合中
        }
    }
    int ans = 0;
    for(int i = 1 ; i <= n ; i++)if(f[i] == i)ans++;
    printf("%d\n", ans);
    return 0;
}
Published 73 original articles · won praise 100 · Views 2696

Guess you like

Origin blog.csdn.net/qq_34261446/article/details/103793572