Ejemplo n.º 1
0
void move(int x,int y)
{
	findset(x);findset(y);
	d[pa[x]]=cnt[pa[y]];
	cnt[pa[y]]+=cnt[pa[x]];
	pa[pa[x]]=pa[y];
	//printf("%d:%d %d:%d\n",x,d[x],y,d[y]);
}
	bool Triangulator::RemoveAnyEdge(_Internal::_edge* e)
	{
		_Internal::_edge* bedge = NULL;
		_Internal::_edge* hedge = NULL;
		if(e->type == _Internal::Inside) e->type = _Internal::Hole; //after removal, a hole will be created.
		for(int k=0; k<2; ++k)
		{
			if(e->faces[k])
			{
				for(int i=0; i<3; ++i)
				{
					if(bedge == NULL && e->faces[k]->edges[i]->type == _Internal::Boundary)
					{
						bedge = e->faces[k]->edges[i];
					}
					if(hedge == NULL && e->faces[k]->edges[i]->type == _Internal::Hole)
					{
						hedge = e->faces[k]->edges[i];
					}
				}
			}
		}
		_Internal::_edge* dominant = e;
		if(bedge) 
		{
			dominant = bedge;
		}
		else if(hedge)
		{
			dominant = hedge;
		}
		for(int k=0; k<2; ++k)
		{
			_Internal::_triangle* tr = e->faces[k]; //a triangle to be removed
			if(tr == NULL) continue;

			//remove this triangle from three edges forming the triangle.
			for(int i=0; i<3; ++i)
			{
				tr->edges[i]->type = dominant->type;
				merge(dominant->node, tr->edges[i]->node);
				tr->edges[i]->RemoveFace(tr);
			}
			vector<_Internal::_triangle*>::iterator itf = find(faces.begin(), faces.end(), tr);
			assert(itf != faces.end());
			faces.erase(itf);
			deleted_faces.push_back(tr);
		}
		for(int i=0; i<edges.size(); ++i)
		{
			if(findset(dominant->node)==findset(edges[i]->node))
			{
				edges[i]->type = dominant->type;
			}
		}
		_DeleteEdge(e);
		return true;
	}
