Alp?r javaslat?ra k?sz?tettem egy strukt?r?t 2 dimenzi?s vektorok (s?kvektorok) t?rol?s?ra. Olyasmiket tud, amiket s?kvektorokt?l szeretn?nk. A neve: xy, a hasonl? nevu .h file-ban tal?lhat?.
10 template <typename T, typename TIntMap>
14 typedef T ElementType;
15 typedef std::pair<int,int> PairType;
18 std::vector<PairType> data;
22 UnionFind(TIntMap& m) : map(m) {}
25 int whichComponent(T a)
27 int comp0 = map.get(a);
29 return insertNewElement(a);
33 while ( (next = data[comp].first) != comp) {
36 while ( (next = data[comp0].first) != comp) {
37 data[comp0].first = comp;
44 int insertNewElement(T a)
47 data.push_back(make_pair(n, 1));
52 bool joinComponents(T a, T b)
54 int ca = whichComponent(a);
55 int cb = whichComponent(b);
60 if ( data[ca].second > data[cb].second ) {
62 data[ca].second += data[cb].second;
66 data[cb].second += data[ca].second;