-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarketing.c
103 lines (87 loc) · 1.51 KB
/
marketing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
problem: http://community.topcoder.com/stat?c=problem_statement&pm=1524&rd=4472
Graph - DFS
*/
#include<stdio.h>
#define G 1000
#define RED 2
#define BLUE 3
#define TRUE 1
#define FALSE 0
int graph[G][G],connectedCount = 0;
void printGraph(int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
printf("%d ",graph[i][j]);
printf("\n");
}
}
int changeColor(int color)
{
if(color==RED)
return BLUE;
else if(color==BLUE)
return RED;
}
int dfs(int size,int start)
{
int stack[G],top=-1,current,color[G]={0},visited[G]={0},i;
stack[++top]=start;
color[start]=RED;
visited[start]=TRUE;
while(top>=0)
{
current = stack[top--];
connectedCount++;
// add neighbours.
for(i=0;i<size;i++)
{
if(graph[current][i]==1 && visited[i] == FALSE)
{
// Mark as visited.
visited[i]=TRUE;
if(color[i]==color[current])
{
// Not Possible.
return 0;
}
// Push to stack.
stack[++top]=i;
//Assign opposite color.
color[i]=changeColor(color[current]);
}
}
}
// Possible.
return 1;
}
int main(){
int total_products,i,ans;
scanf("%d",&total_products);
for(i=0;i<total_products;i++)
{
int num_competitor,temp,j;
scanf("%d",&num_competitor);
for(j=0;j<num_competitor;j++)
{
scanf("%d",&temp);
graph[i][temp]=1;
graph[temp][i]=1;
}
}
//printGraph(total_products);
if(dfs(total_products,0))
{
ans=2;
ans = ans << (total_products-connectedCount);
printf("%d\n",ans);
}
else
{
printf("-1");
}
return 0;
}