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


C++ DisjointSet::merge方法代码示例

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


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

示例1: 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

示例2: main

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

示例3: main

int main()
{
  std::cerr << "Running Test for Disjoint Set..." << std::endl;

  DisjointSet<int> test;
  for (int i = 0; i < 10; ++i)
    test.add(i);

  for (int i = 0; i < 9; ++i) {
    for (int j = i + 1; j < 10; ++j) {
      ASSERT_CONDITION(test.isConnected(i, j) == false, "No connectivity check");
    }
  }

  ASSERT_CONDITION(test.getNumElements() == 10, "Number of elements check");
  ASSERT_CONDITION(test.getNumSets() == 10, "Number of sets check");

  test.merge(1, 2);
  test.merge(1, 5);
  test.merge(2, 6);
  test.merge(8, 5);
  test.merge(3, 4);
  test.merge(9, 7);

  ASSERT_CONDITION(test.getNumElements() == 10, "Number of elements (after merge) check");
  ASSERT_CONDITION(test.getNumSets() == 4, "Number of sets (after merge) check");

  ASSERT_CONDITION(test.isConnected(1, 2) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(1, 5) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(1, 6) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(1, 8) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(3, 4) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(7, 9) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(0, 1) == false, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(0, 3) == false, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(0, 7) == false, "Connectivity (after merge) check");

  return 0;
}
开发者ID:Harsha03,项目名称:coursera-courses,代码行数:39,代码来源:Test_DisjointSet.cpp

示例4: dfs

void dfs(int r, int c, int n, DisjointSet& ds) {
  stack<rc> sta;
  sta.push(rc(r, c));
  while (!sta.empty()) {
    int r = sta.top().r;
    int c = sta.top().c;
    sta.pop();
    if (V[r][c]) continue;
    V[r][c] = 1;

    for (int i = 0; i < 4; i++) {
      int nr = r+dr[i];
      int nc = c+dc[i];
      if (nr < 0 || nr >= N || nc < 0 || nc >= M || A[nr][nc] < n)
        continue;
      ds.merge(conv(r, c), conv(nr, nc));
      sta.push(rc(nr, nc));
    }
  }
}
开发者ID:wateret,项目名称:ps,代码行数:20,代码来源:659F.cpp


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