long int kruskal(int m, vector<triple> &arestas){

	DisjointSet<int> *ds = new DisjointSet<int>(m);
	// vector<ii> result;
	long int t = 0;

	for(int i=0; i<m; i++)
		ds->make(i, 0);

	sort(arestas.begin(), arestas.end());

	int e = 0, i = 0;
	while(e < m-1){

		int u = ds->find(arestas[i].first);
		int v = ds->find(arestas[i].second);

		if(ds->find(u) != ds->find(v)){
			t += arestas[i].third;
			// result.push_back(ii(u, v));
			ds->join(u, v);
			e++;
		}

		i++;
	}

	return t;
}
Пример #2
0
int main(void) {
  std::cin.tie(0);
  int n, p, q;
  while(cin >> n && n) {
    cin >> p >> q;
    for(int i = 0; i < n; ++i)
      cin >> pts[i].first >> pts[i].second;
    vector<edge> es;
    for(int i = 0; i < n; ++i)
      for(int j = i+1; j < n; ++j)
        es.push_back(edge(i, j, dist(i, j)));
    sort(es.begin(), es.end());
    DisjointSet ds = DisjointSet(n);
    double ans = 0;
    ds.merge(p-1, q-1);
    ans += dist(p-1, q-1);
    for(size_t i = 0; i < es.size(); ++i) {
      if(ds.find(es[i].u) != ds.find(es[i].v)) {
        ds.merge(es[i].u, es[i].v);
        ans += es[i].c;
      }
    }
    printf("%.2lf\n", ans);
  }
  return 0;
}
Пример #3
0
void doAFind(DisjointSet set)
{
    cout << "Do a find on each number" << endl;
    for (int i = 0; i < 10; i++) {
        cout << "Find on "<< i << " = " << set.find(i) << endl;
    }
}
Пример #4
0
int main(void){
    int T;
    scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++) {
        memset(v, 0, sizeof(v));
        int n, m;
        scanf("%d %d", &n, &m);

        DisjointSet d = DisjointSet(n + 1);
        bool flag = true;

        int i, j, pi = 0, pj = 0;
        while(m--) {
            scanf("%d %d", &i, &j);
            if(!flag) continue;
            v[i] = d.find(i);
            v[j] = d.find(j);

            if(d.find(i) == d.find(j))
                flag = false;

            if(d.find(pj) == v[i] || d.find(pi) == v[j]) {
                d.merge(i, pj);
                d.merge(j, pi);
            } else {
                d.merge(i, pi);
                d.merge(j, pj);
            }

            pi = i;
            pj = j;
        }

        printf("Scenario #%d:\n", kase);
        if(flag) printf("No suspicious bugs found!\n\n");
        else printf("Suspicious bugs found!\n\n");
    }
    return 0;
}
Пример #5
0
int main() {
	DisjointSet DS;
	DS.makeSet(0);
	DS.makeSet(1);
	DS.makeSet(2);
	DS.makeSet(3);
	DS.makeSet(4);
	DS.union_sets(0, 2);
	//cout << DS.find(2) << endl;
	DS.union_sets(3, 4);
	DS.union_sets(0, 1);
	DS.union_sets(1, 2);
	DS.union_sets(1, 3);
	cout << DS.size() << endl;
	cout << DS.find(0) << endl;
	cout << DS.find(1) << endl;
	cout << DS.find(2) << endl;
	cout << DS.find(3) << endl;
	cout << DS.find(4) << endl;
	//for(int i = 0; i < DS.size(); i++)
	//	cout << i << "'s root: " << DS.find(i) << endl;
	//DS.print();
}
void GraphController::kruskalRequest()
{
	viewer->unselectEdges();
	DisjointSet set;
	auto result = GraphAlgorithms::getKruskalMST(*graph, &set);

	if (result.empty())
	{
		viewer->showResult("No MST found.");
		return;
	}
	std::unordered_map<int, int> sums;
	std::vector<int> edgesIds;
	for (auto& edge: result)
	{
		int id = set.find(edge.getVertex1());
		auto it = sums.find(id);
		if (it != sums.end())
			it->second += edge.getWeight();
		else
		{
			sums[id] = edge.getWeight();
		}
		edgesIds.push_back(edge.getId());
	}
	std::vector<int> nodes;
	std::string st;
	for (auto& it:sums)
	{
		st += "(" + std::to_string(it.first) +") = " + std::to_string(it.second) + ", ";
		nodes.push_back(it.first);
	}
	st.erase(st.end() - 2, st.end());
	viewer->selectEdges(edgesIds, nodes);
	std::string s = "";
	if (nodes.size() > 1)
	{
		s = "s";
	}
	viewer->showResult((std::to_string(sums.size()) +" MST found with total weight"+s+":  " + st).c_str());
}