-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_paths_of_length_2.cpp
60 lines (53 loc) · 1.34 KB
/
print_paths_of_length_2.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
#include <iostream>
#include <vector>
using namespace std;
#define _CRT_SECURE_NO_DEPRECATE
#define __elshorpagi__ (ios_base::sync_with_stdio(false), cin.tie(NULL))
#define sz(v) ((int)((v).size()))
#define edl '\n'
typedef vector<vector<int>> GRAPH;
void add_directed_edge(GRAPH &graph, int from, int to)
{
graph[from].emplace_back(to);
}
void print_paths_len_2(GRAPH &graph)
{
int nodes(sz(graph)), first, second;
for (int i(0); i < nodes; ++i) // iterate on nodes
{
for (int j(0); j < sz(graph[i]); ++j) // iterate on the node's neighbors
{
first = graph[i][j];
for (int k(0); k < sz(graph[first]); ++k) // iterate on the neighbor's neighbors
{
second = graph[first][k];
cout << i << ' ' << first << ' ' << second << edl;
}
}
}
}
void Solve()
{
int nodes, edges;
cin >> nodes >> edges;
GRAPH graph(nodes);
for (int e(0); e < edges; ++e)
{
int from, to;
cin >> from >> to;
add_directed_edge(graph, from, to);
}
print_paths_len_2(graph);
cout << edl << "DONE" << edl;
}
int main()
{
__elshorpagi__;
freopen("../test/input.txt", "r", stdin);
freopen("../test/output.txt", "w", stdout);
int tc(1);
// cin >> tc;
while (tc--)
Solve();
return (0);
}