Code: Select all
I got AC Thanks :)
Moderator: Board moderators
Code: Select all
I got AC Thanks :)
Code: Select all
scanf("%d",&n);
while (getchar() != '\n') ;
UFDS* set=new UFDS(n);
char line[100];
while(gets(line) && sscanf(line, " %c %d %d", &type, &i, &j) == 3) {
Code: Select all
#include <cstdio>
using namespace std;
int pc[1000];
int rank[1000];
int parent(int i) { return (pc[i] == i) ? i : (pc[i] = parent(pc[i])); }
bool isConnected(int i, int j) { return parent(i) == parent(j); }
void join(int i, int j)
{
if(!isConnected(i, j))
{
int x = parent(i), y = parent(j);
if(rank[x] > rank[y]) pc[y] = x;
else { pc[x] = y; if(rank[x] == rank[y]) rank[y]++; }
}
}
int main()
{
int T; scanf("%d\n", &T);
while(T--)
{
int N;
scanf("\n%d\n", &N);
for(int i = 0; i < N; i++) { pc[i] = i; rank[i] = 0; }
char c;
int i, j;
int succ = 0, fail = 0;
while(scanf("%c %d %d\n", &c, &i, &j) == 3)
{
i--; j--;
if(c == 'c') join(i, j);
else isConnected(i, j) ? succ++ : fail++;
}
printf("%d,%d\n", succ, fail); if(T != 0) printf("\n");
}
return 0;
}
Code: Select all
2
4
c 1 4
c 2 4
q 1 3
q 1 4
10
c 1 5
c 2 7
q 7 1
c 3 9
q 9 6
c 2 5
q 7 5
Code: Select all
1,1
1,2
Code: Select all
1
2
q 1 2
q 1 2
Code: Select all
0,2
Code: Select all
code removed after AC
pd: don't forget that computers go from 1 to N, that was my problem