POJ 2236 Wireless Network

來源:互聯網
上載者:User

標籤:os   io   for   ar   amp   size   ios   har   

思路詳見課本 P 213

思路:直接用並查集,最後看 p 和 q 是否 在一個 集合中 即可。屬於同一集合,則 可以通訊;否則失敗。

 

#include<iostream>#include<cstring>using namespace std;const int maxn=1000 +100;int set[maxn];int xx[maxn];int yy[maxn];bool valid[maxn];int n,d;int set_find(int d)//路徑壓縮的 合并樹根 的函數{if(set[d]<0)return d;return set[d]=set_find(set[d]);}void join(int x,int y){x=set_find(x);y=set_find(y);if(x!=y)//-----------此處這個判斷千萬不能少,否則在 set_find()函數中,出現死迴圈(由於節點 自己 指向 自己,找不到 結束條件 (即樹根))//------------提交會出現runtime error  set[x]=y;}int in(int i,int p)//判斷i 是否在 p的範圍內{if(  (xx[i]-xx[p]) * (xx[i]-xx[p]) + (yy[i]-yy[p]) * (yy[i]-yy[p]) <= d*d  )return 1;return 0;}void repair(int p)//修複 p{valid[p]=1;int i;for(i=1;i<=n;i++) if( valid[i] &&in(i,p) &&i!=p)join(i,p);}void search(int p,int q) //查看 p  和 q是否 可以 通訊{if( set_find(p)==set_find(q) )cout<<"SUCCESS"<<endl;elsecout<<"FAIL"<<endl;}int main(){memset(set,-1,sizeof(set));memset(xx,0,sizeof(xx));memset(yy,0,sizeof(yy));memset(valid,0,sizeof(valid));cin>>n>>d;int i;for(i=1;i<=n;i++)cin>>xx[i]>>yy[i];char c;while(cin>>c){if(c==‘O‘){int p;cin>>p;repair(p);}else if(c==‘S‘){int p,q;cin>>p>>q;search(p,q);}}return 0;}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.