예제 #1
0
파일: union.c 프로젝트: gkirion/Algorithms
int main(void) {
	int i;
	for (i = 0; i < N; i++) {
		A[i].rep = i;
		A[i].value = i;
		A[i].index = i;
		A[i].size = 1;
	}
	printf("%d\n", find(A[499]).value);
	printf("%d\n", find(A[499]).size);
	unite(A[499], A[500]);
	printf("%d\n", find(A[499]).value);
	printf("%d\n", find(A[499]).size);
	unite(A[600], A[601]);
	unite(A[499], A[600]);
	unite(A[700], A[800]);
	unite(find(A[499]), find(A[800]));
	printf("%d\n", A[499].rep);
	printf("%d\n", find_and_compress(A[499]).value);
	printf("%d\n", find(A[499]).size);
	printf("%d\n", A[499].rep);
	printf("%d\n", A[700].rep);
	find_and_compress(A[700]);
	printf("%d\n", A[700].rep);
	return 0;
}
예제 #2
0
/*!
    Unites this box with \a box by expanding this box to encompass the
    region defined by \a box.  If \a box is already contained within
    this box, then this box will be unchanged.

    \sa united(), intersect()
*/
void QBox3D::unite(const QBox3D& box)
{
    if (box.boxtype == Finite) {
        unite(box.minimum());
        unite(box.maximum());
    } else if (box.boxtype == Infinite) {
        setToInfinite();
    }
}
예제 #3
0
void unite (pnode &t,pnode l, pnode r) {
	if (!l || !r) return void(t = l ? l : r);
	if (l->prior < r->prior) swap (l, r);
	pnode lt, rt;
	split (r,lt, rt,l->val);
	unite (l->l,l->l, lt);
	unite (l->r,l->r, rt);
	t=l;upd_sz(t);
}
예제 #4
0
파일: RCCamera.cpp 프로젝트: jorj1988/Shift
void computeViewBounds(RCViewableTransform *tr)
  {
  auto cb = tr->viewBounds.computeLock();

  cb->clear();
  cb->unite(tr->worldSpaceFromUnitSpace(Eks::Vector4D(-1.0f, -1.0f, -1.0f, 1.0f)));
  cb->unite(tr->worldSpaceFromUnitSpace(Eks::Vector4D( 1.0f, -1.0f, -1.0f, 1.0f)));
  cb->unite(tr->worldSpaceFromUnitSpace(Eks::Vector4D(-1.0f,  1.0f, -1.0f, 1.0f)));
  cb->unite(tr->worldSpaceFromUnitSpace(Eks::Vector4D( 1.0f,  1.0f, -1.0f, 1.0f)));
  cb->unite(tr->worldSpaceFromUnitSpace(Eks::Vector4D(-1.0f, -1.0f,  1.0f, 1.0f)));
  cb->unite(tr->worldSpaceFromUnitSpace(Eks::Vector4D( 1.0f, -1.0f,  1.0f, 1.0f)));
  cb->unite(tr->worldSpaceFromUnitSpace(Eks::Vector4D(-1.0f,  1.0f,  1.0f, 1.0f)));
  cb->unite(tr->worldSpaceFromUnitSpace(Eks::Vector4D( 1.0f,  1.0f,  1.0f, 1.0f)));
  }
 /**
  * @param nodes a array of directed graph node
  * @return a connected set of a directed graph
  */
 vector<vector<int>> connectedSet2(vector<DirectedGraphNode *>& nodes) {
     vector<vector<int>> res;
     if (nodes.empty())  return res;
     unordered_map<DirectedGraphNode *, DirectedGraphNode *> parent;
     for (int i = 0; i < nodes.size(); i++) {
         DirectedGraphNode *t = nodes[i];
         DirectedGraphNode *pt = find(parent, t);
         for (int j = 0; j < (t->neighbors).size(); j++) {
             DirectedGraphNode *nt = (t->neighbors)[j];
             unite(parent, t, nt);
         }
     }
     // post-processing to get the components
 	unordered_map<DirectedGraphNode *, vector<int>> components;
 	for (int i = 0; i < nodes.size(); i++) {
     	DirectedGraphNode *p = find(parent, nodes[i]);
     	components[p].push_back(nodes[i]->label);
 	}
 	for (auto it = components.begin(); it != components.end(); it++) {
     	vector<int> tmp = it->second;
     	sort(tmp.begin(), tmp.end());
     	res.push_back(tmp);
 	}
 	return res;
 }
