题目描述
有一个nn个点mm条边的图画在了平面上,你想知道有多少对边之间对应的线段相交。
特别地,对于图中的一对边,如果有公共点且只在对应的端点相交,那么我们不认为这对边相交。
输入描述
第一行两个整数n, m(1\leq n\leq 1000, 1\leq m\leq 2000)n,m(1≤n≤1000,1≤m≤2000),表示点数和边数。
接下来mm行,每行两个整数(u,v)(u,v)表示一条uu与vv之间的无向边,保证图中没有重边和自环。
接下来nn行,每行两个整数x_i, y_i (0\leq x_i, y_i\leq 10^9)xi,yi(0≤xi,yi≤109)表示图中第ii个顶点的坐标,保证所有的坐标两两不同。
输出描述
输出一个整数,表示答案。
样例输入 1
4 6 1 2 1 3 1 4 2 3 2 4 3 4 0 0 0 1 1 1 1 0
样例输出 1
1
施工代码:
#include<stdio.h>
#include<iostream>
using namespace std;
struct point
{
long long x,y;
};
struct Line{
int U;
int V;
};
//struct point a,b,c,d;
int mp(point a,point b,point c,point d){
double C=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
double D=(d.x-c.x)*(b.y-c.y)-(b.x-c.x)*(d.y-c.y);
if(C*D<0) return 0;
else return 1;
}
bool check(point a , point b , point c , point d){
if(!mp(a,b,c,d)) return false;
if(!mp(c,d,a,b)) return false;
//cout<<"ou"<<endl;
return true;
}
const int maxn=2010;
Line a[maxn];
point b[maxn];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
a[i].U=u;
a[i].V=v;
}
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
b[i].x=x;
b[i].y=y;
}
//cout<<"&&&&&&&&&&&&&&&"<<endl;
// for(int i=1;i<=m;i++){
// cout<<"i="<<i<<" "<<a[i].U<<' '<<a[i].V<<endl;
// }
// for(int i=1;i<=n;i++){
// cout<<b[i].x<<' '<<b[i].y<<endl;
// }
int ans=0;
for(int i=1;i<=m;i++){
for(int j=i;j<=m;j++){
//cout<<i<<endl;
if(a[i].U==a[j].U||a[i].V==a[j].V||a[i].U==a[j].V||a[i].V==a[j].U){
continue;
}
//cout<<"%%%%%%%%%%%%%"<<endl;
//cout<<"i="<<i<<' '<<"j="<<j<<endl;
//cout<<a[i].U<<' '<<a[i].V<<endl;
//cout<<a[j].U<<' '<<a[j].V<<endl;
if(check(b[a[i].U],b[a[i].V],b[a[j].U],b[a[j].V])){
ans++;
//cout<<ans<<"!"<<endl;
}
}
}
cout<<ans<<endl;
}
/*
4 6
1 2
1 3
1 4
2 3
2 4
3 4
0 0
0 999999999
999999999 999999999
999999999 0
4 2
1 2
3 4
0 2
2 2
1 0
1 2
*/