Пример #1
0
 int swimInWater(vector<vector<int>>& grid) {
     const int n = grid.size();
     vector<pair<int, int>> positions(n * n);
     for (int i = 0; i < n; ++i) {
         for (int j = 0; j < n; ++j) {
             positions[grid[i][j]] = {i, j};
         }
     }
     static const vector<pair<int, int>> directions{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
     UnionFind union_find(n * n);
     for (int elevation = 0; elevation < positions.size(); ++elevation) {
         int i, j;
         tie(i, j) = positions[elevation];
         for (const auto& dir : directions) {
             int x = i + dir.first;
             int y = j + dir.second;
             if (0 <= x && x < n &&
                 0 <= y && y < n &&
                 grid[x][y] <= elevation) {
                 union_find.union_set(i * n + j, x * n + y);
                 if (union_find.find_set(0) == union_find.find_set(n * n - 1)) {
                     return elevation;
                 }
             }
         }
     }
     return n * n - 1;
 }
int main(int argc,char** argv){
	// Initialize ROS
	ros::init (argc, argv, "test_unionfind");
	ros::NodeHandle* nh = new ros::NodeHandle("~");
	ros::Publisher pub = nh->advertise<sensor_msgs::PointCloud2> ("inliers", 1);

	pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_in( new pcl::PointCloud<pcl::PointXYZRGBA>(10,10));
	std::vector<int> inliers;

	for(int col = 0 ; col < 10 ; col ++){
		for (int row =0 ; row <10; row ++){
			cloud_in->at(col,row).x = col;
			cloud_in->at(col,row).y = row;
		}
	}


	//first group
	cloud_in->at(2,1).a = 1;
	cloud_in->at(2,2).a = 1;
	cloud_in->at(2,0).a = 1;
	cloud_in->at(1,1).a = 1;
	cloud_in->at(3,1).a = 1;

	//second group
	cloud_in->at(5,4).a = 1;
	cloud_in->at(6,4).a = 1;
	cloud_in->at(6,5).a = 1;

	//third group
	cloud_in->at(4,7).a = 1;

	cloud_in->at(5,6).a = 1;
	//cloud_in->at(5,7).a = 1;
	cloud_in->at(6,6).a = 1;

	union_find(cloud_in,inliers);


	sensor_msgs::PointCloud2 msg_cloud_inliers;
	pcl::PointCloud<pcl::PointXYZRGBA> cloud_inliers;
	//for (size_t i = 0; i < inliers->size (); ++i)
	//	cloud_inliers.insert(cloud_inliers.end(),cloud->points[(*inliers)[i]]);
	for (size_t i = 0; i < inliers.size (); ++i)
		cloud_inliers.insert(cloud_inliers.end(),cloud_in->points[inliers[i]]);

	// convert and Publish the data
	//pcl::toROSMsg(cloud_inliers,msg_cloud_inliers);
	pcl::toROSMsg(cloud_inliers,msg_cloud_inliers);
	//pcl::toROSMsg(*line1_cloud,msg_cloud_inliers);
	//modify the frame id
	msg_cloud_inliers.header.frame_id="/world";
	while(nh->ok()){
	pub.publish (msg_cloud_inliers);
	}

	return 0;
}
Пример #3
0
 vector<int> findRedundantConnection(vector<vector<int>>& edges) {
     UnionFind union_find(edges.size() + 1);
     for (const auto& edge : edges) {
         if (!union_find.union_set(edge[0], edge[1])) {
             return edge;
         }
     }
     return {};
 }
Пример #4
0
int main() {
	UnionFind union_find(kUnionArraySize);
	union_find.Union(5, 3);
	union_find.Union(1, 2);
	union_find.Union(1, 6);
	union_find.Union(1, 3);	
	std::cout << "Is IsConnected : " << union_find.IsConnected(5, 3) << std::endl;
	std::cout << "Is IsConnected : " << union_find.IsConnected(1, 5) << std::endl;
	std::cout << "Is IsConnected : " << union_find.IsConnected(5, 8) << std::endl;
}
Пример #5
0
int main()
{
	int a,b;
	char p1[256],p2[256];
	while(scanf("%d %d\n",&n,&m)!=EOF)
	{
		for(int i = 0;i<n;i++)
		{
			scanf("%s\n",names[i]);

			
		}

		for(int i = 0;i<m;i++)
		{
			scanf("%s %s\n",p1,p2);

			for(int j = 0; j<n; j++)
			{
				if(strcmp(names[j],p1)==0)
				{
					a = j;
					break;
				}
			}

			for(int j = 0; j<n; j++)
			{
				if(strcmp(names[j],p2)==0)
				{
					b= j;
					break;
				}
			}


			list[a][len[a]]=b;
			len[a]++;
			list[b][len[b]]=a;
			len[b]++;

		}
		int max = -1;
		for(int i = 0; i<n;i++)
		{
			int v = union_find(i);
			if(v>max)
				max = v;
		}

		printf("%d\n",max);
	}

	return 0;
}
Пример #6
0
 vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
     vector<int> results;
     if (n == 0) {
         return results;
     }
     if (m == 0) {
         return results;
     }
     if (positions.size() == 1) {
         results.push_back(1);
         return results;
     }
     vector<vector<bool>> grid(m, vector<bool>(n));
     for (int i = 0; i < grid.size(); i++) {
         for (int j = 0; j < grid[0].size(); j++) {
             grid[i][j] = 0;
         }
     } 
     UnionFind union_find(n * m);
     int size;
     for (int x = 0; x < positions.size(); x++) {
         if (x == 0) {
             union_find.set_count(x + 1);
         } else {
             union_find.set_count(size + 1);
         }
         
        
         grid[positions[x].first][positions[x].second] = 1;
     
         int i = positions[x].first;
         int j = positions[x].second;
         
         if (i > 0 && grid[i - 1][j]) {
             union_find.connect(i * n + j, (i - 1) * n + j);
         }
         if (i < m - 1 && grid[i + 1][j]) {
             union_find.connect(i * n + j, (i + 1) * n + j);
         }
         if (j > 0 && grid[i][j - 1]) {
             union_find.connect(i * n + j, i * n + j - 1);
         }
         if (j < n - 1 && grid[i][j + 1]) {
             union_find.connect(i * n + j, i * n + j + 1);
         }
         results.push_back(union_find.query());
         size = union_find.query();
     }
     
     return results;
 }