예제 #6
0
파일: main.c 프로젝트: kenp-89/kp_huffman
struct kpnode* mktree(struct lnode *list)
{
	struct kpnode *root = 0;
	struct tlist *tlp = mktlist(0);
	struct tlist *tlroot = tlp;

	/* make leaves */
	for (; list != 0; list = list->next) {
		tlp->tree = mknode(list->ch);
		tlp->freq = list->freq;

		if (list->next != 0) {
			tlp->next = mktlist(0);
			tlp = tlp->next;
		}
	}

	tlroot = sorttlist(tlroot);

	disptlist(tlroot);

	/* connect leaves to form whole tree */
	tlroot = unite(tlroot);

	disptlist(tlroot);

	root = tlroot->tree;

	return root;
}
예제 #7
0
파일: pathview.cpp 프로젝트: tumic0/GPXSee
void PathView::addWaypoints(const QList<Waypoint> &waypoints)
{
	qreal scale = mapScale(_zoom);

	for (int i = 0; i < waypoints.count(); i++) {
		const Waypoint &w = waypoints.at(i);

		WaypointItem *wi = new WaypointItem(w);
		wi->setScale(1.0/scale);
		wi->setZValue(1);
		wi->showLabel(_showWaypointLabels);
		wi->setVisible(_showWaypoints);
		_scene->addItem(wi);

		if (_wr.isNull()) {
			if (_wp.isNull())
				_wp = wi->coordinates();
			else
				_wr = qrectf(_wp, wi->coordinates());
		} else
			unite(_wr, wi->coordinates());

		_waypoints.append(wi);
	}

	if (_poi)
		addPOI(_poi->points(waypoints));

	_zoom = scale2zoom(contentsScale());
}
예제 #8
0
void QTextDocumentPrivate::insert_string(int pos, uint strPos, uint length, int format, QTextUndoCommand::Operation op)
{
    // ##### optimise when only appending to the fragment!
    Q_ASSERT(noBlockInString(text.mid(strPos, length)));

    split(pos);
    uint x = fragments.insert_single(pos, length);
    QTextFragmentData *X = fragments.fragment(x);
    X->format = format;
    X->stringPosition = strPos;
    uint w = fragments.previous(x);
    if (w)
        unite(w);

    int b = blocks.findNode(pos);
    blocks.setSize(b, blocks.size(b)+length);

    Q_ASSERT(blocks.length() == fragments.length());

    QTextFrame *frame = qobject_cast<QTextFrame *>(objectForFormat(format));
    if (frame) {
        frame->d_func()->fragmentAdded(text.at(strPos), x);
        framesDirty = true;
    }

    adjustDocumentChangesAndCursors(pos, length, op);
}
void Grammar::length_reduction() {
	auto current = get_first(unite(non_terminals, terminals), 'W');
	SymbolList temp_nts = non_terminals;
	auto new_map = production_rules;
	new_map.clear();
	unsigned int c = 0;
	for( auto nT : temp_nts ) {
		std::vector<SymbolList> temp_vector;
		for( auto rule : production_rules[get_nt_index(nT)] ) {
			while( rule.size() > 2 ) {
				SymbolList temp_str;
				temp_str.push_back(*(rule.end() -2));
				temp_str.push_back(*( rule.end() - 1 ));
				rule.pop_back();
				rule.pop_back();
				rule.push_back(current);
				non_terminals.push_back(current);
				new_map[get_nt_index(current)].push_back(temp_str);
				//std::cout << current << "(id: " << get_nt_index(nT) << ") -> " << temp_str << '\n';
				get_next(current, ++c);
			}
			new_map[get_nt_index(nT)].push_back(rule);
		}
	}
	production_rules = new_map;
}
예제 #10
0
파일: union_find.cpp 프로젝트: DavieV/judge
 // Takes in the indexes of two items, and places all of the items
 // in the set that y belongs to into the set that x belongs to
 void unite(int x, int y) {
     if (parent[x] < 0 && parent[y] < 0) {
         parent[x] += parent[y];
         parent[y] = x;
     } else {
         unite(find(x), find(y));
     }
 }
예제 #11
0
inline pure
typename std::enable_if<is_container<C<vec<T,N> > >::value,
                        box<T,N>>::type