Ejemplo n.º 3
0
void unionset(int u, int v)
{
	u = findset(u);
	v = findset(v);
	if (u != v)
	{
		p[u] = v;
		cnt[v] += cnt[u];
	}
}
Ejemplo n.º 4
0
int findset(int x)
{
	if(x==parent[x])
		return x;
	return parent[x]=findset(parent[x]);
	
}
Ejemplo n.º 5
0
int union_set(int a, int b)
{
		int parent_a = findset(a);
		int parent_b = findset(b);
		if(parent_a == parent_b) return -1; 
		if(size[parent_a] > size[parent_b])
		{
				parent[parent_b] = parent_a;
				size[parent_a] += size[parent_b];
		}
		else
		{
				parent[parent_a] = parent_b;
				size[parent_b] += size[parent_a];
		}
		return 1;
}
Ejemplo n.º 6
0
void setunion(int x,int y)
{
	int px,py;
	px=findset(x);
	py=findset(y);
	
	if(px==py)
		return;
	
	if(rank[px]>rank[py])
		parent[py]=px;
	else
		parent[px]=py;
		
	if(rank[px]==rank[py])
		rank[py]++;
}
Ejemplo n.º 7
0
bool sol(){
    bool found = false;
    for(int i=0; i<m; i++){
        int a, b, ta, tb;
        scanf("%d%d", &a, &b);
        if(found) continue;
        ta = findset(a);
        tb = findset(b);
        if(ta == tb)
            found = s[a] == s[b];
        else{
            p[ta] = tb;
            s[ta] = s[a]==s[b];
        }
    }
    return found;
}
Ejemplo n.º 8
0
void MST::Kruskals(Map& map)
{//http://zobayer.blogspot.nl/2010/01/kruskals-algorithm-in-c.html
	std::vector<Room*> rooms = map.getRooms();
	if (rooms.size() == 0) return;
	map.resetRooms();

	int w, u, v;
	Nodes = rooms.size();
	this->parent.reserve(Nodes);


	for (auto& r : rooms) {
		u = r->getID();
		parent.push_back(u);
		for (const auto& p : r->getAllPossiblePassages())
		{
			Room* next = p.second->GetRoom(p.first);
			w = next->getWeight() + 1;
			v = next->getID();
			Edges++;
			if (next->isReached() == false) {
				edge e{ u, v };
				EDGES.push_back(e);
				GRAPH.push_back(std::pair< int, edge >(w, e));
				next->setReached(true);
			}
		}
	}


	int i, pu, pv;
	sort(GRAPH.begin(), GRAPH.end()); // increasing weight
	for (i = total = 0; i<Nodes; i++)
	{
		pu = findset(GRAPH[i].second.first, parent);
		pv = findset(GRAPH[i].second.second, parent);
		if (pu != pv)
		{
			_MST.push_back(GRAPH[i]); // add to tree
			total += GRAPH[i].first; // add edge cost
			parent[pu] = parent[pv]; // link
		}
	}
	map.resetRooms();
}
Ejemplo n.º 9
0
int findset(int x)
{
	if(pa[x]==x)
		return x;
	int root=findset(pa[x]);
	d[x]^=d[pa[x]];
	pa[x]=root;
	return pa[x];
}
Ejemplo n.º 10
0
void update(int x, int y)
{
	for (int j = 0; j < 8; ++j)
	{
		int nx = x + dx[j];
		int ny = y + dy[j];
		int id = idx(x, y);
		int nid = idx(nx, ny);
		if (nid == -1)
			continue;
		if (ny >= 0 && ny < m && s[nx][ny] == '0')
			continue;
		int u = findset(id);
		int v = findset(nid);
		p[u] = v;
	}
	s[x][y] = '1';
}
Ejemplo n.º 11
0
int findset(int x)
{
	if(pa[x]==x)return pa[x];
	else
	{
		int root=findset(pa[x]);
		d[x]+=d[pa[x]];
		//printf("%d %d %d %d\n",x,root,d[x],d[root]);
		return pa[x]=root;
	}
}
Ejemplo n.º 12
0
Archivo: C.c Proyecto: mayukuner/HITCS
int main(){
#ifdef QWERTIER
  freopen("in.txt","r",stdin);
#endif 
  int kase = 1, i, n, m;
  while(scanf("%d%d",&n,&m) && n){
    for(i = 1; i <= n; i++)
      pa[i] = i;
    int ans = n;
    while(m--){
      int u, v;
      scanf("%d%d",&u,&v);
      if(findset(u) != findset(v)){
        pa[pa[u]] = pa[v];
        ans--;
      }
    }
    printf("Case %d: %d\n", kase++, ans);
  }
  return 0;
}
Ejemplo n.º 13
0
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d %d", &n, &m);
		int ans = -1;
		L = n * m;
		R = L + 1;
		for (int i = 0; i < n * m + 2; ++i)
			p[i] = i;
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j)
				s[i][j] = '0';
		for (int i = 0; i < n; ++i)
		{
			scanf("%s", s[i]);
			for (int j = 0; j < m; ++j)
				if (s[i][j] == '1')
					update(i, j);
		}
		if (findset(L) == findset(R))
			ans = 0;
		int Q;
		scanf("%d", &Q);
		for (int i = 0; i < Q; ++i)
		{
			int x, y;
			scanf("%d %d", &x, &y);
			update(x, y);
			if (ans == -1 && findset(L) == findset(R))
				ans = i + 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}
