コード例 #1
0
ファイル: 4498488_RE.cpp プロジェクト: suikay/acm
int main()
{
	char s[100];
	int n,count = 0,lenght,max;
	int x,y;
	scanf("%d\n",&n);
	for( int i = 1; i <= n; i++) {
		for(int j = 1; j < i;j++) {
			scanf("%d",&lenght);
			edge[count].x = i;
			edge[count].y = j;
			edge[count].lenght = lenght;
			count++;
		}
		gets(s);
	}
	qsort(edge,count,sizeof(Edge),compare);
	count = n - 1;
	scanf("%d",&n);
	for( int i = 0; i < n; i++) {
		scanf("%d%d",&x,&y);
		MergeSet(x,y);
		count--;
	}
	for( int i = 0; count; i++) 
		if(FindParent(edge[i].x) != FindParent(edge[i].y)) {
			MergeSet(edge[i].x,edge[i].y);
			count--;
			max = edge[i].lenght;
		}
	printf("%d\n",max);
}
コード例 #2
0
ファイル: 4487204_WA.cpp プロジェクト: suikay/acm
int MergeOppisiteSet(int a,int b)
{
	bool flag = true;
	int t1 = FindParent(a);
	int t2 = FindParent(b);
	if( t1 == t2 || aparent[t1] == aparent[t2])
		return 0;
	if( aparent[t1] != t2)
		MergeSet(aparent[t1],t2);
	if( aparent[t2] != t1)
		MergeSet(aparent[t2],t1);
	return 1;
}
コード例 #3
0
ファイル: 4495036_AC_297MS_688K.cpp プロジェクト: suikay/acm
int main()
{
	int m,n;
	int count = 0;
	scanf("%d",&n);
	for( int i = 0; i < n; i++) {
		scanf("%d",&m);
		for( int j = 0; j < m; j++)
			parent[j] = j;
		for( int j = 0; j < m; j++)
			for( int k = 0; k < m; k++) {
				scanf("%d",&edge[count].weigh);
				edge[count].x = j;
				edge[count++].y = k;
			}
		qsort(edge,m * m,sizeof(Edge),compare);
		count = 1;
		int max;
		for( int j = m; j < (m * m) && count < m; j++)
			if(FindParent(edge[j].x) != FindParent(edge[j].y)) {
				count ++;
				MergeSet(edge[j].x,edge[j].y);
				max = edge[j].weigh;
			}
		printf("%d\n",max);
	}
}
コード例 #4
0
ファイル: 4497296_WA.cpp プロジェクト: suikay/acm
int main()
{
	int n,m,t;
	int x,y;
	char s[20];
	scanf("%d",&t);
	for( int i = 0; i < t; i++) {
		memset(parent,0,100001 * sizeof(int));
		memset(rela,0,100001 * sizeof(bool));
		scanf("%d%d\n",&n,&m);
		for( int j = 0; j < m; j++) {
			gets(s);
			if(s[0] == 'A') {
				sscanf(s,"A %d%d",&x,&y);
				if(FindParent(x) == FindParent(y))
					if( cal(x) == cal(y))
						printf("In the same gang.\n");
					else
						printf("In different gangs.\n");
				else
					printf("Not sure yet.\n");
			}
			else {
				sscanf(s,"D %d%d",&x,&y);
				if( FindParent(x) != FindParent(y))
				MergeSet(x,y);
				}
		}
	}
}
コード例 #5
0
ファイル: 4494460_RE.cpp プロジェクト: suikay/acm
int main()
{
	int n, count;
	char x,y;
	int v, weigh, sum;
	scanf("%d",&n);
	while( n) {
		getchar();
		count = sum = 0;
		for( int i = 0; i < n - 1; i++) {
			scanf("%c%d",&x,&v);
			x -= 'A';
			for( int j = 0; j < v; j++) {
				scanf(" %c%d",&y,&weigh);
				y -= 'A';
				edge[count].x = x;
				edge[count].y = y;
				edge[count++].weigh = weigh;
			}
			getchar();
		}
		qsort(edge,count,sizeof(Edge),compare);
		memset(parent,0,26 * sizeof(int));
		for(int i = 0; i < count; i++)
			if(FindParent(edge[i].x) != FindParent(edge[i].y)) {
				sum += edge[i].weigh;
				MergeSet(edge[i].x,edge[i].y);
			}
		printf("%d\n",sum);
		scanf("%d",&n);
	}
}
コード例 #6
0
ファイル: 4486949_AC_16MS_320K.cpp プロジェクト: suikay/acm
int main()
{
	int parent[30001];
	int m,n,time;
	int t1,t2;
	int count;
	scanf("%d%d",&n,&m);
	while(n) {
		count = 0;
		for( int i = 0; i < n; i++)
			parent[i] = i;
		for (int i = 0; i < m; i++) {
			scanf("%d%d",&time,&t1);
			for( int j = 1; j < time; j++) {
				scanf("%d",&t2);
				MergeSet(parent,t1,t2);
				t1 = t2;
			}
		}
		for( int i = 0; i < n; i++)
			if(!FindParent(parent,i)) 
				count++;
		printf("%d\n",count);
		scanf("%d%d",&n,&m);
	}
	return 0;
}
コード例 #7
0
void Pr7Solve()
{   
	/* 0 - set contains a bridge
	 * 1 - set contains a fork 
	 * SetState[0] contains the smallest stone id. The first 
	 * stone that causes win structure.
	 */
	int SetState[3];
    int StoneX[STONES_MAX];
	int StoneY[STONES_MAX];
	struct Pr7SetItem StonesSet[HEX_COORD_MAX][HEX_COORD_MAX]; 
	/* This array represents all hexagon's cells */
    int Hexagon[HEX_COORD_MAX][HEX_COORD_MAX];
	/* Number of testcases */
	int testCaseNumber;
	int hexagonSize;
	int stonesNumber;
    int i, j, t, k;
	int countRing;
	/* Reading of number of testcases */
    scanf("%d", &testCaseNumber);
    /* Process all testcases */
    for (i = 1; i <= testCaseNumber; i++)
    {
		/* Read of stones number and hexagon size */
        scanf("%d%d", &hexagonSize, &stonesNumber);
		/* Read all stones point destination inside the hexagon */
        for (j = 0; j < stonesNumber; j++)
        {
		    scanf ("%d%d", &StoneX[j], &StoneY[j]);
        }
		for (j = 0; j < 3; j++)
		{
            SetState[j] = INT_MAX;
		}
        /* Fill in all hexagon's cells by zero */
		memset(Hexagon, 0, sizeof(Hexagon));
		/* Read all stones sequentually and put them in the appropriate
		 * position inside the hexagon.
		 */
        for (j = 0; j < stonesNumber; j++)
        {
			int isBridgeOrFork;
			/* Hexagon[x][y] = 1 means that there is stone in the cell
			 * Hexagon[x][y] = 0 the cell is empty and we can put a stone here
			 */
			Hexagon[StoneX[j]][StoneY[j]] = 1;
            /* Create a set item for each stone */
			CreateSetItem(
				 StoneX[j], 
				 StoneY[j], 
				 &StonesSet[StoneX[j]][StoneY[j]], 
				 hexagonSize);
			/* Check all neighbour, adjacent cells whether there is  */
            for (t = 0; t < HEX_NEIGHBOURS_NUM; t++)
			{
				/* If neighbour has a stone we need to merge the current item
				 * with its neighbour.
				 */
                if (Hexagon[StoneX[j] + DeltaX[t]][StoneY[j] + DeltaY[t]])
				{
                     MergeSet(
						 StoneX[j], 
						 StoneY[j], 
	                     StoneX[j] + DeltaX[t], 
					     StoneY[j] + DeltaY[t], 
                         StonesSet,
						 SetMask
						 );
				}
			}
            
			Hexagon[StoneX[j]][StoneY[j]] = 1;

			isBridgeOrFork = IsBridgeOrFork(StoneX[j], StoneY[j], StonesSet);

            if ((isBridgeOrFork & 1) && j < SetState[0])
			{
                SetState[0] = j;
			}

            if ((isBridgeOrFork & 2) && j < SetState[1])
			{
                SetState[1] = j;
			}
		}

		count2 = 1;
        
		StonesSet[0][0].ParentIdX = 0;
		StonesSet[0][0].ParentIdY = 0;

		/* Go through all cells inside hexagon and find ones 
		 * suspicious for being inside a circle.
		 * We take only those cells, which are inside the hexagon
		 * and there are not a stone in it.
		 */
        for (j = 0; j < 2 * hexagonSize - 1; j++)
		{
            for (t = 0; t < 2 * hexagonSize - 1; t++)
			{
                if (InHexagon(j, t, hexagonSize) && !Hexagon[j][t])
				{
					Hexagon[j][t] = 2;

                    StonesSet[j][t].ParentIdX = j;
		            StonesSet[j][t].ParentIdY = t;

					count2++;

					for (k = 0; k < 6; k++)
					{
                        if (Hexagon[j + DeltaX[k]][t + DeltaY[k]] == 2)
				        {
                            MergeSet(
						       j, 
						       t, 
	                           j + DeltaX[k], 
					           t + DeltaY[k], 
                               StonesSet,
						       IncrementCount2
						    );      
						}
					}

                    if (AtEdge(j, t, hexagonSize))
					{
                        MergeSet(
						       j, 
						       t, 
	                           0, 
					           0, 
                               StonesSet,
						       IncrementCount2
						    );      
					}
				}
			}
		}
        /* Here we need some explanation about count2. 
		 * In case when there is a ring after the set union 
		 * we always have count2 set to 2. Lets count how many merge operations 
		 * we need to unite the adjacents set of n cells in an arbitrary order
		 * For two adjacenr cells we need count2 = 1. For three we need 2
		 * For n we need n - 1 merge operations. 
		 * So we have n increments of the count2 variable and n - 1 decrements of 
		 * this count2. So after this we have count2 = 1 + 1.
		 * When we process a set which has at least on cell on the edge of the hexagon.
		 * we merge only one with the point (0, 0) and decrement the count2 variable as well
		 * This means that after this we have count2 = 0.
		 * If count2 > 1 that means there at least one ring inside the hexagon
		 */
		if (count2 > 1)
		{
			/* We have at least one winning ring.
			 * We set the last stone which makes the winning ring to the last stone
			 */
            SetState[2] = stonesNumber - 1;
		}
        
		/* We need to find the first stone which causes the winning ring
		 * We loop from the latest stone to the first and remove it from the 
		 * hexagon. When we remove a stone there are 4 possible variants:
		 *
		 * 1. Extend existing space. 
		 *    If we discover a ring count2 +1 
		 *    If we discover a ring which on the edge +0
		 *    If we extend an existed ring count2 +0
		 *    If we extend an existed ring on an edge count2 +0
		 *
		 * 2. Unite two rings. 
		 *    Remove stone +1, unite cell with the first set -1,
		 *    unite the cell with the second set -1. When we unite two
		 *    rings into one ring we decrement count2.
		 * 
		 * 3. Unite a ring with edge ring. Also decrement count2
		 * 
		 */
        for (j = stonesNumber - 1; j > 0; j--)
        {
            Hexagon[StoneX[j]][StoneY[j]] = 2;

            StonesSet[StoneX[j]][StoneY[j]].ParentIdX = StoneX[j];
		    StonesSet[StoneX[j]][StoneY[j]].ParentIdY = StoneY[j];

			count2++;
            
			for (k = 0; k < 6; k++)
			{
                if (Hexagon[StoneX[j] + DeltaX[k]][StoneY[j] + DeltaY[k]] == 2)
		        {
                    MergeSet(
				       StoneX[j], 
				       StoneY[j], 
                       StoneX[j] + DeltaX[k], 
			           StoneY[j] + DeltaY[k], 
                       StonesSet,
				       IncrementCount2
				    );      
				}
			}

            if (AtEdge(StoneX[j], StoneY[j], hexagonSize))
			{
                MergeSet(
				       StoneX[j], 
				       StoneY[j], 
                       0, 
			           0, 
                       StonesSet,
				       IncrementCount2
				    );      
			}

			if (count2 > 1)
			{
				SetState[2] = j - 1;
			}
		}
	}
}