outer_box(const C<vec<T,N> >& a) {
    auto b = a.size() ? static_cast<box<T,N> >(*begin(a)) : box<T,N>::unite_unit();
    std::for_each(begin(a), end(a), [&b](const vec<T,N>& e) { b = unite(b, e); });
    return b;
}
예제 #12
0
vector<Edge> kruskal(int V, vector<Edge>& edges){
	auto union_find = UnionFind(V);
	sort(edges.begin(), edges.end());
	vector<Edge> res;
	for(auto& edge: edges){
		if(union_find.unite(edge.from, edge.to)){
			res.push_back(edge);
		}
	}
	return res;
}
예제 #13
0
 int findCircleNum(vector<vector<int>>& M) {
     int n = M.size();
     par.resize(n);
     for (int i = 0; i != par.size(); ++i)
         par[i] = i;
     result = n;
     for (int i = 0; i != n; ++i)
         for (int j = i + 1; j != n; ++j)
             if (M[i][j] == 1)
                 unite(i, j);
     return result;
 }
예제 #14
0
void solve () {
    init(N*3);
    int ans = 0;
    for (int i = 0; i < K; i++) {
        int t = T[i];
        int x = X[i] - 1, Y[i] - 1;
        
        // incorrect number
        if (x < 0 || N <= x || y < 0 || N <= y) {
            ans++;
            continue;
        }
        if (t == 1) {
            // x and y is same kind
            if (same(x, y+N) || same(x, y+2*N)) {
                ans++;
            } else {
                unite(x, y);
                unite(x+N, y+N);
                unite(x+N*2, y+N*2);
            }
        } else {
            // x eat y
            if (same(x, y) || same(x, y+2*N)) {
                ans++;
            } else {
                unite(x, y+N);
                unite(x+N, y+2*N);
                unite(x+2*N, y);
            }
        }
    }
    printf("%d\n",ans);
}
void Grammar::reduce() {
	non_terminals = intersection(get_reachables(), get_actives());
	SymbolList all_symbols = unite(non_terminals, terminals);
	auto temp_rules = production_rules;
	production_rules.clear();
	for( auto &nT : non_terminals ) {
		for( auto rule : temp_rules[get_nt_index(nT)] ) {
			if( subset_of(rule, all_symbols) ) {
				production_rules[get_nt_index(nT)].push_back(rule);
			}
		}
	}
}
예제 #16
0
int kruskal(){
  sort(es, es + E, comp);//sort 
  init_union_find(V);//initialization of disjoint set
  int res = 0;
  for(int i = 0; i < E; i++){
    edge e = es[i];
    if(!same(e.u, e.v)){
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}
예제 #17
0
파일: 7512732_TLE.c 프로젝트: kaleo211/poj
int main() {
	int		n, i, j, q, min, road, y, x;
	int		distence[101][101];
	while(scanf("%d", &n) !=EOF) {
		for(i=0; i<=100; i++)
			parent[i] = -1;

		for(i=1; i<=n; i++)
			for(j=1; j<=n; j++)
				scanf("%d", &distence[i][j]);
		scanf("%d", &q);
		while(q--) {
			scanf("%d %d", &i, &j);
			distence[i][j] = 1001;
			unite(find(i), find(j));
		}
		
		road = 0;
		while(1) {
			min = 1001;
			for(i=1; i<n; i++)
				for(j=i+1; j<=n; j++) {
					if( parent[i]!=parent[j])
						if(min > distence[i][j]) {
							min = distence[i][j];
							x = i;
							y = j;
						}
				}
			if(min==1001) break;
			road += min;
			distence[x][y] = 1001;
			unite(find(x), find(y));
		}
		printf("%d\n", road);
	}
	return 1;
}
예제 #18
0
int kruskal()
{
    sort(es, es+E, comp);
    init(V); // init union_find
    int res = 0;
    for (int i = 0; i < E; i++) {
	edge e = es[i];
	if (!same(e.u, e.v)) {
	    unite(e.u, e.v);
	    res += e.cost;
	}
    }
    return res;
}
예제 #19
0
void unionTransformedBounds(RCTransform* tr)
  {
  auto lock = tr->bounds.computeLock();
  lock = Eks::BoundingBox();

  xForeach(auto r, tr->renderGroup.walker<RCRenderablePointer>())
    {
    const RCRenderable* ptd = r->pointed();

    lock->unite(ptd->bounds());
    }

  lock = tr->transform() * lock;
  }
예제 #20
0
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < 20; ++ i) {
        for (int j = 1; j <= n - (1 << i) + 1; ++ j) {
            father[i][j] = j;
            size[i][j] = 1;
        }
    }
    for (int i = 1; i <= m; ++ i) {
        scanf("%d %d %d", &len, &x, &y);
        int k = 0;
        while ((1 << k) <= len) {
            k ++;
        }
        k --;
        unite(k, x, y);
        unite(k, x + len - (1 << k), y + len - (1 << k));
    }
    for (int k = 19; k >= 1; -- k) {
        for (int i = 1; i <= n - (1 << k) + 1; ++ i) {
            int r = root(k, i);
            unite(k - 1, i, r);
            unite(k - 1, i + (1 << (k - 1)), r + (1 << (k - 1)));
        }
    }
    int cnt = 0;
    for (int i = 1; i <= n; ++ i) {
        if (root(0, i) == i) {
            cnt ++;
        }
    }
    int ans = 1;
    for (int i = 1; i <= cnt; ++ i) {
        ans = (1LL * ans * 26) % mod;
    }
    printf("%d\n", ans);
}
void Grammar::pseudo_non_terminals() {
	auto current = get_first(unite(non_terminals, terminals), 'Q');
	SymbolList temp_non_terms = non_terminals;
	SymbolList pseudo_non_terms;
	unsigned int c = 0;
	for( auto T : terminals ) {
		SymbolList temp;
		temp.push_back(T);
		non_terminals.push_back(current);
		pseudo_non_terms.push_back(current);
		production_rules[get_nt_index(current)].push_back(temp);
		//std::cout << current << " " << production_rules[get_nt_index(current)].back() << " " << production_rules[get_nt_index(current)].size() << '\n';
		get_next(current, ++c);
	}

	for( auto nT : temp_non_terms ) {
		std::vector<SymbolList> temp_rules;
		for( auto rule : production_rules[get_nt_index(nT)] ) {
			//std::cout << "rule: " << rule << " => ";
			if( rule.size() == 1 ) {
				//std::cout << "unchanged\n";
				temp_rules.push_back(rule);
				continue;
			}
			for( auto ch : rule ) {
				//std::cout << "ch: " << ch << '\n';
				if( contains_char(terminals, ch) ) {
					unsigned int i = pseudo_non_terms.size();
					while( i --> 0 ) {
						//std::cout << "while( "<<i<<" --> 0)\n";
						auto x = production_rules[get_nt_index(pseudo_non_terms[i])];
						//std::cout << production_rules[get_nt_index(pseudo_non_terms[i])][0];
						//std::cout << x[0][0] << " =?= " << ch << '\n';
						if( ch == x[0][0] ) {
							//std::cout << " :: TRUE\n";
							break;
						}
					}
					auto t = pseudo_non_terms[i];
					replace_first_of(rule, ch, t);
				}
			}
			//std::cout << rule << '\n';
			temp_rules.push_back(rule);
		}
		production_rules[get_nt_index(nT)] = temp_rules;
	}

}
예제 #22
0
파일: 7512342_TLE.c 프로젝트: kaleo211/poj
int main() {
	int		n, i, j, q, min, road, y, x, count;
	int		distence[101][101];
	while(scanf("%d", &n) !=EOF) {
		for(i=0; i<=100; i++)
			parent[i] = -1;

		for(i=1; i<=n; i++)
			for(j=1; j<=n; j++)
				scanf("%d", &distence[i][j]);
		scanf("%d", &q);
		count = q;
		while(q--) {
			scanf("%d %d", &i, &j);
			unite(find(i), find(j));
		}
		
		road = 0;
		while(count<n-1) {
			min = 1001;
			for(i=2; i<=n; i++)
				for(j=1; j<i; j++) {
					if(find(i)!=find(j) && min > distence[i][j]) {
						min = distence[i][j];
						x = i;
						y = j;
					}
				}
			count++;
			road += min;
			unite(find(x), find(y));
		}
		printf("%d\n", road);
	}
	return 1;
}
예제 #23
0
void unionBounds(RCRenderArray* array)
  {
  auto lock = array->bounds.computeLock();
  lock = Eks::BoundingBox();

  xForeach(auto r, array->renderGroup.walker<RCRenderablePointer>())
    {
    const RCRenderable* ptd = r->pointed();

    if(ptd)
      {
      lock->unite(ptd->bounds());
      }
    }
  }
