Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 238082e

Browse files
Added problems
1 parent 5098174 commit 238082e

File tree

4 files changed

+452
-0
lines changed

4 files changed

+452
-0
lines changed

‎graph_algorithms/course-schedule.cpp‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int lli;
6+
typedef vector<lli> vi;
7+
8+
class Graph
9+
{
10+
11+
lli nodes;
12+
vector<vi> adj;
13+
stack<lli> order;
14+
vector<bool> white;
15+
vector<bool> gray;
16+
vector<bool> black;
17+
18+
public:
19+
Graph(lli _nodes)
20+
{
21+
this->nodes = _nodes;
22+
this->adj.resize(_nodes, vi{});
23+
this->white.resize(_nodes, true);
24+
this->gray.resize(_nodes, false);
25+
this->black.resize(_nodes, false);
26+
}
27+
28+
void addEdge(lli src, lli dst)
29+
{
30+
this->adj[src - 1].emplace_back(dst - 1);
31+
}
32+
33+
bool dfs(lli node)
34+
{
35+
this->white[node] = false;
36+
this->gray[node] = true;
37+
38+
for (auto nxt : this->adj[node])
39+
{
40+
if(this->white[nxt]) {
41+
if(this->dfs(nxt) == false) {
42+
return false;
43+
}
44+
} else if (this->black[nxt]) {
45+
continue;
46+
} else if(this->gray[nxt]) {
47+
return false;
48+
}
49+
}
50+
51+
this->order.push(node);
52+
this->gray[node] = false;
53+
this->black[node] = true;
54+
return true;
55+
}
56+
57+
void solve()
58+
{
59+
for (lli i = 0; i < this->nodes; i++)
60+
{
61+
if (this->white[i])
62+
{
63+
if(dfs(i) == false) {
64+
cout<<"IMPOSSIBLE\n";
65+
return;
66+
}
67+
}
68+
}
69+
70+
while (!this->order.empty())
71+
{
72+
cout << this->order.top() + 1 << ' ';
73+
this->order.pop();
74+
}
75+
76+
cout << '\n';
77+
}
78+
};
79+
80+
void task()
81+
{
82+
83+
lli lenCourses, lenPrereqs;
84+
cin >> lenCourses >> lenPrereqs;
85+
Graph g(lenCourses);
86+
lli pre, nxt;
87+
for (lli i = 0; i < lenPrereqs; i++)
88+
{
89+
cin >> pre >> nxt;
90+
g.addEdge(pre, nxt);
91+
}
92+
93+
g.solve();
94+
}
95+
96+
int main()
97+
{
98+
99+
ios::sync_with_stdio(false);
100+
cin.tie(NULL);
101+
102+
task();
103+
104+
return 0;
105+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int lli;
6+
typedef vector<lli> vi;
7+
8+
class Graph
9+
{
10+
11+
vector<vi> adj;
12+
vi distances;
13+
vi parents;
14+
lli nodes;
15+
vi longestPath;
16+
17+
public:
18+
Graph(lli _nodes)
19+
{
20+
this->nodes = _nodes;
21+
this->adj.resize(_nodes, vi{});
22+
this->distances.resize(_nodes, INT_MAX);
23+
this->parents.resize(_nodes, -1);
24+
}
25+
26+
void addEdge(lli src, lli dst)
27+
{
28+
src--;
29+
dst--;
30+
if (find(this->adj[src].begin(), this->adj[src].end(), dst) == this->adj[src].end())
31+
{
32+
this->adj[src].emplace_back(dst);
33+
}
34+
}
35+
36+
void dfs(lli node, lli parent, lli cost)
37+
{
38+
if (distances[node] > cost)
39+
{
40+
distances[node] = cost;
41+
parents[node] = parent;
42+
}
43+
44+
for (auto nxt : this->adj[node])
45+
{
46+
this->dfs(nxt, node, cost - 1);
47+
}
48+
}
49+
50+
void solve()
51+
{
52+
this->distances[0] = 0;
53+
this->dfs(0, -1, 0);
54+
55+
if (this->distances[this->nodes - 1] == INT_MAX)
56+
{
57+
cout << "IMPOSSIBLE\n";
58+
return;
59+
}
60+
61+
lli x = this->nodes - 1;
62+
stack<lli> path;
63+
cout << this->distances[this->nodes - 1] * -1 + 1 << '\n';
64+
while (x > -1)
65+
{
66+
path.push(x);
67+
x = parents[x];
68+
}
69+
70+
while (!path.empty())
71+
{
72+
cout << path.top() + 1 << ' ';
73+
path.pop();
74+
}
75+
76+
cout << '\n';
77+
}
78+
};
79+
80+
void task()
81+
{
82+
83+
lli lenCities, lenFlights;
84+
cin >> lenCities >> lenFlights;
85+
Graph g(lenCities);
86+
lli a, b;
87+
for (lli i = 0; i < lenFlights; i++)
88+
{
89+
cin >> a >> b;
90+
g.addEdge(a, b);
91+
}
92+
g.solve();
93+
}
94+
95+
int main()
96+
{
97+
98+
ios::sync_with_stdio(false);
99+
cin.tie(NULL);
100+
101+
task();
102+
103+
return 0;
104+
}

‎graph_algorithms/round-trip-ii.cpp‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int lli;
6+
typedef vector<lli> vi;
7+
typedef tuple<lli, lli> ii;
8+
typedef tuple<lli, lli, lli> iii;
9+
10+
class Graph
11+
{
12+
13+
vector<vi> adj;
14+
vector<bool> white;
15+
vector<bool> gray;
16+
vector<bool> black;
17+
vi parents;
18+
lli nodes;
19+
20+
public:
21+
Graph(lli _nodes)
22+
{
23+
this->nodes = _nodes;
24+
this->adj.resize(this->nodes, vi{});
25+
this->white.resize(this->nodes, true);
26+
this->gray.resize(this->nodes, false);
27+
this->black.resize(this->nodes, false);
28+
this->parents.resize(this->nodes, -1);
29+
}
30+
31+
bool isAvailable(lli src)
32+
{
33+
return this->white[src];
34+
}
35+
36+
void addEdge(lli src, lli dst)
37+
{
38+
this->adj[src - 1].emplace_back(dst - 1);
39+
}
40+
41+
bool dfs(lli node, lli parent)
42+
{
43+
44+
this->white[node] = false;
45+
this->gray[node] = true;
46+
this->parents[node] = parent;
47+
48+
for (auto nxt : this->adj[node])
49+
{
50+
51+
if (this->black[nxt])
52+
{
53+
continue;
54+
}
55+
else if (this->white[nxt])
56+
{
57+
if (this->dfs(nxt, node))
58+
{
59+
return true;
60+
}
61+
}
62+
else if (this->gray[nxt])
63+
{
64+
65+
// cycle detected
66+
vi cycle;
67+
68+
lli x = node;
69+
70+
while (x != nxt)
71+
{
72+
cycle.push_back(x);
73+
x = this->parents[x];
74+
}
75+
76+
if (cycle.size() == 0)
77+
{
78+
continue;
79+
}
80+
81+
cycle.push_back(nxt);
82+
cycle.push_back(node);
83+
84+
reverse(cycle.begin(), cycle.end());
85+
86+
cout << cycle.size() << '\n';
87+
for (auto n : cycle)
88+
{
89+
cout << n + 1 << ' ';
90+
}
91+
cout << '\n';
92+
93+
return true;
94+
}
95+
}
96+
97+
this->gray[node] = false;
98+
this->black[node] = true;
99+
return false;
100+
}
101+
};
102+
103+
void task()
104+
{
105+
106+
lli lenCities, lenFlights;
107+
cin >> lenCities >> lenFlights;
108+
Graph g(lenCities);
109+
lli a, b;
110+
for (lli i = 0; i < lenFlights; i++)
111+
{
112+
cin >> a >> b;
113+
g.addEdge(a, b);
114+
}
115+
for (lli i = 0; i < lenCities; i++)
116+
{
117+
if (g.isAvailable(i))
118+
{
119+
if (g.dfs(i, -1))
120+
{
121+
return;
122+
}
123+
}
124+
}
125+
cout<<"IMPOSSIBLE\n";
126+
}
127+
128+
int main()
129+
{
130+
131+
ios::sync_with_stdio(false);
132+
cin.tie(NULL);
133+
134+
task();
135+
136+
return 0;
137+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /