标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4479 Accepted Submission(s): 1672
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> using namespace std; const int N = 1005; struct Point { double x,y; }; struct Line { Point a,b; } line[N]; double cross(Point a,Point b,Point c) { return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x); } bool isCross(Point a, Point b, Point c, Point d) { if (cross(c, b, a)*cross(b, d, a)<0)return false; if (cross(a, d, c)*cross(d, b, c)<0)return false; return true; } int father[N],cnt[N]; int _find(int x) { if(x==father[x]) return x; return _find(father[x]); } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { int n; scanf("%d",&n); int k =1; for(int i=1; i<=N; i++) { father[i]=i; cnt[i]=1; } while(n--) { char c[2]; scanf("%s",c); if(c[0]==‘P‘) { scanf("%lf%lf%lf%lf",&line[k].a.x,&line[k].a.y,&line[k].b.x,&line[k].b.y); k++; for(int i=1; i<k; i++) { if(isCross(line[i].a,line[i].b,line[k-1].a,line[k-1].b)) { int x = _find(k-1); int y = _find(i); if(x!=y) { father[x] = y; cnt[y]+=cnt[x]; } } } } if(c[0]==‘Q‘) { int num; scanf("%d",&num); /* for(int i=1; i<k; i++) //没想明白 { if(isCross(line[i].a,line[i].b,line[num].a,line[num].b)) { int x = _find(num); int y = _find(i); if(x!=y) { father[x] = y; cnt[y]+=cnt[x]; } } }*/ printf("%d\n",cnt[_find(num)]); } } if(tcase) printf("\n"); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5475142.html