当前位置: 首页>>代码示例>>C++>>正文


C++ DisjointSet类代码示例

本文整理汇总了C++中DisjointSet的典型用法代码示例。如果您正苦于以下问题:C++ DisjointSet类的具体用法?C++ DisjointSet怎么用?C++ DisjointSet使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DisjointSet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main()
{
    Trie trie;
    DisjointSet djset;
    int degree[500001] = {0};
    string word1, word2;
    char w1[15], w2[15];
    int hash1, hash2;
    while (scanf("%s %s", w1, w2) != EOF) {
        hash1 = trie.addWord(w1);
        hash2 = trie.addWord(w2);
        degree[hash1]++;
        degree[hash2]++;
        djset.union_set(hash1, hash2);
    }
    int max_count = trie.max_count();
    int sum = 0;
    for (int i = 0; i < max_count; i++) {
        if (degree[i] & 1)
            sum++;
        if (sum > 2) {
            cout << "Impossible" << endl;
            return 0;
        }
    }

    for (int i = 0; i < max_count; i++) {
        if (djset.find_set(i) != djset.find_set(0)) {
            cout << "Impossible" << endl;
            return 0;
        }
    }
    cout << "Possible" << endl;
    return 0;
}
开发者ID:wangkendy,项目名称:grids,代码行数:35,代码来源:2513.cpp

示例2: main

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;
}
开发者ID:Corei13,项目名称:contest-programming-codes,代码行数:29,代码来源:11966.cpp

示例3: main

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;
}
开发者ID:Corei13,项目名称:contest-programming-codes,代码行数:28,代码来源:1197.cpp

示例4: main

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;
}
开发者ID:jiang42,项目名称:AlgoContest,代码行数:26,代码来源:hdu4463.cpp

示例5: main

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;
}
开发者ID:Corei13,项目名称:contest-programming-codes,代码行数:27,代码来源:11690.cpp

示例6: kruskal

inline
int kruskal(Edge E[], const int size)
{
  static DisjointSet ds;
  int cnt = 0, sum = 0;
  REP(i, size){
    ds.make(i);
  }
开发者ID:Johniel,项目名称:uva,代码行数:8,代码来源:11631.cpp

示例7: main

int main() {
	DisjointSet djset;
	DisjointSet::NodePtr id[3];
	id[0] = djset.makeSet(0);
	id[1] = djset.makeSet(1);
	id[2] = djset.makeSet(2);

	djset.linkSets(id[0], id[1]);
	djset.linkSets(id[1], id[2]);

	DisjointSet::SetIdentifier t1 = djset.findSet(id[0]);
	DisjointSet::SetIdentifier t2 = djset.findSet(id[2]);
}
开发者ID:OasisGallagher,项目名称:CLRS,代码行数:13,代码来源:21.2-5.cpp

示例8: main

int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		PrintHelp();
		return 1;
	}
	
	map<string, string> systems;
	DisjointSet links;
	
	ifstream in(argv[1]);
	string line;
	string current;
	while(getline(in, line))
	{
		// Skip blank lines.
		if(IsEmpty(line))
			continue;
		// If this line is not indented, it starts a new root object.
		if(line[0] > ' ')
			current.clear();
		
		if(Token(line, 0) == "system")
			current = Token(line, 1);
		else if(!current.empty() && Token(line, 0) == "link")
			links.Join(current, Token(line, 1));
		
		if(!current.empty())
		{
			systems[current] += line;
			systems[current] += '\n';
		}
	}
	
	vector<string> components;
	for(char **it = argv + 2; *it; ++it)
		components.push_back(*it);
	for(const pair<string, string> &it : systems)
	{
		bool match = components.empty();
		for(const string &component : components)
			match |= links.IsJoined(it.first, component);
		
		if(match)
			cout << it.second << endl;
	}
	return 0;
}
开发者ID:endless-sky,项目名称:endless-sky-tools,代码行数:49,代码来源:map-component.cpp

