In a social network, online communities refer to the group of people with an interest towards the same topic. People connect with each other in a social network. A connection between Person I and Person J is represented as C I J. When two persons belonging to different communities connect, the net effect is merger of both communities which I and J belonged to.
We are only interested in finding out the communities with the member count being an even number. Your task is to find out those communities.
Input Format:
Input will consist of three parts, viz.
1. The total number of people on the social network (N)
2. Queries
· C I J, connect I and J
· Q 0 0, print the number of communities with even member-count -1 will represent end of input.
Output Format:
For each query Q, output the number of communities with even member-count.
Constraints:
1<=N<=10^6
1<=I, J<=N
Input:
5
Q 0 0
C 1 2
Q 0 0
C 2 3
Q 0 0
C 4 5
Q 0 0
-1
Output:
0
1
0
1
Explanation:
For first query there are no members in any of the groups hence answer is 0. After C 1 2, there is a group (let’s take it as G1) with 1 and 2 as members hence total count at this moment is 1. After C 2 3 total members in G1 will become {1, 2, 3} hence there are no groups with even count. After C 4 5 there formed a new group G2 with {4, 5} as members, hence the total groups with even count is 1.
Solution: In C
#include<stdio.h>
int comm[1000001],n;
void mergecom(int,int);
void print();
int even(int);
int main(){
int x,y,comNo=1;
scanf("%d",&n);
char c;
while(1){
scanf("%c",&c);
if(c=='C'){
scanf("%d %d",&x,&y);
if(comm[x]==0 && comm[y]==0){
comm[x]=comm[y]=comNo;
comNo++;
}
else if(comm[x]==0){
comm[x]=comm[y];
}
else if(comm[y]==0){
comm[y]=comm[x];
}
else if(comm[x]>comm[y]){
mergecom(comm[y],comm[x]);
comNo--;
}
else if(comm[y]>comm[x]){
mergecom(comm[x],comm[y]);
comNo--;
}
}
else if(c=='Q'){
scanf("%d %d",&x,&y);
printf("%d",even(comNo));
}
else
break;
}
print();
return 0;
}
void mergecom(int x,int y){
for(int i=0;i<=n;i++){
if(comm[i]==y)
comm[i]=x;
else if(comm[i]>y)
comm[i]--;
}
}
int even(int comNo){
int count =0,temp=0;
for(int i=1;i<comNo;i++){
for(int j=0;j<=n;j++){
if(comm[j]==i)
temp++;
}
if(temp%2==0 && temp!=0){
count++;
}
temp=0;
}
return count;
}
void print(){
for(int i=0;i<n;i++)
printf("%d %d\n",i,comm[i]);
}
Solution: In Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bufferedReader.readLine());
int[] users = new int[N + 1],
groupSizes = new int[N / 2 + 1];
int group = 0;
String line;
while (!(line = bufferedReader.readLine()).equals("-1")) {
if (line.startsWith("C")) {
String[] inputs = line.split(" ");
int I = Integer.parseInt(inputs[1]),
J = Integer.parseInt(inputs[2]);
if (users[I] != 0) {
users[J] = users[I];
groupSizes[users[I]]++;
} else if (users[J] != 0) {
users[I] = users[J];
groupSizes[users[J]]++;
} else {
group++;
users[I] = group;
users[J] = group;
groupSizes[group] = 2;
}
} else if (line.equals("Q 0 0")) {
if (group == 0) {
System.out.println("0");
continue;
}
int evenGroups = 0;
for (int groupSize : groupSizes) {
if (groupSize > 0 && groupSize % 2 == 0) {
evenGroups++;
}
}
System.out.println(evenGroups);
}
}
}
}
Also Checkout