예제 #1
0
int main(int argc, char const *argv[]) {
    ios::sync_with_stdio(false);

    int t;
    DisjointSet *D;
    cin >> t;
    for (int cs = 0; cs < t; ++cs) {
        int n;
        double d;
        cin >> n >> d;
        VD x(n), y(n);
        for (int i = 0; i < n; ++i) {
            cin >> x[i] >> y[i];
        }
        D = new DisjointSet(n);
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) if ((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) <= d * d) {
                    D->Union(i, j);
                }
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            ans += D->parent[i] == i;
        }
        cout << "Case " << cs + 1 << ": " << ans << endl;
    }

    return 0;
}
예제 #2
0
int main(int argc, char const *argv[]) {
    ios::sync_with_stdio(false);

    int t;
    DisjointSet *D;
    cin >> t;
    for (int cs = 0; cs < t; ++cs) {
        int n, m;
        cin >> n >> m;
        D = new DisjointSet(n);
        for (auto& a : D->total) {
            cin >> a;
        }
        for (int i = 0; i < m; ++i) {
            int a, b;
            cin >> a >> b;
            D->Union(a, b);
        }
        string ans = "POSSIBLE";
        for (int i = 0; i < n; ++i) if (D->Find(i) == i && D->total[i] != 0) {
                ans = "IMPOSSIBLE";
            }
        cout << ans << endl;
    }

    return 0;
}
예제 #3
0
int main(int argc, char const *argv[]) {
    ios::sync_with_stdio(false);

    int n, m;
    DisjointSet *D;
    while (cin >> n >> m && n != 0) {
        D = new DisjointSet(n);
        for (int i = 0; i < m; ++i) {
            int k;
            cin >> k;
            int a = -1, b;
            for (int j = 0; j < k; ++j) {
                cin >> b;
                if (a != -1) {
                    D->Union(a, b);
                }
                a = b;
            }
        }
        int suspect = 0;
        for (int i = 0; i < n; ++i) {
            suspect += D->Find(i) == D->Find(0);
        }
        cout << suspect << endl;
    }

    return 0;
}
예제 #4
0
 ll MST (vector <pair <ll, PI>> *mst = NULL) {
     ll ret = 0;
     D = new DisjointSet(n);
     sort(all(edges));
     for (auto e : edges) if (D->Union(e.y.x, e.y.y)) {
             ret += e.x;
             if (mst) {
                 mst->push_back(e);
             }
         }
     return ret;
 }
int main(int argc, char **argv) {
    if (argc != 2) {
        std::cout << "Usage: " << argv[0] << "||||| Should be: <MAXIMUM_NUMBER_OF_VERTICES>" << std::endl;
        return 0;
    }
    std::cout << "The maximus number of vertices is " << argv[1] << std::endl;
    
    
    // Try to translate MAXIMUM_NUMBER_OF_VERTICES
    // to size_t in order to determine the number of vertices.
    try {
        long num_of_nodes = std::stoi(argv[1]);
        if (num_of_nodes < 0) {
            std::cout << "Invalid argument for MAXIMUM_NUMBER_OF_NODES::Must be a positive integer!::TERMINATING PROGRAM\n";
            exit(1);
        }
        
        const size_t max_val = num_of_nodes;
        UndirectedGraph<size_t> testGraph;
        DisjointSet<size_t> testDS;
        
        size_t counter = 1;
        while (counter <= num_of_nodes) {
            testGraph.AddVertex(counter);
            testDS.AddNewNode(counter);
            ++counter;
        }

        srand(time(0)); //use current time as seed for random generator
        while (testDS.Size() > 1) {
            int i1 = rand() % max_val + 1;
            int i2 = rand() % max_val + 1;
            if (testGraph.AddEdge(i1, i2)) {
                testDS.Union(i1, i2);
            }
        }
        
//        testGraph.printGraph();
        testGraph.PrintGraphStats();
        
    } catch (std::invalid_argument) {
        std::cout << "Invalid argument for MAXIMUM_NUMBER_OF_NODES::Must be a positive integer::TERMINATING PROGRAM\n";
        exit(1);
    }
    
    
    return 0;
}
예제 #6
0
double Graph::MSTAlgo() {
  DisjointSet<Vertex>* A = new DisjointSet<Vertex>(n);
  double totalweight = 0;
  for(int i = 0; i < n; ++i){
	A->MakeSet(i, AdjacencyList[i]->getElem());
  }
  sortEdge();
  for(int i = 0; i < EdgeList.size(); ++i){
	if(A->FindSet(EdgeList[i]->vertex_i)->getKey() != A->FindSet(EdgeList[i]->vertex_j)->getKey()){
	  A->Union(*(A->FindSet(EdgeList[i]->vertex_i)), *(A->FindSet(EdgeList[i]->vertex_j)));
	  totalweight += EdgeList[i]->weight;
	  MST.push_back(EdgeList[i]);
	}
  }
  
  
  return totalweight;
}
예제 #7
0
int main(int argc, char *argv[]) {
  DisjointSet ds;
  for (int set_number=0; set_number<DSET_SIZE; ++set_number) {
    ds.MakeSet(set_number);
  }
  srand(time(NULL));
  int unions = 0;
  int attempts = 0;
  while (ds.size() > 1) {
    // generate random couples and do Union operations, up to when there is
    // only a single set left.
    int x = rand() % DSET_SIZE;
    int y = rand() % DSET_SIZE;
    if (ds.Union(x,y)) {
      unions += 1;
    } else {
      attempts += 1;
    }
    //std::cout << ds.toDot() << std::endl;
  }
  std::cerr << "unions=" << unions << ", attempts=" << attempts << std::endl;
  std::cout << ds.toDot() << std::endl;
  return 0;
}