-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG Graph-12 CircleOfStrings.cpp
95 lines (83 loc) · 1.81 KB
/
GFG Graph-12 CircleOfStrings.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#include <vector>
#include <queue>
using namespace std;
vector < vector <int> > g;
vector <bool> vis;
void dfs(int k)
{
vis[k]=true;
for (int i=0;i<g[k].size();i++)
{
if (!vis[g[k][i]])
dfs(g[k][i]);
}
}
int main()
{
int n,t,ans=0;
string w;
vector <string> words;
cin >> n;
vector <int> in;
vector <int> out;
in.assign(30,0);
out.assign(30,0);
g.assign(30, vector <int> () );
for (int i=0;i<n;i++)
{
cin >> w;
words.push_back(w);
t=w[0]-'a';
out[t]++;
// cout <<t;
t=w[w.size()-1]-'a';
in[t]++;
// cout <<t;
}
for (int i=0;i<=26;i++) //Checking that in-degree and Out-degree are same
{
if (in[i]!=out[i])
{
cout << "Circle is Not Possible" << endl;
ans = 1; break;
}
else
continue;
}
int a,b,s,flag=-1;
for (int i=0;i<words.size();i++) //This creates a graph
{
a=words[i][0]-'a';
s=words[i].size();
b=words[i][s-1]-'a';
g[a].push_back(b);
}
vis.assign(30,false);
int p;
for (p=0;p<30;p++) //dfs IS executed for any character which is first ch. in a string
{
if (g[p].size())
break;
}
dfs(p);
for (int i=0;i<30;i++)
{
if (g[i].size())
{
if (vis[i]==false)
{
flag=0;
break;
}
}
flag=1;
}
if (ans==1 || flag==0)
cout << "Circle is not possible" << endl;
if (flag==1)
cout << "Circle is possible" << endl;
return 0;
}