示例9: main

int main(int argc, char **argv)
{
	ifstream ifs;

	if (argc != 2)
	{
		cout << getlonglong("1 1 1 1") << endl;
		cout << getlonglong("1 0 1 0") << endl;
		return -1;
	}

	ifs.open(argv[1]);
	string str;
	ifs >> N >> M;
	getline(ifs, str);
	vl G = vl(N);
	DisjointSet ds = DisjointSet(N);

	for (int i = 0; i < N; ++i)
	{
		std::getline(ifs, str);
		G[i] = getlonglong(str);
	}

	sort(G.begin(), G.end());
	unionEquals(G, ds, CmpByMask(-1));

	/* find all nodes, which differ by single bit only */
	for (int i = 0; i < M; ++i)
	{
		long long mask = ~(1LL << i);
		sort(G.begin(), G.end(), CmpByMask(mask));
		unionEquals(G, ds, CmpByMask(mask));
	}
	/* find all nodes, which differ by two bits */
	for (int i = 0; i < M - 1; ++i)
	{
		for (int j = i + 1; j < M; ++j)
		{
			long long mask = ~((1LL << i) + (1LL << j));
			sort(G.begin(), G.end(), CmpByMask(mask));
			unionEquals(G, ds, CmpByMask(mask));
		}
	}
	/* output how many clusters do we have */
	cout << ds.getNumberOfComponent() << endl;

	return 0;
}
开发者ID:ykirpichev,项目名称:experiments,代码行数:49,代码来源:main.cpp

示例10: main

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;
}
开发者ID:stagadish,项目名称:random-connected-undirected-graph-generator,代码行数:48,代码来源:TestRandomGraph.cpp

示例11: doAFind

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;
    }
}
开发者ID:tricyclelad,项目名称:CS2420-Assignment-6,代码行数:7,代码来源:main.cpp

示例12: kruskal

int kruskal(int N, vector<Edge> edges)
{
	int totalCost = 0;
	sort(edges.begin(), edges.end());
	DisjointSet dset = DisjointSet(N + 1);
	int source;
	int target;
	for (int i = 0; i < edges.size(); i++) {
		Edge e = edges[i];
		if (!dset.same(e.source, e.target)) {
			//MST.push_back(e);
			totalCost += e.cost;
			dset.unite(e.source, e.target);
		}
	}
	return totalCost;
}
开发者ID:ubuntuh,项目名称:github,代码行数:17,代码来源:textbook.cpp

示例13: kruskal

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;
}
开发者ID:mtreviso,项目名称:university,代码行数:29,代码来源:1764-ItinerarioDoPapaiNoel.cpp

示例14: kruskal

int kruskal(int v)
{
    DisjointSet mst;
    int origen, destino, peso, total = 0;

    mst.initSet(v);
    sort(aristas.begin(), aristas.end());
    for(int i = 0; i < aristas.size(); i++){
        peso = aristas[i].first;
        origen = aristas[i].second.first;
        destino = aristas[i].second.second;
        if(!mst.isSameSet(origen, destino)){
            total += peso;
            mst.unionSet(origen, destino);
        }
    }
    return total;
}
开发者ID:RasecD,项目名称:UVa-Solutions,代码行数:18,代码来源:908+-+Re-connecting+Computer+Sites.cpp

示例15: main

int main(){
	int n;
	long q;
	scanf(" %d %ld", &n, &q);
	
	DisjointSet ds = DisjointSet(n);
	
	for(long i=0; i<q; ++i){
		int com, x, y;
		scanf(" %d %d %d", &com, &x, &y);
		
		if(com==0) ds.unite(x,y);	
		else if(com==1){
			if(ds.same(x,y)) printf("1\n");
			else printf("0\n");
		}
		
	}
}
开发者ID:imulan,项目名称:procon,代码行数:19,代码来源:1a.cpp


注:本文中的DisjointSet类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。