标签:
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 18066 | Accepted: 7618 |
Description
Input
Output
Sample Input
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4
Sample Output
FAIL SUCCESS
今天学了并查集,并查集的模板题。
#include <iostream> #include <cstdio> #include <string> #include <queue> #include <vector> #include <map> #include <algorithm> #include <cstring> #include <cctype> #include <cstdlib> #include <cmath> #include <ctime> using namespace std; const int SIZE = 1005; int FATHER[SIZE],RANK[SIZE]; int N,D; pair<int,int> G[SIZE]; vector<int> OK; double DIS[SIZE][SIZE]; void ini(int); int find_father(int); void unite(int,int); bool same(int,int); double dis(pair<int,int>,pair<int,int>); int main(void) { int x,y; char ch; while(scanf("%d%d",&N,&D) != EOF) { ini(N); for(int i = 1;i <= N;i ++) scanf("%d%d",&G[i].first,&G[i].second); for(int i = 1;i <= N;i ++) for(int j = 1;j <= N;j ++) DIS[i][j] = dis(G[i],G[j]); while(scanf(" %c",&ch) != EOF) if(ch == ‘O‘) { scanf("%d",&x); for(int i = 0;i < OK.size();i ++) if(DIS[x][OK[i]] <= D) unite(x,OK[i]); OK.push_back(x); } else { scanf("%d%d",&x,&y); printf("%s\n",same(x,y) ? "SUCCESS" : "FAIL"); } } return 0; } void ini(int n) { for(int i = 1;i <= n;i ++) { FATHER[i] = i; RANK[i] = 0; } } int find_father(int n) { if(FATHER[n] == n) return n; return FATHER[n] = find_father(FATHER[n]); } void unite(int x,int y) { x = find_father(x); y = find_father(y); if(x == y) return ; if(RANK[x] < RANK[y]) FATHER[x] = y; else { FATHER[y] = x; if(RANK[x] == RANK[y]) RANK[y] ++; } } bool same(int x,int y) { return find_father(x) == find_father(y); } double dis(pair<int,int> a,pair<int,int> b) { return sqrt(pow(a.first - b.first,2) + pow(a.second - b.second,2)); }
POJ 2236 Wireless Network (并查集)
标签:
原文地址:http://www.cnblogs.com/xz816111/p/4529031.html