Skip to content

Commit

Permalink
equivalence class
Browse files Browse the repository at this point in the history
  • Loading branch information
Takanori MAEHARA committed Jun 15, 2016
1 parent d856cc1 commit fdc02eb
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions __WIP/equivalence.cc
@@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
#define TEST(s) if (!(s)) { cout << __LINE__ << " " << #s << endl; exit(-1); }

// specify operator ==
template <class T, class F>
void equivalence(vector<T> x, F eq) {
vector<int> parent(x.size());
for (int i = ; i < x.size(); ++i) {
parent[i] = i;
for (int j = 0; j < i; ++j) {
parent[j] = parent[parent[j]];
if (eq(x[i], x[j])) parent[parent[parent[j]]] = i;
}
}
for (int i = 0; i < x.size(); ++i) parent[i] = parent[parent[i]];

for (int i = 0; i < parent.size(); ++i)
printf("%d %d \n", x[i], parent[i]);
}

int main() {
vector<int> x = {3,1,4,1,5,9,2,6,5,3,5,8,9};
equivalence(x, [&](int a, int b) { return a == b; });
}

0 comments on commit fdc02eb

Please sign in to comment.