예제 #24
0
파일: main.c 프로젝트: kenp-89/kp_huffman
struct tlist* unite(struct tlist *list)
{
	struct tlist *tmp = 0;

	if (list == 0)
		return list;

	while (list->next != 0) {
		tmp = list;
		list->tree = insnode(0, list->tree, list->next->tree);
		list->next = unite(list->next->next);
		
	}

	return list;
}
예제 #25
0
int main() {
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; i++) p[i] = i;
	for (int i = 0; i < m * 2; i++)
		scanf("%d", x);
	for (int i = 0; i < k; i++)
		scanf("%s%d%d", s + i, x + i, y + i);
	m = 0;
	for (int i = k - 1; i >= 0; i--)
		if (s[i][0] == 'a')
			ans[m++] = get(x[i]) == get(y[i]);
		else unite(get(x[i]), get(y[i]));
	for (int i = m - 1; i >= 0; i--)
		printf("%s\n", ans[i] ? "YES" : "NO");
	return 0;
}
예제 #26
0
    bool equationsPossible(vector<string>& equations) {
        int n = equations.size();
        unordered_map<int, int> m;
        for(int i = 'a'; i <= 'z'; ++i) m[i] = i;

        function<int(int)> root = [&](int i)
        {
            while (i != m[i])
            {
                m[i] = m[m[i]];
                i = m[i];
            }
            return i;
        };

        function<bool(int, int)> find = [&](int p, int q)
        {
            return root(p) == root(q);
        };

        function<void(int, int)> unite = [&](int p, int q)
        {
            int i = root(p);
            int j = root(q);
            m[i] = j;
        };

        for(int i = 0; i < n; ++i)
        {
            if(equations[i][1] == '=')
            {
                unite(equations[i][0], equations[i][3]);
            }
        }

        for(int i = 0; i < n; ++i)
        {
            if(equations[i][1] == '!')
            {
                int k = root(int(equations[i][0]));
                int j = root(int(equations[i][3]));
                if(k == j) return false;
            }
        }
        return true;
    }
