Exemple #1
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;
}
Exemple #2
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;
}
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;
}
Exemple #4
0
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));
    }
  }
}