Hackerearth problem-Assignment

Here is the link to the problem.How do I solve this problem.
Can anyone help me? The contest is over.

There are N men standing in a row.We can assign one of three color R(Red),G(Green) and Black(B) to each man. Unfortunately,at the end we forgot that what color we assigned to each man.But we remember a list of K relationships between pairs of men. Example 1st man and 2nd man has same color and 3rd man and 5th having different color. Your tast is to calculate number of possible assignmnet of colors to men.

INPUT:

Line 1:N and K.

Lines 2.K lines: Each line describes the relationship between a pair of men A and B (1 <= A,B <= N, A != B). It is either of the form “1 A B”, meaning that A and B have the same color, or “0 A B”, meaning that A and B have different colors.

OUTPUT:

Print an Integer indicating number of possible assignmnet of colors to men.
If given information is contradictory then print 0.
Constraints:

1<=N<=15

1<=k<=50

Sample Input

4 2

1 1 2

0 1 3

Sample Output

18

can anyone tell me how to approach this problem?

I have partially solved this question. Here is my solution. I’ve coded it pretty simply. If you find anything difficult to understand, just let me know.

#include
#include<bits/stdc++.h>
using namespace std;

int main()
{
int n,k,a,b,c,p1,i,arr[16];
cin>>n>>k;
for(i=0;i<16;i++)
{
arr[i]=1;
}
for(i=1;i<=n;i++)
arr[i]=3;
for(i=0;i<k;i++)
{
cin>>a>>b>>c;
if(a==1)
{
arr[b]=3;
arr[c]=1;
}
if(a==0)
{
arr[b]=3;
arr[c]=2;
}
}
p1=1;
for(i=1;i<16;i++)
{
p1=p1*arr[i];
}
cout<<p1<<endl;
}

I was able to get the partial solution.But it does not work in many cases.
This is incorrect solution. Moreover,You have not taken account the case where information is contradictory.(See the above question).