-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion61.c
150 lines (117 loc) · 2.54 KB
/
question61.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
http://practice.geeksforgeeks.org/problems/circle-of-strings/0
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
struct adjListNode{
int data;
struct adjListNode *next;
};
struct adjList{
struct adjListNode *head;
};
struct graph{
int vertices, edges;
struct adjList *arr;
};
struct adjListNode *newNode(int data){
struct adjListNode *temp = (struct adjListNode *)malloc(sizeof(struct adjListNode));
temp->data = data;
temp->next = NULL;
return temp;
}
void addEdge(struct graph *newGraph, char start, char end) {
struct adjListNode *temp = newNode(end);
if(newGraph->arr[start].head) {
temp->next = newGraph->arr[start].head;
newGraph->arr[start].head = temp;
}else{
newGraph->arr[start].head = temp;
}
}
void getDegrees(struct graph *newGraph, int *inDegree, int *outDegree) {
int i;
struct adjListNode *temp;
for(i=0;i<newGraph->vertices;i++){
temp = newGraph->arr[i].head;
int count = 0;
while(temp){
inDegree[temp->data]++;
temp = temp->next;
count++;
}
outDegree[i] = count;
}
}
void dfs(int index, struct graph *newGraph, int *visited) {
visited[index] = 1;
struct adjListNode *temp = newGraph->arr[index].head;
while(temp) {
if(!visited[temp->data]) {
dfs(temp->data, newGraph, visited);
}
temp = temp->next;
}
}
int isEuler(struct graph *newGraph) {
int vertices = newGraph->vertices;
int inDegree[vertices];
int outDegree[vertices];
int visited[vertices];
int i;
for(i=0;i<vertices;i++){
inDegree[i] = 0;
outDegree[i] = 0;
visited[i] = 0;
}
getDegrees(newGraph, inDegree, outDegree);
for(i=0;i<vertices;i++){
if(inDegree[i] != outDegree[i]){
return 0;
}
}
i = 0;
while(inDegree[i] + outDegree[i] <= 0){i++;}
dfs(i, newGraph, visited);
for(i=0;i<vertices;i++){
if(inDegree[i] + outDegree[i] > 0 && !visited[i]){
return 0;
}
}
return 1;
}
int isCircle(int n, int length, char arr[n][length]) {
int i;
struct graph *newGraph = (struct graph *)malloc(sizeof(struct graph));
newGraph->arr = (struct adjList *)calloc(26,sizeof(struct adjList));
newGraph->vertices = 26;
for(i=0;i<n;i++) {
int size = strlen(arr[i]);
int start = arr[i][0] - 'a';
int end = arr[i][size-1] - 'a';
addEdge(newGraph,start,end);
}
if(isEuler(newGraph)){
return 1;
}
return 0;
}
int main() {
int cases;
int i;
scanf("%d",&cases);
for(i=0;i<cases;i++) {
int n;
scanf("%d",&n);
int length = 11;
char arr[n][length];
int j;
for(j=0;j<n;j++){
scanf("%s", arr[j]);
}
printf("%d\n", isCircle(n,length,arr));
}
return 0;
}