-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2097.cpp
More file actions
39 lines (33 loc) · 1.01 KB
/
Copy path2097.cpp
File metadata and controls
39 lines (33 loc) · 1.01 KB
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
class Solution {
public:
vector<vector<int>> validArrangement(const vector<vector<int>> &pairs) const {
unordered_map<int, int> in, out, count;
unordered_map<int, vector<int>> adj;
for (const auto &p : pairs) {
adj[p[0]].push_back(p[1]);
out[p[0]]++, in[p[1]]++;
}
int start = pairs[0][0];
for (const auto &[v, _] : adj) {
if (out[v] - in[v] != 1) continue;
start = v;
break;
}
stack<int> st;
vector<int> res;
for (st.push(start); !st.empty();) {
const int crnt = st.top();
if (count[crnt] != size(adj[crnt])) {
st.push(adj[crnt][count[crnt]++]);
} else {
res.push_back(crnt);
st.pop();
}
}
vector<vector<int>> pair_res;
for (int i = size(res) - 2; i >= 0; i--) {
pair_res.push_back({res[i + 1], res[i]});
}
return pair_res;
}
};