Skip to content

Гусев Иван 312 группа #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion task_01/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ add_executable(${PROJECT_NAME}_tests ${test_source_list})
target_link_libraries(
${PROJECT_NAME}_tests
GTest::gtest_main
Utils
)

include(GoogleTest)
Expand Down
56 changes: 55 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
int main() { return 0; }
#include <cmath>
#include <iostream>
#include <vector>

#include "solution.h"

int main() {
std::vector<node> nodes;
std::vector<int> ans;

int N = 5;
nodes.resize(N);
// список ребер
/*nodes[1 2);*/
nodes[0].neighbours.push_back(1);
nodes[0].neighbours.push_back(4);
nodes[1].neighbours.push_back(2);
nodes[3].neighbours.push_back(0);
nodes[3].neighbours.push_back(4);
nodes[4].neighbours.push_back(1);
nodes[4].neighbours.push_back(2);
for (int i = 0; i < N; i++) {
nodes[i].id = i;
}

for (int i = 0; i < ans.size(); i++) {
std::cout << ans[i] << "\n";
}

bool flag = true;
ans = solution(nodes);

/*std::vector<node> nodes;*/
/*std::vector<int> ans;*/
/**/
/*int n, k, tmp, N;*/
/*std::cin >> N >> tmp; // колич*/
/*nodes.resize(N);*/
/*// список ребер*/
/*while ((tmp--)>0) {*/
/* std::cin >> n >> k;*/
/* nodes[n].neighbours.push_back(k);*/
/*}*/
/*for(int i =0; i<N;i++){*/
/* nodes[i].id = i;*/
/*}*/
/**/
/*ans = solution(nodes);*/

for (int i = ans.size() - 1; i >= 0; i--) {
std::cout << ans[i] << " ";
}

return 0;
}
47 changes: 47 additions & 0 deletions task_01/src/solution.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

кодстайл немного не тот, и грязноватый код, не только в этом файле

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "solution.h"

std::vector<int> solution(std::vector<node> nodes) {
std::deque<int> gray;
std::vector<int> ans;
int N = nodes.size(), tmp = 0;
for (int i = 0; i < N; i++) {
nodes[i].id = i;
std::sort(nodes[i].neighbours.begin(), nodes[i].neighbours.end());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

из-за этого у нас будет сложность большая, в самом худшем поличиться nnlog(n)

}

/*gray.push_back(0);*/
node *current;

tmp = 0;
int i = 0;
while (true) {
if (i >= N) {
break;
}
if (nodes[i].flag == false) {
i++;
continue;
}
gray.push_back(i);
current = &nodes[i];

while (gray.size()) {
current = &nodes[gray.back()];
if (!current->flag) {
gray.pop_back();
continue;
}
if (current->neighbours.size() <= current->last) {
ans.push_back(current->id);
current->flag = false;
gray.pop_back();
continue;
}
tmp = current->neighbours[current->last];
gray.push_back(tmp);
current->last++;
}
i++;
}
return ans;
}
14 changes: 14 additions & 0 deletions task_01/src/solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#include <algorithm>
#include <deque>
#include <vector>

class node {
public:
std::vector<int> neighbours;
int last = 0;
int id;
bool flag = true; // белая
};

std::vector<int> solution(std::vector<node> nodes);
90 changes: 88 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,91 @@
#include <gtest/gtest.h>

#include <vector>

#include "solution.h"

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
std::vector<node> nodes;
std::vector<int> ans;

int N = 3;
nodes.resize(N);
// список ребер
nodes[1].neighbours.push_back(2);
nodes[0].neighbours.push_back(2);
nodes[0].neighbours.push_back(1);
for (int i = 0; i < N; i++) {
nodes[i].id = i;
}

bool flag = true;
ans = solution(nodes);
std::vector<int> assertion = {0, 1, 2};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ASSERT_EQ лучше использовать

ASSERT_TRUE(assertion.size() == ans.size());
for (int i = 0; i < ans.size(); i++) {
flag = flag && (ans[N - i - 1] == assertion[i]);
}
ASSERT_TRUE(flag) << ans[0] << " УЭЭЭЭЭЭ";
}

TEST(Test, Simple2) {
std::vector<node> nodes;
std::vector<int> ans;

int N = 5;
nodes.resize(N);
// список ребер
/*nodes[1 2);*/
nodes[0].neighbours.push_back(1);
nodes[0].neighbours.push_back(4);
nodes[1].neighbours.push_back(2);
nodes[3].neighbours.push_back(0);
nodes[3].neighbours.push_back(4);
nodes[4].neighbours.push_back(1);
nodes[4].neighbours.push_back(2);
for (int i = 0; i < N; i++) {
nodes[i].id = i;
}

for (int i = 0; i < ans.size(); i++) {
std::cout << ans[i] << "\n";
}

bool flag = true;
ans = solution(nodes);
std::vector<int> assertion = {3, 0, 4, 1, 2};
ASSERT_TRUE(assertion.size() == ans.size());
for (int i = 0; i < ans.size(); i++) {
flag = flag && (ans[N - i - 1] == assertion[i]);
}
ASSERT_TRUE(flag) << ans[0] << " УЭЭЭЭЭЭ";
}

