HDU--1558--Segment set--並查集***經典***,hdu並查集
Segment setTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3699 Accepted Submission(s): 1394
Problem DescriptionA segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set.
InputIn the first line there is an integer t - the number of test case. For each test case in first line there is an integer n (n<=1000) - the number of commands.
There are two different commands described in different format shown below:
P x1 y1 x2 y2 - paint a segment whose coordinates of the two endpoints are (x1,y1),(x2,y2).
Q k - query the size of the segment set which contains the k-th segment.
k is between 1 and the number of segments in the moment. There is no segment in the plane at first, so the first command is always a P-command.
OutputFor each Q-command, output the answer. There is a blank line between test cases.
Sample Input
110P 1.00 1.00 4.00 2.00P 1.00 -2.00 8.00 4.00Q 1P 2.00 3.00 3.00 1.00Q 1Q 3P 1.00 4.00 8.00 2.00Q 2P 3.00 3.00 6.00 -2.00Q 5
Sample Output
12225
AuthorLL
SourceHDU 2006-12 Programming Contest
題意:二維座標上給你n條小段,每次詢問的時候求詢問線段所有存在串連關係的線段的數量
這題很水。但是對於高中數學沒學好或者像我這種遺忘到差不多的群體來說,太糾結了。
關鍵點在於證明兩線段是否有串連關係,比如交叉,頂點相同,都算串連
由於遺忘的差不多了,就網上臨時複習了向量積,花了不少時間才消化KO。。其實網上的都是標準說法,對於我們這種不喜歡看書的孩紙來說是天書,所以我把這抽象化的東東用日常一些的語言解釋一遍。
假如兩線段的兩頂點分別是ab和cd,要麼證明他們平行且有重疊,重疊就是說其中一線段有頂點在另一線段中。或者證明他們沒有平行之後向量ab在向量ac和向量ad之間,同時向量ca在向量ca和向量cb之間,看圖。。
從途中可以看到,紅色箭頭是不符合cd在ca與cb中間的,這就是為什麼要兩個線段的端點都要取出來驗證了,還有就是平行但是不共線,或者平行共線但是因為是線段不相連,所以不能算串連,下面說公式。。。
因為線段A(x1,y1,x2,y2)(x和y表示兩個頂點的座標)斜率L=(y2 - y1)/(x2 - x1)假設要求的是ac與ad的斜率差,那麼就是Lac-Lad,然後把公式代入再展開就得到了我代碼中dd函數裡面的公式,它是用來求斜率差值的,而裡面的cmp函數中用dd函數求了4次,這是因為要直接證明ab在ac和ad中間不簡單,那麼換一方案,直接比較斜率差值,就像中左下角的那個圖,求出ac和ad的斜率差值還有bc和bd的斜率差值,如果你把線段的斜率用圖畫出來,你會發現兩個差值相乘能判斷ab和cd是否交叉,我畫個圖得了。。。
這樣就解決了ab與cd存在交叉但是ab太短了沒辦法從cd中間插過去的問題- -!確定插過去了是cmp函數中d1*d2和d3*d4都小於零的前提,d1*d2是保證ab插過cd,d3*d4是保證cd插過ab,咳咳咳,太邪惡了,baby你懂了沒?
總述一下,先考慮平行問題,平行情況下是否有重疊,然後是可能存在交叉的問題,判斷好ab從cd中間穿過且ab夠長,同時cd對ab也要判斷一次,這樣就可以AC了
注意:資料類型要弄好,因為int會把double的小數位去掉,1.1會變成1,咳咳,太可惡了,就是這個WA我到死。。總之,這個題很經典,我是這麼認為的
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node{ double x1,x2,y1,y2;}s[1111];int f[1111],num[1111];double cc=1e-7;int Find(int x){ return f[x]=(f[x]==x?x:Find(f[x]));}void Union(int x,int y){ x=Find(x); y=Find(y); if(x==y)return;//已經在一個集合中就不要重複了 f[x]=y; num[y]+=num[x];//聯絡兩個線段集合的時候順便把集合中線段數量集中起來}double Max(double a,double b){ return a>b?a:b;}double Min(double a,double b){ return a<b?a:b;}double dd(double x1,double y1,double x2,double y2,double x3,double y3)//兩線段的斜率差{ return (x2-x1)*(y3-y1)-(y2-y1)*(x3-x1);}int ddd(double x1,double y1,double x2,double y2,double x3,double y3)//共線情況下是否有重疊{ if(Max(x1,x2)>=x3&&Min(x1,x2)<=x3&&Max(y1,y2)>=y3&&Min(y1,y2)<=y3) return 1; return 0;}int cmp(node a,node b){ double d1,d2,d3,d4; d1=dd(a.x1,a.y1,a.x2,a.y2,b.x1,b.y1); d2=dd(a.x1,a.y1,a.x2,a.y2,b.x2,b.y2); d3=dd(b.x1,b.y1,b.x2,b.y2,a.x1,a.y1); d4=dd(b.x1,b.y1,b.x2,b.y2,a.x2,a.y2);//cout<<d1<<d2<<" "<<d3<<d4<<endl; if(d1*d2<0&&d3*d4<0)return 1;//cout<<"X"<<endl;//斜率差一正一負可以得出兩不共線線段交叉,上面有本人的個人看法說明 if(d1==0&&ddd(a.x1,a.y1,a.x2,a.y2,b.x1,b.y1))return 1;//cout<<"A"<<endl;//共線的時候看看是否有重疊 if(d2==0&&ddd(a.x1,a.y1,a.x2,a.y2,b.x2,b.y2))return 1;//cout<<"S"<<endl; if(d3==0&&ddd(b.x1,b.y1,b.x2,b.y2,a.x1,a.y1))return 1;//cout<<"D"<<endl; if(d4==0&&ddd(b.x1,b.y1,b.x2,b.y2,a.x2,a.y2))return 1;//cout<<"F"<<endl; return 0;}int main (void){ int t,n,i,j,k,l; char c; scanf("%d",&t); while(t--&&scanf("%d",&n)) { for(i=0;i<1111;i++)f[i]=i,num[i]=1; l=0; while(n--) { scanf("%*c%c",&c); if(c=='P') { scanf("%lf%lf%lf%lf",&s[l].x1,&s[l].y1,&s[l].x2,&s[l].y2); for(i=0;i<l;i++)//與之前記錄的所有線段匹配串連關係 if(cmp(s[i],s[l]))//如果有串連 /*cout<<"A"<<endl,*/Union(i,l);//並查集把線段聯絡起來 l++; }else { scanf("%d",&k); printf("%d\n",num[Find(k-1)]); } //for(i=0;i<l;i++)cout<<num[i]<<" ";cout<<endl; } if(t)puts("");//媽啵的,這裡沒寫PE了 } return 0;}