Hi, Im trying to send the folowing code, but I am geting wrong answear, I cant find where Im wrong... Im using quickfind algorithm, can anyone help?
[c]
"@BEGIN_OF_SOURCE_CODE"
/* @JUDGE_ID: xxxxxx 793 C */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
typedef unsigned long mytype;
int
main(int argc, char **argv)
{
char op;
mytype p, q, N;
mytype i, j;
mytype conected, unconected;
mytype *id;
char buff[500];
char getop(void);
void unionid(mytype *id, mytype p, mytype q, mytype N);
int find(mytype *id, mytype p, mytype q);
scanf("%ld", &N);
/* inicializa os vetores que conterao
* os logs da coneccao */
id = (mytype *) calloc(N+1, sizeof(mytype));
for (i = 1; i <= N; i++)
id[i] = i;
conected = unconected = 0;
while (fgets(buff, 500, stdin) != NULL) {
sscanf(buff, "%c %ld %ld", &op, &p, &q);
switch (op) {
case 'c': /* union */
if (!find(id, p, q))
unionid(id, p, q, N);
break;
case 'q': /* find */
if (find(id, p, q))
conected++;
else
unconected++;
break;
}
}
printf("%ld,%ld\n", conected, unconected);
return 0;
}
void unionid(mytype *id, mytype p, mytype q, mytype N)
{
mytype t, i;
for (t = id[p], i = 1; i <= N; i++)
if (id[i] == t)
id[i] = id[q];
}
int find(mytype *id, mytype p, mytype q)
{
return (id[p] == id[q]) ? 1 : 0;
}
"@END_OF_SOURCE_CODE"
[/c]