vector<int>
labelIntraFramePoints(vector<MotionPoint*>& points)
{
	vector<Node<int>*> vnodes;
	for (int i = 0; i < points.size(); ++i)
	{
		vnodes.push_back(makeset(i));
	}
	for (int i = 0; i < points.size(); ++i)
	{
		for (int j = i + 1; j < points.size(); ++j)
		{
			//if (i == j) continue;
			for (int m = 0; m < points[i]->candidates.size(); ++m)
			{
				for (int n = 0; n<points[j]->candidates.size(); ++n)
				{
					//if (points[i].GetLink(j, m, n) > .5) {
					if (points[i]->getLink(m, j, n) > 0.5) //probLink[j][m][n] > .5)
					{
						merge(vnodes[i], vnodes[j]);
					}
				}
			}
		}
	}
	vector<Node<int>*> labels = clusters(vnodes);
	vector<int> ilabels(points.size());
	for (int i = 0; i < points.size(); ++i)
	{
		ilabels[i] = distance(labels.begin(), find(labels.begin(), labels.end(), findset(vnodes[i])));
	}
	for (int i = 0; i < points.size(); ++i)
	{
		delete vnodes[i];
	}
	return ilabels;
}
Ejemplo n.º 15
0
Archivo: C.c Proyecto: mayukuner/HITCS
int findset(int u){
  if(pa[u]!=u)
    pa[u] = findset(pa[u]);
  return pa[u];
}
Ejemplo n.º 16
0
void prims::algorithm()
{
t=0;
t1=1;
T1[1]=1; //The source node
t2=n-1;
int i;
for(i=1;i<=n-1;i++)
T2[i]=i+1; //The reamining nodes
cout<<"\n*****The algorithm starts*****\n\n";
while(g!=0 && t!=n-1)
{
// Find the least cost edge
int min=9999;
int p;
int u,v,w;
for(i=1;i<=g;i++)
{
bool flag1=false,flag2=false;
//if u and v are in different sets
if(findset(graph_edge[i][1])!=findset(graph_edge[i][2]))
{
if(min>graph_edge[i][3])
{
min=graph_edge[i][3];
u=graph_edge[i][1];
v=graph_edge[i][2];
w=graph_edge[i][3];
p=i;
}
}
}
//break if there is no such edge
cout<<"The edge included in the tree is ::";
cout<<" < "<<u<<" , "<<v<<" > "<<endl;
//delete the edge from graph edges
for(int l=p;l<g;l++)
{
graph_edge[l][1]=graph_edge[l+1][1];
graph_edge[l][2]=graph_edge[l+1][2];
graph_edge[l][3]=graph_edge[l+1][3];
}
g--;
//add the edge to the tree
t++;
tree_edge[t][1]=u;
tree_edge[t][2]=v;
tree_edge[t][3]=w;
//Alter the set partitions
t1++;
int m;
if(findset(v)==2)
{
T1[t1]=v;
m=v;
}
else if(findset(u)==2)
{
T1[t1]=u;
m=u;
}
int x;
for(x=1;T2[x]!=m;x++);
for(;x<t2;x++)
T2[x]=T2[x+1];
t2--;
// Print the sets
int k;
cout<<"NOW\nT1 :: ";
for(k=1;k<=t1;k++)
cout<<T1[k]<<' ';
cout<<endl;
cout<<"T2 :: ";
for(k=1;k<=t2;k++)
cout<<T2[k]<<' ';
cout<<endl;
cout<<"The graph edges are ::\n";
for(i=1;i<=g;i++)
cout<<" < "<<graph_edge[i][1]
<<" , "<<graph_edge[i][2]
<<" > ::"<<graph_edge[i][3]<<endl;
cout<<endl<<endl;
}
}
Ejemplo n.º 17
0
int findset(int i)
{
		if(parent[parent[i]] != parent[i])
				parent[i] = findset(parent[i]);
		return parent[i];
}
Ejemplo n.º 18
0
int MST::findset(int x, std::vector<int> parent)
{
	if (x != parent[x])
		parent[x] = findset(parent[x], parent);
	return parent[x];
}
Ejemplo n.º 19
0
int findset(int x)
{
	return x == p[x] ? x : p[x] = findset(p[x]);
}
Ejemplo n.º 20
0
void uni(int a, int b){
    a = findset(a);
    b = findset(b);
    if(a != b) link(a, b);
}
Ejemplo n.º 21
0
int main()
{
	scanf("%d %d", &N, &K);
	for (int i = 0; i < N; ++i)
		scanf("%s", s[i]);
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			sum[i + 1][j + 1] = sum[i][j] - sum[i + 1][j] - sum[i][j + 1] + (s[i][j] == '.');
	for (int i = 0; i < N * N; ++i)
		p[i] = i, cnt[i] = 1;
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			if (s[i][j] == '.')
			{
				for (int k = 0; k < 4; ++k)
				{
					int ni = i + dx[k];
					int nj = j + dy[k];
					if (ni >= 0 && ni < N && nj >= 0 && nj < N)
						unionset(idx(i, j), idx(ni, nj));
				}
			}
	int tim = 1;
	int mx = 0;
	for (int i = 0; i < N - K + 1; ++i)
		for (int j = 0; j < N - K + 1; ++j)
		{
			int ans = 0;
			if (i > 0)
			{
				for (int k = 0; k < K; ++k)
				{
					int id = vis[findset(idx(i - 1, j + k))];
					if (id != tim)
					{
						id = tim;
						ans += cnt[findset(id)];
					}
				}
			}
			if (j > 0)
			{
				for (int k = 0; k < K; ++k)
				{
					int id = vis[findset(idx(i + k, j - 1))];
					if (id != tim)
					{
						id = tim;
						ans += cnt[findset(id)];
					}
				}
			}
			if (i + K < N)
			{
				for (int k = 0; k < K; ++k)
				{
					int id = vis[findset(idx(i + K, j + k))];
					if (id != tim)
					{
						id = tim;
						ans += cnt[findset(id)];
					}
				}
			}
			if (j + K < N)
			{
				for (int k = 0; k < K; ++k)
				{
					int id = vis[findset(idx(i + k, j + K))];
					if (id != tim)
					{
						id = tim;
						ans += cnt[findset(id)];
					}
				}
			}
			++tim;
		}
	return 0;
}
Ejemplo n.º 22
0
int main()
{
	int n,q,a,b,kase=1;
	char order[200];
	while(scanf("%d%d",&n,&q)&&n)
	{
		printf("Case %d:\n",kase++);
		memset(d,0,sizeof(d));
		memset(def,0,sizeof(def));
		for(int i=0; i<n; i++)
			pa[i]=i;
		bool ok=true;
		int fact_cnt=0;
		for(int i=1; i<=q; i++)
		{
			if(!ok){gets(order);continue;}
			scanf("%s",order);
			if(order[0]!='I'&&order[0]!='Q')
				scanf("%s",order);
			if(order[0]=='I')
			{
				int v;
				fact_cnt++;
				gets(order);
				if(sscanf(order,"%d %d %d",&a,&b,&v)==2)
				{
					//printf("%d %d %d\n",a,b,q);
					findset(a);
					if(!def[pa[a]])
						num[pa[a]]=b^d[a],def[pa[a]]=1;
					else if(num[pa[a]]!=b^d[a])
					{
						printf("The first %d facts are conflicting.\n",fact_cnt);
						ok=false;
						continue;
					}
				}
				else
				{
					//printf("%d %d %d\n",a,b,q);
					findset(a);findset(b);
					if(pa[a]==pa[b])
					{
						if(d[a]^d[b]!=v)
						{
							printf("The first %d facts are conflicting.\n",fact_cnt);
							ok=false;
							continue;
						}
					}
					else
					{
						if(def[pa[a]]&&def[pa[b]]&&num[pa[a]]^num[pa[b]]!=v)
						{
							printf("The first %d facts are conflicting.\n",fact_cnt);
							ok=false;
							continue;
						}
						int t=pa[a];pa[t]=a;d[t]=d[a];
						pa[a]=b;
						d[a]=v;
						findset(t);
						if(!def[pa[t]]&&def[t])
						{
							num[pa[t]]=num[t]^d[t];
							def[pa[t]]=1;
						}
					}
				}
			}
			else
			{
				int v[100],k,cnt=0,t=0;
				scanf("%d",&k);
				for(int j=1; j<=k; j++)
				{
					scanf("%d",&v[j]);
					if(v[j]>=n)
						cnt=-1;
				}
				int root_num=-1;
				for(int j=1; j<=k&&cnt!=-1; j++)
				{
					findset(v[j]);
					if(def[pa[v[j]]])
						v[j]=d[v[j]]^num[pa[v[j]]];
					else
					{
						if(pa[v[j]]==root_num||root_num==-1)
						{
							root_num=pa[v[j]];
							v[j]=d[v[j]];
							cnt++;
						}
						else
							cnt=-1;
					}
				}
				if(cnt%2)printf("I don't know.\n");
				else {for(int j=1;j<=k; j++)t^=v[j]; printf("%d\n",t);}
			}
		}
		printf("\n");
	}
	return 0;
}
Ejemplo n.º 23
0
void AGOSEngine_PN::opn_opcode50() {
	_fnst = 0;
	setScriptReturn(findset());
}
Ejemplo n.º 24
0
/* ARGSUSED */
int
set_io(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	GElf_Sym	setiosym;
	uint64_t	i;
	size_t		offset = 0;
	uint64_t	opt_a = 0;		/* up to active_sets */
	char		*opt_s = (char *)NULL;	/* a named set */
	uint64_t	opt_m = 0;		/* array element */
	int		setno = 0;
	int		argnum = 0;

	argnum = mdb_getopts(argc, argv,
		'a', MDB_OPT_UINT64, &opt_a,
		's', MDB_OPT_STR, &opt_s,
		'm', MDB_OPT_UINT64, &opt_m, NULL);

	if (argnum != argc) {
		mdb_printf("invalid arguments\n");
		return (DCMD_USAGE);
	}

	if ((opt_s != 0) && (opt_m != 0)) {
		mdb_printf("-s and -m cannot both be specified\n");
		return (DCMD_USAGE);
	}

	snarf_sets();

	if (opt_a == 0)
		opt_a = active_sets;

	/* find the array */
	if (mdb_lookup_by_name("md_set_io", &setiosym) == -1) {
		mdb_warn("SVM - no set io counts set\n");
		return (DCMD_ERR);
	}

	if (md_verbose) {
		mdb_printf("Base address for the md_set_io array: %p\n",
		    setiosym.st_value);
	}
	if (opt_s != NULL) {
		setno = findset(opt_s);
		if (setno == -1) {
			mdb_warn("no such set: %s\n", opt_s);
			return (DCMD_ERR);
		}
		opt_m = setno;
	}

	if (opt_m > 0) {
		mdb_printf("%lld]\t%ld\t%ld", opt_m,
		    md_setio[opt_m].io_cnt, md_setio[opt_m].io_state);
		mdb_printf("\t%hb\n", io_state_bits);
		return (DCMD_OK);
	}

	if (opt_a == 0) {
		mdb_warn("No active set!\n");
		return (DCMD_ERR);
	}

	for (i = 0; i < opt_a; i++) {
		if (mdb_vread(&md_setio[i], sizeof (md_set_io_t),
		    setiosym.st_value + offset) == -1) {
			mdb_warn("failed to read md_set_io_t at 0x%x\n",
			    setiosym.st_value + offset);
		}
		mdb_printf("%lld]\t%ld\t%ld", i, md_setio[i].io_cnt,
		    md_setio[i].io_state);
		mdb_printf("\t%hb", io_state_bits);
		if (md_verbose) {
			mdb_printf(" - io_cnt: %p",
			    setiosym.st_value + offset + sizeof (kmutex_t) +
			    sizeof (kcondvar_t));
			mdb_printf(" %d", sizeof (md_set_io_t));
		}
		mdb_printf("\n");
		offset += sizeof (md_set_io_t);
	}
	return (DCMD_OK);
}
Ejemplo n.º 25
0
int findset(int a)
{
    return a!=p[a]? findset(p[a]):a;
}
Ejemplo n.º 26
0
int findset(int x)//递归写法,其实有更凶残的非递归写法
{
    return p[x] == -1 ? x : p[x] = findset(p[x]);//带路径压缩的查找
}
Ejemplo n.º 27
0
void AGOSEngine_PN::opn_opcode51() {
	_fnst = varval();
	setScriptReturn(findset());
}
Ejemplo n.º 28
0
Archivo: p3.cpp Proyecto: alxsoares/OI
int Solve::findset(int u)
{
	if(parent[u] != u)
		parent[u] = findset(parent[u]);
	return parent[u];
}
Ejemplo n.º 29
0
int findset(int x){
    return p[x]==x ? x : p[x]=findset(p[x]);
}
Ejemplo n.º 30
0
int findset(int x) {
	if(x != p[x])
		p[x] = findset(p[x]);
	return p[x];
}