Skip to content

Commit

Permalink
Merge pull request #83 from AlgoLeadMe/24-InSange
Browse files Browse the repository at this point in the history
24-InSange
  • Loading branch information
InSange authored Aug 9, 2024
2 parents 9541186 + 843a861 commit 39b1ca8
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
106 changes: 106 additions & 0 deletions InSange/BFS/1068.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Node
{
Node()
{
parent = nullptr;
leafNode = true;
}

vector<int> childs;
Node* parent;
bool leafNode;
};

vector<Node*> nodes;
int N, ans = 0, rIndex;

void NodeUpdate(Node* curNode)
{
for (auto child : curNode->childs)
{
if (nodes[child] != nullptr)
{
curNode->leafNode = false;
return;
}
}
curNode->leafNode = true;
}

void Solve()
{
cin >> N;

for (int i = 0; i < N; i++)
{
Node* node = new Node();

nodes.push_back(node);
}
int root_index = 0;

for(int i = 0; i < N; i++)
{
int p;
cin >> p;

if (p == -1)
{
root_index = i;
continue;
}
nodes[i]->parent = nodes[p];
nodes[i]->parent->childs.push_back(i);
if (nodes[i]->parent)
{
NodeUpdate(nodes[i]->parent);
}
}

cin >> rIndex;
if (rIndex == root_index) // Á¦°Å µÈ°Ô ·çÆ®¶ó¸é
{
cout << ans << "\n";
return;
}
Node* parentNode = nodes[rIndex]->parent;
delete nodes[rIndex];
nodes[rIndex] = nullptr;
NodeUpdate(parentNode);

queue<Node*> q;
q.push(nodes[root_index]);

while (!q.empty())
{
Node* cur = q.front();
q.pop();

if (cur->leafNode) ans++;
for (auto index : cur->childs)
{
if (nodes[index] != nullptr)
{
q.push(nodes[index]);
}
}
}

cout << ans;
}

int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);

Solve();

return 0;
}
1 change: 1 addition & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
| 21차시 | 2024.07.21 | 그리디 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/75)]
| 22차시 | 2024.07.31 | DP | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [#22](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/81)]
| 23차시 | 2024.08.03 | 슬라이딩 윈도우 | [Minimum Swaps to Group All 1's Together 2](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) | [#23](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/82)]
| 24차시 | 2024.08.04 | BFS | [트리](https://www.acmicpc.net/problem/1068) | [#24](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/83)]
---

https://leetcode.com/problems/robot-collisions/

0 comments on commit 39b1ca8

Please sign in to comment.