예제 #27
0
int main()  
{  
    #ifdef LOCAL  
    freopen("input.txt","r",stdin);  
    #endif  
    int i,a,b,n=0;  
    while(EOF !=scanf("%d %d",&N,&M) && 0!=N && 0!=M)  
    {  
        init(N);  
        int count=0;  
        for(i=0;i<M;i++)  
        {  
            scanf("%d %d",&a,&b);  
            if(!same(a,b)) { unite(a,b),count++; };  
        }  
        printf("Case %d: %d\n",++n,N-count);  
    }  
    return 0;  
}  
예제 #28
0
int main(){  
#ifdef LOCAL  
    freopen("in.txt", "r", stdin);  
    freopen("out.txt", "w+", stdout);  
#endif  
    int n, m, t, l, p, k;  
    while (~scanf("%d %d", &n, &m)){  
        if (0 == n && 0 == m) break;  
        init(n);  
        while (m--){  
            scanf("%d %d", &t, &k);  
            l = t;  
            for (l = 1; l < t; l++){  
                scanf("%d", &p);  
                unite(k, p);  
            }  
        }  
        printf("%d\n", rank[par[0]]);  
    }  
    return 0;  
}  
void MST_kruskal(int G[][20],int no_of_nodes,int mst[][20])
{
  int i,j;
  int u,v;

  for(i=0;i<no_of_nodes;i++)
    for(j=0;j<no_of_nodes;j++)
      mst[i][j]=0;

  for(i=0;i<no_of_nodes;i++)make_set(i);
  get_sorted_edge_list(G,no_of_nodes);
  for(i=0;i<edge_list_size;i++)
    {
      u=edge_list[i][0];
      v=edge_list[i][1];
      if(find_set(u)!=find_set(v))
	{
	  mst[u][v]=mst[v][u]=edge_list[i][2];
	  unite(u,v);
	}
    }
}
예제 #30
0
int main() {
    freopen(file ".in", "r", stdin);
    freopen(file ".out", "w", stdout);

    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; i++)
        p[i] = i;
    for (int i = 0; i < m; i++)
        scanf("%d%d", x, y);
    for (int i = 0; i < k; i++) {
        scanf("%s%d%d", s, x + i, y + i);
        cmd[i] = s[0] == 'a';
    }
    for (int i = k - 1; i >= 0; i--) {
        if (cmd[i])
            ans[i] = find(x[i]) == find(y[i]);
        else unite(x[i], y[i]);
    }
    for (int i = 0; i < k; i++)
        if (cmd[i])
            printf("%s\n", ans[i] ? "YES" : "NO");
    return 0;
}