Пример #7
0
Файл: bool.c Проект: SamB/racket
static int union_check(Scheme_Object *obj1, Scheme_Object *obj2, Equal_Info *eql) 
{
  if (eql->depth < 50) {
    if (!eql->next_next)
      eql->depth += 2;
    return 0;
  } else {
    Scheme_Hash_Table *ht = eql->ht;
    if (!ht) {
      ht = scheme_make_hash_table(SCHEME_hash_ptr);
      eql->ht = ht;
    }
    obj1 = union_find(obj1, ht);
    obj2 = union_find(obj2, ht);

    if (SAME_OBJ(obj1, obj2))
      return 1;

    scheme_hash_set(ht, obj2, obj1);

    return 0;
  }
}
Пример #8
0
int main(void){
	char corq, line[1024];
	for(int t = 1; --t || scanf("%d", &t) == 1; ){
		int n, s = 0, u = 0;
		scanf("%d\n", &n);
		for(int i = 1; i <= n; ++i) p[i] = i;
		for(int a, b; fgets(line, 1024, stdin) && sscanf(line, " %c %d %d", &corq, &a, &b) == 3; ){
			if(corq == 'c')	union_find(a, b);
			else {
				if(root(a) == root(b)) ++s;
				else ++u;
			}
		}
		printf("%d,%d\n", s, u);
		if(t > 1) puts("");
	}
	return 0;
}
Пример #9
0
int union_find(int v)
{

	if(visited[v]=='1')
		return 0;

	visited[v] = '1';
	int w = 1;

	for(int i = 0;i<len[v];i++)
	{

		w += union_find(list[v][i]);
		
	}

	return w;

}
Пример #10
0
    vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
        vector<int> cand1, cand2;
        unordered_map<int, int> parent;
        for (const auto& edge : edges) {
            if (!parent.count(edge[1])) {
                parent[edge[1]] = edge[0];
            } else {
                cand1 = {parent[edge[1]], edge[1]};
                cand2 = edge;
            }
        }

        UnionFind union_find(edges.size() + 1);
        for (const auto& edge : edges) {
            if (edge == cand2) {
                continue;
            }
            if (!union_find.union_set(edge[0], edge[1])) {
                return cand2.empty() ? edge : cand1;
            }
        }
        return cand2;
    }