-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_Split_Node.cc
More file actions
83 lines (77 loc) · 1.75 KB
/
Copy pathGet_Split_Node.cc
File metadata and controls
83 lines (77 loc) · 1.75 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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
class Solution {
public:
int getSplitNode(vector<string> matrix, int indexA, int indexB);
};
int Solution::getSplitNode(vector<string> matrix, int indexA, int indexB) {
for (int i=0; i < matrix.size(); ++i) {
for (int j=0; j < i; ++j) {
matrix[i][j] = '0';
}
}
queue<int> qn;
vector<int> svn;
qn.push(0);
while (!qn.empty()) {
for (int i=0; i < matrix.size(); ++i)
if (matrix[qn.front()][i] == '1')
qn.push(i);
svn.push_back(qn.front());
qn.pop();
}
stack<int> sn;
int pn = indexA;
for (int i=0; i < matrix.size(); ++i) {
if (matrix[pn][i] == '1' || matrix[i][pn] == '1') {
int ipn = 0, ii = 0;
for (int j=0; j < svn.size(); ++j) {
if (svn[j] == pn) ipn = j;
if (svn[j] == i) ii = j;
}
if (ii < ipn) {
sn.push(i);
pn = i;
}
}
}
/*
while (!sn.empty()) {
cout << sn.top() << endl;
sn.pop();
}
*/
pn = indexB;
for (int i=0; i < matrix.size(); ++i) {
if (matrix[pn][i] == '1' || matrix[i][pn] == '1') {
int ipn = 0, ii = 0;
for (int j=0; j < svn.size(); ++j) {
if (svn[j] == pn) ipn = j;
if (svn[j] == i) ii = j;
}
if (ii < ipn) {
stack<int> snc = sn;
while (!snc.empty()) {
if (i == snc.top())
return i;
snc.pop();
}
pn = i;
}
}
}
return 0;
}
int main(int argc, char **argv) {
Solution s;
vector<string> matrix;
matrix.push_back("01011");
matrix.push_back("10100");
matrix.push_back("01000");
matrix.push_back("10000");
matrix.push_back("10000");
cout << s.getSplitNode(matrix, 0, 0) << endl;
}