I'm trying to solve problem 10158 but always get TLE.
![:evil:](./images/smilies/icon_evil.gif)
This problem should be about "disjiont set".
But it's like to think about so many conditions .
Could someone tell me how to solve this problem in detail ??
THX!!
Moderator: Board moderators
Code: Select all
ACed
Code: Select all
10
2 7 4
1 9 4
3 8 2
1 5 5
2 7 1
4 5 2
4 6 1
1 2 3
1 2 1
1 8 5
4 6 1
3 9 2
4 9 5
3 3 1
3 3 3
1 1 1
0 0 0
Code: Select all
0
0
0
0
1
0
1
1
Code: Select all
ACed
I don't understand what you are mentioning. However test the set...hi!man! wrote: I think that it should not have a "s" in setFriend()
Code: Select all
10
4 4 0
2 4 8
3 2 4
2 5 1
4 1 1
4 2 7
1 1 4
3 3 2
3 1 6
3 5 7
3 1 8
2 2 7
4 5 4
4 1 2
2 3 4
2 1 3
1 7 4
3 7 7
4 3 1
2 8 6
0 0 0
Code: Select all
0
0
0
0
0
0
0
0
1
0
1
1
Code: Select all
#include <stdio.h>
int main(){
int f[10000];
int kind,b,c;
int people=0;
int count=1;
scanf("%d",&people);
for(int i=0;i<people;i++)f[i]=0;
scanf("%d %d %d",&kind,&b,&c);
while ((kind!=0)||(b!=0)||(c!=0)){
if(kind==1){
if (f[b]==0&&f[c]==0){
f[b]=count;
f[c]=count;
count++;
}
else if (f[b]!=0&&f[c]==0) f[c]=f[b];
else if (f[c]!=0&&f[b]==0) f[b]=f[c];
else if (f[b]!=f[c]&&f[b]!=-f[c]) for(int i=0;i<people;i++){if(f[i]==f[c])f[i]=f[b];
if(f[i]==-f[c])f[i]=-f[b];}
else if (f[c]==-f[b]) printf("-1\n");
}
if(kind==2){
if(f[b]==f[c]){
if (f[b]==0&&f[c]==0){
f[b]=count;
f[c]=0-count;
count++;
}
else printf("-1\n");
}
else if (f[b]!=0&&f[c]==0) f[c]=-f[b];
else if (f[c]!=0&&f[b]==0) f[b]=-f[c];
else if (f[b]!=f[c]&&f[b]!=-f[c]) for(int i=0;i<people;i++){if(f[i]==f[c])f[i]=-f[b];
if(f[i]==-f[c])f[i]=f[b];}
}
if(kind==3){
if (f[b]==f[c]&&f[b]!=0&&f[c]!=0) printf("1\n");
else printf("0\n");
}
if(kind==4){
if (f[b]==-f[c]&&f[b]!=0&&f[c]!=0) printf("1\n");
else printf("0\n");
}
scanf("%d %d %d",&kind,&b,&c);
}
return 0;
}
Code: Select all
#include <stdio.h>
int *pai, *rank, *flag, pessoas;
void MakeSet(int x);
int FindSet(int x);
void Union(int x, int y);
void Separa(int x, int y);
int main()
{
int comando, n1, n2, i;
scanf("%d", &pessoas);
pai = calloc(pessoas, sizeof(int));
rank = calloc(pessoas, sizeof(int));
flag = calloc(pessoas, sizeof(int));
for (i = 0; i < pessoas; i++)
{
MakeSet(i);
}
while (scanf("%d %d %d", &comando, &n1, &n2))
{
if (comando == 0)
{
break;
}
n1 = FindSet(n1);
n2 = FindSet(n2);
if (comando == 1)
{
if (flag[n1] == n2)
{
printf("-1\n");
}
else
{
Union(n1, n2);
}
}
else if (comando == 2)
{
if (n1 == n2)
{
printf("-1\n");
}
else
{
Separa(n1, n2);
}
}
else if (comando == 3)
{
for (i = 0; i < pessoas; i++)
{
if (flag[n1] == i && flag[n2] == i)
{
n1 = n2;
break;
}
}
if (n1 != n2)
{
printf("0\n");
}
else
{
printf("1\n");
}
}
else
{
if (flag[n1] != n2)
{
printf("0\n");
}
else
{
printf("1\n");
}
}
}
return 0;
}
void MakeSet(int x)
{
pai[x] = x;
rank[x] = 1;
flag[x] = -1;
}
int FindSet(int x)
{
if (x != pai[x])
{
pai[x] = FindSet(pai[x]);
}
return pai[x];
}
void Union(int x, int y)
{
int i;
if (x == y)
{
return;
}
if (rank[x] > rank[y])
{
pai[x] = y;
rank[y] += rank[x];
for (i = 0; i < pessoas; i++)
{
if (flag[x] == i)
{
flag[y] = i;
flag[i] = y;
}
}
}
else
{
pai[y] = x;
rank[x] += rank[y];
for (i = 0; i < pessoas; i++)
{
if (flag[y] == i)
{
flag[x] = i;
flag[i] = x;
}
}
}
}
void Separa(int x, int y)
{
if (flag[x] == -1 && flag[y] == -1)
{
flag[x] = y;
flag[y] = x;
}
else
{
Union(flag[x], y);
Union(flag[y], x);
}
}
Code: Select all
5
1 0 1
1 2 3
2 1 3
1 0 2
0 0 0