HDU - 4305 - Lightning 生成树计数 + 叉积判断三点共线
题意:
比较裸的一道生成树计数问题,构造Krichhoof矩阵,求解行列式即可。但是这道题还有一个限制,就是给定的坐标中,两点连线中不能有其他的点,否则这两点就不能连接。枚举点,用叉积计算是否共线即可。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 10007; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 500; int n,r; int a[maxn][maxn],fa[maxn]; struct node { int x,y; }p[maxn]; int find(int x){ if(fa[x] == x)return x; return fa[x] = find(fa[x]); } void uni(int x,int y){ int px = find(x); int py = find(y); fa[px] = py; } void cal(){ ll ans = 1;int sign = 0; for(int i=1; i<=n; i++){ //当前行 for(int j=i+1; j<=n; j++){ int x = i, y = j; while(a[y][i]){ //利用gcd的方法,不停地进行辗转相除,达到消去其他行对应列元素的目的 ll t = a[x][i] / a[y][i]; for(int k=i; k<=n; k++) a[x][k] = (a[x][k] - a[y][k]*t)%mod; swap(x,y); } if(x != i){ //奇数次交换,则D=-D'整行交换 for(int k = 1; k<=n; k++){ swap(a[i][k], a[x][k]); } sign ^= 1; } } if(a[i][i] == 0){ //斜对角中有一个0,则结果为0 puts("0"); return; } else ans = ans * a[i][i] %mod; } if(sign) ans *= -1; if(ans < 0) ans += mod; printf("%lld\n", ans); } double dis(int i,int j){ return sqrt(1.0*(p[i].x - p[j].x)*(p[i].x - p[j].x) + 1.0*(p[i].y - p[j].y)*(p[i].y - p[j].y)); } bool check(int i,int k,int j){ return ((p[j].x - p[k].x)*(p[j].y - p[i].y) == (p[j].x - p[i].x)*(p[j].y - p[k].y)) \ &&(max(p[i].x,p[j].x) >= p[k].x) &&(min(p[i].x,p[j].x) <= p[k].x)\ &&(max(p[i].y,p[j].y) >= p[k].y) &&(min(p[i].y,p[j].y) <= p[k].y); } int main(){ int t; scanf("%d", &t); while(t--){ memset(a,0,sizeof(a)); scanf("%d%d", &n, &r); for(int i=1; i<=n; i++)fa[i] = i; for(int i=1; i<=n; i++){ scanf("%d%d", &p[i].x, &p[i].y); } for(int i=1; i<=n; i++){ for(int j=1; j<i; j++){ int ok = 1; for(int k=1; k<=n; k++){ if(k==i||k==j)continue; if(check(i,k,j)){ok=0;break;} } if(ok && dis(i,j) <= r){ a[i][j] = a[j][i] = -1; a[i][i]++,a[j][j]++; uni(i,j); } } } int c = 0; for(int i=1; i<=n; i++){ if(fa[i] == i)c++; } if(c!=1)puts("-1"); else { n--; cal(); } } return 0; }
skr