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 e1efe2b

Browse files
Added problems
1 parent 104c7fa commit e1efe2b

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

‎round_trip.cpp‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long int li;
6+
typedef long long int lli;
7+
typedef vector<long long int> vlli;
8+
typedef vector<int> vi;
9+
typedef stack<long long int> slli;
10+
typedef pair<lli, lli> plli;
11+
12+
class Graph
13+
{
14+
15+
vector<vector<lli>> adj;
16+
lli nodes;
17+
vector<bool> visited;
18+
vector<lli> path;
19+
lli pre;
20+
21+
public:
22+
Graph(lli nodes)
23+
{
24+
this->adj.resize(nodes, vector<lli>{});
25+
this->nodes = nodes;
26+
this->visited.resize(nodes, false);
27+
}
28+
29+
void addDirectedEdge(lli src, lli dst)
30+
{
31+
32+
if (src == dst)
33+
{
34+
return;
35+
}
36+
37+
this->adj[src].emplace_back(dst);
38+
}
39+
40+
void addUndirectedEdge(lli src, lli dst)
41+
{
42+
43+
if (src == dst)
44+
{
45+
return;
46+
}
47+
48+
this->adj[src].emplace_back(dst);
49+
this->adj[dst].emplace_back(src);
50+
}
51+
52+
bool dfs(lli node, lli parent)
53+
{
54+
55+
this->visited[node] = true;
56+
57+
this->path.push_back(node);
58+
59+
for (auto neighbour : this->adj[node])
60+
{
61+
62+
if (!this->visited[neighbour])
63+
{
64+
65+
if (dfs(neighbour, node))
66+
{
67+
return true;
68+
}
69+
}
70+
else if (neighbour != parent)
71+
{
72+
73+
this->path.push_back(neighbour);
74+
this->pre = neighbour;
75+
return true;
76+
}
77+
}
78+
79+
this->path.pop_back();
80+
return false;
81+
}
82+
83+
void searchRoundTrips()
84+
{
85+
86+
bool flag = false;
87+
88+
for (lli i = 0; i < this->nodes; i++)
89+
{
90+
91+
if (!this->visited[i])
92+
{
93+
94+
if (this->dfs(i, -1))
95+
{
96+
97+
flag = true;
98+
break;
99+
}
100+
}
101+
}
102+
103+
if (flag)
104+
{
105+
106+
lli j = 0;
107+
108+
for (j = 0; j < this->path.size(); j++)
109+
{
110+
111+
if (this->path[j] == this->pre)
112+
{
113+
break;
114+
}
115+
}
116+
117+
cout << this->path.size() - j << '\n';
118+
119+
for (lli i = j; i < this->path.size(); i++)
120+
{
121+
122+
cout << this->path[i] + 1 << ' ';
123+
}
124+
125+
cout << '\n';
126+
}
127+
else
128+
{
129+
130+
cout << "IMPOSSIBLE\n";
131+
}
132+
}
133+
};
134+
135+
void task()
136+
{
137+
lli cities, roads;
138+
cin >> cities >> roads;
139+
Graph g(cities);
140+
lli a, b;
141+
for (lli i = 0; i < roads; i++)
142+
{
143+
cin >> a >> b;
144+
g.addUndirectedEdge(a - 1, b - 1);
145+
}
146+
147+
g.searchRoundTrips();
148+
}
149+
150+
int main()
151+
{
152+
153+
ios::sync_with_stdio(0);
154+
cin.tie(0);
155+
156+
task();
157+
158+
return 0;
159+
}

0 commit comments

Comments
(0)

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