Repository navigation
Expand file tree
/
Copy path133.cpp
More file actions
97 lines (93 loc) · 2.56 KB
/
Copy path133.cpp
File metadata and controls
97 lines (93 loc) · 2.56 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
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
96
97
#include <vector>
#include "Jutil.h"
using namespace std;
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
class Solution {
public:
Node* cloneGraph(Node* node) {
if (node == nullptr) {
return nullptr;
}
vector<Node*> arr;
stack<Node*> st;
st.push(node);
unordered_map<int, bool> vis;
while (!st.empty()) {
Node* t = st.top();
st.pop();
if (vis[t->val]) continue;
int i = t->val - 1;
while (i >= arr.size()) {
arr.push_back(new Node());
}
arr[i]->val = t->val;
vis[t->val] = true;
for (auto& e : t->neighbors) {
if (vis[e->val]) continue;
st.push(e);
}
}
st.push(node);
vis.clear();
while (!st.empty()) {
Node* t = st.top();
st.pop();
if (vis[t->val]) continue;
vis[t->val] = true;
for (auto& e : t->neighbors) {
arr[t->val - 1]->neighbors.push_back(arr[e->val - 1]);
if (vis[e->val]) continue;
st.push(e);
}
}
return arr[0];
}
};
int main() {
string problemName = "133";
auto begin = jtimer();
Solution sol;
ifstream file_in("testcases/" + problemName + "_in.txt");
ifstream file_out("testcases/" + problemName + "_out.txt");
bool allPass = true;
int caseCount = 0;
int passCount = 0;
string line_in;
string line_out;
while (getline(file_in, line_in)) {
auto nodes = jread_graph(line_in);
Node* node = nullptr;
if (nodes.size() > 0) {
node = nodes[0];
}
auto res = sol.cloneGraph(node);
getline(file_out, line_out);
auto anss = jread_graph(line_out);
Node* ans = nullptr;
if (anss.size() > 0) {
ans = anss[0];
}
printf("Case %d", ++caseCount);
if (graphEqual(res, ans)) {
passCount++;
printf(" %s(PASS)", KGRN);
} else {
printf(" %s(WRONG)", KRED);
allPass = false;
}
printf("\n%s", KNRM);
jprint(res, "res");
jprint(ans, "ans");
printf("\n");
}
if (allPass) {
printf("%sALL CORRECT [%d/%d]\n%s", KGRN, passCount, caseCount, KNRM);
} else {
printf("%sWRONG ANSWER [%d/%d]\n%s", KRED, passCount, caseCount, KNRM);
}
auto end = jtimer();
jprint_time(begin, end);
return 0;
}