TEST(Test, Simpl3) {
std::vector<node> nodes;
std::vector<int> ans;

int N = 6;
nodes.resize(N);
// список ребер
// 0 -> 1
// V Δ
// 3 -> 4
// 5 -> 2
nodes[0].neighbours.push_back(1);
nodes[5].neighbours.push_back(2);
nodes[3].neighbours.push_back(4);
nodes[0].neighbours.push_back(3);
nodes[4].neighbours.push_back(1);
for (int i = 0; i < N; i++) {
nodes[i].id = i;
}
bool flag = true;
ans = solution(nodes);
std::vector<int> assertion = {5, 2, 0, 3, 4, 1};
ASSERT_TRUE(assertion.size() == ans.size());
for (int i = 0; i < ans.size(); i++) {
flag = flag && (ans[N - i - 1] == assertion[i]);
}
ASSERT_TRUE(flag) << ans[2] << " УЭЭЭЭЭЭ";
}
2 changes: 2 additions & 0 deletions task_02/src/.clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CompileFlags:
Add: [-std=c++23]
Binary file added task_02/src/a.out
Binary file not shown.
62 changes: 61 additions & 1 deletion task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
#include <cmath>
#include <iostream>
#include <vector>

int main() { return 0; }
#include "solution.h"

/*
______________________
/ . \
| ├── main.cpp |
| ├── solution.cpp |
| ├── solution.h |
| ├── stack.cpp |
| ├── stack.hpp |
| └── test.cpp |
| |
\ 1 directory, 6 files /
----------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
*/
int main() {
std::vector<node> nodes;

int n, k = 0, tmp, N;
N = 4;
tmp = 4; // колич
nodes.resize(N);
// список ребер
std::vector<std::pair<int, int>> loh = {{0, 1}, {1, 2}, {2, 3}, {0, 2}};
while (k < tmp) {
nodes[loh[k].first].neighbours.push_back(loh[k].second);
nodes[loh[k].second].neighbours.push_back(loh[k].first);
k++;
}
/*std::vector<node> nodes;*/
/**/
/*int n, k, tmp, N;*/
/*std::cin >> N >> tmp; // колич*/
/*nodes.resize(N);*/
/*// список ребер*/
/*while ((tmp--)>0) {*/
/* std::cin >> n >> k;*/
/* nodes[n].neighbours.push_back(k);*/
/* nodes[k].neighbours.push_back(n);*/
/*}*/

auto ans = solution(nodes);

std::cout << "\n\n" << ans.first.size() << "\n";
for (int i = 0; i < ans.first.size(); i++) {
std::cout << ans.first[i].first << " " << ans.first[i].second << "\n";
}
std::cout << "~~~~~~~~~~~~~~\n";
for (int i = 0; i < ans.second.size(); i++) {
std::cout << ans.second[i] << " ";
}

return 0;
}
69 changes: 69 additions & 0 deletions task_02/src/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "solution.h"

#include <utility>
#include <vector>

static std::vector<int> h, d;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

статические переменные не самая хорошая идея(

static std::vector<bool> used;
static std::vector<int> lockers;
static std::vector<std::pair<int, int>> bridges;

void dfs_lockers(std::vector<node> &nodes, int v = 0, int p = -1) {
used[v] = 1;
d[v] = h[v] = (p == -1 ? 0 : h[p] + 1);
int children = 0;
for (auto u : nodes[v].neighbours) {
if (u != p) {
if (used[u])
d[v] = std::min(d[v], h[u]);
else {
dfs_lockers(nodes, u, v);
d[v] = std::min(d[v], d[u]);
if (h[v] <= d[u]) { // корень (p == -1) обрабатываем отдельно
if (h[v] != d[u]) bridges.push_back(std::pair<int, int>(v, u));
if (p != -1) lockers.push_back(v);
}
children++;
}
}
}
if (p == -1 && children > 1) {
lockers.push_back(v);
}
}
void dfs_bridges(std::vector<node> &nodes, int v = 0, int p = -1) {
used[v] = true;
d[v] = h[v] = (p == -1 ? 0 : h[p] + 1);
for (auto u : nodes[v].neighbours) {
if (u != p) {
if (used[u]) // если ребро обратное
d[v] = std::min(d[v], h[u]);
else { // если ребро прямое
dfs_bridges(nodes, u, v);
d[v] = std::min(d[v], d[u]);
if (h[v] < d[u]) {
bridges.push_back(std::pair<int, int>(v, u));
}
}
}
}
}

std::pair<std::vector<std::pair<int, int>>, std::vector<int>> solution(
std::vector<node> nodes) {
used.resize(nodes.size());
h.resize(nodes.size());
d.resize(nodes.size());
lockers.clear();
bridges.clear();
for (int i = 0; i < nodes.size(); i++) {
used[i] = 0;
h[i] = d[i] = 0;
}

std::vector<node> nc = nodes;
dfs_lockers(nodes);

return std::pair<std::vector<std::pair<int, int>>, std::vector<int>>(bridges,
lockers);
}
Loading
Loading