Ejemplo n.º 1
0
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size();
        if (m == 0) return 0;
        int n = grid[0].size();
        if (n == 0) return 0;
        
        make_set(id, m , n);
        num_components = (grid[0][0] == '1');

        // i == 0
        for (int j = 1; j < n; j++) {
            if (grid[0][j] == '0') continue;
            num_components++;
            if (grid[0][j - 1] == '1')
                unions(to1d(0, j, n), to1d(0, j - 1, n));
        }

        // j == 0
        for (int i = 1; i < m; i++) {
            if (grid[i][0] == '0') continue;
            num_components++;
            if (grid[i - 1][0] == '1')
                unions(to1d(i, 0, n), to1d(i - 1, 0, n)); 
        }
        
        //i != 0 && j != 0
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (grid[i][j] == '0') continue;
                num_components++;
                if (grid[i - 1][j] =='1') {
                    unions(to1d(i, j, n), to1d(i - 1, j, n));
                }
                if (grid[i][j - 1] == '1') {
                    unions(to1d(i, j, n), to1d(i, j - 1, n));
                }
            }
        }
        
        return num_components;
  
    }
Ejemplo n.º 2
0
int main()
{
    FILE *fp = fopen("input.txt", "r");
    FILE *fp2 = fopen("output.txt", "w");
    int n, m;
    memset(father, 0, sizeof(father));
    fscanf(fp, "%d%d", &n, &m);
    int a, b;
    bool flag = true;
    while(m--)
    {
        fscanf(fp, "%d%d", &a, &b);
        unions(a, b + n);//a与b标记为不同集合
        unions(b, a + n);
        if(getFather(a) == getFather(a+n) || getFather(b) == getFather(b+n))
        {
            flag = false;//出现矛盾情况
        }
    }
    if(flag)
    {
        fprintf(fp2, "Yes\n");
        int temp = getFather(1);
        for(int i = 1; i <= n; i++)
            if(getFather(i) == temp)
                fprintf(fp2, "1");
            else
                fprintf(fp2, "0");
        printf("\n");
    }
    else
        fprintf(fp2, "No\n");
    pclose(fp);
    pclose(fp2);
    return 0;
}
Ejemplo n.º 3
0
/*Performs the 'union' set operator*/
VehicleRegistry VehicleRegistry::Join(const VehicleRegistry& vr) const  // a function that returns an object
{
	
	VehicleRegistry unions(*this); //keyword 'this' is the pointer to the 'calling' object

	/*Go through the vr list, and use the find function with union as the calling object, if we can't find it in the union list then we add the vehicle to the union list */
	for (int i = 0; i < vr.numvehicles; i++)
	{		
		if (unions.Find(Vehicle(vr.vehicles[i].GetPlate(), "", "", "")) == -1)
		{
			unions.Insert(Vehicle(vr.vehicles[i].GetPlate(), vr.vehicles[i].GetMake(), vr.vehicles[i].GetModel(), vr.vehicles[i].GetColour()));
		}
	}

	return unions;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
    setIO("sample");CLEAR(st,0xff);
    n = gi;
    for(int i = 1;i<=n;fa[i]=i,++i) a[i] = gi,b[i] = a[i];
    sort(b+1,b+1+n);
    int l = unique(b+1,b+1+n)-b-1;
    for(int i = 1;i<=n;++i) a[i] = lower_bound(b+1,b+l+1,a[i])-b;
    a[0] = -INF,a[n+1] = -INF;
    for(int i = 0;i<=n+1;++i)
    {
     while(top && a[stk[top]]>a[i]) rm[stk[top]]=i,--top;
     ++top;stk[top] = i;
    }
    top = 0;
    for(int i = n+1;i>=0;--i)
    {
     while(top && a[stk[top]]>a[i]) lm[stk[top]]=i, --top;

     ++top;stk[top] = i;
    }
    top = 0;
    for(int i = 1;i<=n;++i)
     add(lm[i]+1,rm[i]-1,0,i);
    for(int i = 1;i<=n;++i){
     while(top && a[stk[top]] <= a[i]) unions(i,stk[top]),--top;
     ++top; stk[top] = i;
     TRA(x,i) 
     ans[lk[x].w] = a[gf(lk[x].v)];
    }
    for(int i = 1;i<=n;++i)
     printf(i==1?"%d":" %d",b[ans[i]]-b[a[i]]);
    puts(" ");
    closeIO();
    return EXIT_SUCCESS;
}
Ejemplo n.º 5
0
void main()
{
		int edg,ver,i,j,k,a,b,ctr=0;
		clrscr();
		printf("\n Enter Number Of Vertex :");
		scanf("%d",&ver);
		printf("\n Enter Number Of Edges : ");
		scanf("%d",&edg);
		g=(struct graph *)malloc(sizeof(struct graph)*ver);

		for(i=0;i<edg;i++)
		{
				parent[i]=0;
				printf("\n Enter Vertex 1:");
				scanf("%d",&g[i].u);
				printf("\n Enter Vertex 2:");
				scanf("%d",&g[i].v);
				printf("\n Enter Cost:");
				scanf("%d",&g[i].cost);
				printf("\n");
		}
		//clrscr();

		for(i=0;i<edg-1;i++)
		{
			for(j=i+1;j<edg;j++)
				{
					if(g[i].cost>g[j].cost)
						{
								temp=g[i];
								g[i]=g[j];
								g[j]=temp;
						}
				}
		}


		for(k=0;k<edg;k++)
		{
				i=g[k].u;
				j=g[k].v;
				a=find(i);
				b=find(j);

				if(a!=b)
				{
						t[ctr][0]=i;
						t[ctr][1]=j;

						if(ctr==ver-1)
						{
							break;
						}
						ctr++;
						unions(i,j);
				}
		}

		for(i=0;i<ctr;i++)
		{
			printf("\n %d -> %d",t[i][0],t[i][1]);
		}
getch();
}