POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 並查集

來源:互聯網
上載者:User

標籤:acm   編程   poj   並查集   


POJ 2236 Wireless Network

http://poj.org/problem?id=2236

題目大意:

給你N台損壞的電腦座標,這些電腦只能與不超過距離d的電腦通訊,但如果x和y均能C通訊,則x和y可以通訊。現在給出若干個操作,

O p 代表修複編號為p的電腦

S p q代表詢問p和q是不是能通訊。

思路:

並查集即可。。

如果修複了一台電腦,則把與它相連距離不超過d的且修複了的放在一個集合裡面。


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=1000+10;int fa[MAXN],n,d;bool canuse[MAXN];int find(int cur){return fa[cur]<0? cur: fa[cur]=find(fa[cur]);}struct computers{int x,y;}a[MAXN];bool ok(int x1,int y1,int x2,int y2){return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) <= d*d;}int main(){memset(fa,-1,sizeof(fa));memset(canuse,0,sizeof(canuse));scanf("%d%d",&n,&d);for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);char cmd[10];int p,q;while(~scanf("%s",cmd)){if(cmd[0]==‘O‘){scanf("%d",&p);int root=find(p);for(int i=1;i<=n;i++){if(i==p) continue;if(ok(a[i].x,a[i].y,a[p].x,a[p].y) && canuse[i]){int root1=find(i);int root2=find(p);if(root1!=root2)fa[root1]=root2;}}canuse[p]=true;}else{scanf("%d%d",&p,&q);int rootp=find(p);int rootq=find(q);if(rootp==rootq)printf("SUCCESS\n");elseprintf("FAIL\n");}}return 0;}


POJ 1703 Find them, Catch them 

http://poj.org/problem?id=1703

題目大意:

xx城市有兩個幫派,給你m條資訊,D a b表示a和b不在一個幫派裡。A a b時要求輸出a和b是不是在一個幫派裡。(在/不在/不確定)

思路:

和這題POJ 1182 食物鏈 並查集差不多。

劃分為兩個集合,給出D a b 說明a和b不在一個集合,那麼就合并a和b+n,b和a+n。


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=200000+10;int fa[MAXN];int find(int cur){return cur==fa[cur]? cur: fa[cur]=find(fa[cur]);}void union_set(int a,int b){int root_a=find(a);int root_b=find(b);if(root_a==root_b)return;fa[root_a]=root_b;}int main(){int T;scanf("%d",&T);while(T--){int n,m;scanf("%d%d",&n,&m);int num=n*2;for(int i=1;i<=num;i++)fa[i]=i;char cmd[5];int a,b;for(int i=0;i<m;i++){scanf("%s %d%d",cmd,&a,&b);if(cmd[0]==‘A‘) {if(find(a)==find(b))printf("In the same gang.\n");else if(find(a)==find(b+n) || find(b)==find(a+n))printf("In different gangs.\n");else printf("Not sure yet.\n");}else{union_set(a,b+n);union_set(a+n,b);}}}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.