This is my first post in csstack exchange, and I don't know if this type of questions can be posted here.
So I have been trying this problem for 3 days and I got a solution but it fails for the hidden test cases.
Question:
There is a village festival happening in which several group of relatives meet every year. Each person is allocated an identifier which is a positive integer.N pairs of relatives identifiers are passed as input.
Then finally given a person's identifier I, the program must print the count of the relatives C in the group of the person with the identifier I.
Input Format: The first line contains the values of N. N lines contain the identifiers of two persons who are related. The next line (N+2)th line, will contain the identifier I of the person for whom the relative count of his group is to be printed.
Output Format: The first line will contain the count of relatives C in the group of the person with identifier I.
Boundary Conditions: 1 <= N <= 100001 <= I <= 1000000
Example Input/Output 1: Input:
5
10 20
30 20
40 10
55 35
55 22
40
Output:4
Explanation:
10, 20, 30, 40 form a relative group. 55, 35, 22 form another relative group. So the count of relatives for the person with identifier 40 is 4.
The method I approached was:
for(auto i = v.begin() ; i!=v.end();i++)
{
if(i->first == r || i->second == r)
{
count+=2;
if(i->first == r)
r = i->second;
else
r = i->first;
remove(v.begin(),v.end(),*i);
n--;
break;
}
}
for(int i =0;i<n;i++)
{
for(int j =0;j<n;j++)
{
if(r == v[j].first || r == v[j].second)
{
if(r == v[j].first)
r = v[j].second;
else
r = v[j].first;
count++;
remove(v.begin(),v.end(),v[j]);
n--;
}
}
}
cout<<count;
So what's the correct solution to this problem?