Skip to content
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

[ 트리 ] 11월 21일 #11

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
69 changes: 69 additions & 0 deletions 12_트리/1390.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <queue>

using namespace std;

int main(){

int n, x_in, y_in , k, l, cnt=0, dir=0, x=0, y=0;
char d;

int board[101][101];
bool visited[101][101];

Choose a reason for hiding this comment

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

P3. visited 배열을 이용해서 뱀의 위치를 나타내셨네요! 이것도 좋은 풀이지만 위에 boardint 배열로 정의했으니 한 가지 숫자를 더 사용해서 뱀이 있는 칸을 나타내면 어떨까요? 이렇게 하면 메모리 공간을 조금이나마 절약할 수 있어 보입니다!

queue<pair<int, int>> snake;
int dx[4]={1, 0, -1, 0};
int dy[4]={0, 1, 0, -1};

snake.push(make_pair(x, y));

cin >> n;
cin >> k;

for(int i=0;i<k;i++){
cin >> x_in >> y_in;
board[x_in-1][y_in-1]=1;
}

cin>>l;

for(int i=0;i<l;i++){

int num;

cin >> num >> d;

while(cnt<num||i==l-1){
Comment on lines +29 to +35

Choose a reason for hiding this comment

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

명령어들을 따로 저장해두지 않고 입력 받을 때마다 게임을 진행하셨네요! 저는 생각하지 못한 방식인데 이런 풀이도 정말 좋은 것 같습니다 👍

int nx=x+dx[dir];
int ny=y+dy[dir];
cnt++;

if(nx<0||ny<0||nx>=n||ny>=n||visited[ny][nx]){
cout << cnt;
return 0;
}
if(board[ny][nx]==1){
board[ny][nx]=0;
snake.push(make_pair(nx, ny));
visited[ny][nx]=true;
}
else{
snake.push(make_pair(nx,ny));
visited[ny][nx]=true;
visited[snake.front().second][snake.front().first]=false;
snake.pop();
}
Comment on lines +44 to +54

Choose a reason for hiding this comment

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

P3. if와 else 안의 내용에 겹치는 부분이 있다면 중복 제거를 위해 if문 밖에서 한 번만 수행하는 것이 좋습니다 :)


x=nx;
y=ny;
if(cnt==num){
if(d=='D'){
dir=(dir+1)%4;
}
else if(d=='L'){
dir=(dir+3)%4;
}
}
}
}
return 0;
}
50 changes: 50 additions & 0 deletions 12_트리/15681.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

vector<int> v[100001];
bool visited[100001];
int num_arr[100001];

void dfs(int num, int upperN) {
visited[num]=true;
for(int i=0;i<v[num].size();i++){
int next=v[num][i];
if(visited[next]==false){
dfs(next,num);
}
}
if(upperN!=-1){
num_arr[upperN]+=num_arr[num];
}
}
Comment on lines +11 to +22
Copy link

Choose a reason for hiding this comment

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

문제 조건에 맞게 dfs로 구현해 주셨네요. 함수 로직을 잘 짜주셔서 특별히 코멘트 드릴 점은 없습니다. Indentation만 살짝 정리하면 보기 좋을 것 같습니다.😊


int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

int n, q, r;

cin >> n >> r >> q;
fill_n(num_arr,100001,1);

for(int i=0;i<n-1;i++){
int temp1,temp2;

cin >> temp1 >> temp2;
v[temp1].push_back(temp2);
v[temp2].push_back(temp1);
}

dfs(r, -1);

for(int i=0;i<q;i++){
int temp;
cin >> temp;
cout << num_arr[temp] << '\n';
}
return 0;
}
54 changes: 54 additions & 0 deletions 12_트리/1967.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include<iostream>
#include<vector>
#include<cstring>

using namespace std;

bool visited[100001];
vector<pair<int, int>> board[100001];
int endI;

void dfs(int x, int len, int &answer){

if(visited[x]==true){
return;
}

visited[x]=true;

if(len>answer){
answer=len;
endI=x;
}

for(int i=0;i<board[x].size();i++){
dfs(board[x][i].first,len+board[x][i].second,answer);
}
}
Comment on lines +11 to +27
Copy link

Choose a reason for hiding this comment

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

완벽합니다👍


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

int n, o, p, q, answer=0;

cin >> n;

for(int i=1;i<n;i++){
cin >> o >> p >> q;

board[o].push_back(make_pair(p,q));
board[p].push_back(make_pair(o,q));
}

dfs(1,0,answer);

memset(visited,false,sizeof(visited));

dfs(endI,0,answer);

cout << answer;

Comment on lines +31 to +52
Copy link

Choose a reason for hiding this comment

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

로직을 깔끔하게 잘 짜 주셨습니다. 주석만 몇 개 추가하면 더 좋은 코드가 될 것 같아요.👍👍

return 0;
}
90 changes: 90 additions & 0 deletions 12_트리/24545.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> vec(100001, vector<int>());
int twoV[2]={0,0};
int max_len=-1;
vector<int> board(100001, 0);
bool visited[100001];

void dfs(int nowN, int prevN, int len, int i, int &n){
if(max_len<len){
max_len=len;
twoV[i]=nowN;
}
for(int j=0;j<vec[nowN].size();j++){
int nextN=vec[nowN][j];
if(nextN!=prevN){
dfs(nextN, nowN, len+1, i, n);
}
}
}

bool route(int nowN, int pointN, int prevN, int len){
board[len]=nowN;
if(nowN==pointN){
return true;
}
bool forCheck=false;
for(int j=0;j<vec[nowN].size();j++){
int nextN=vec[nowN][j];
if(nextN==prevN){
continue;
}
forCheck|=route(nextN, pointN, nowN, len+1);
if(forCheck==true){
break;
}
}
return forCheck;
}

int dfs2(int nowN){
int maxInt=0;
for(int j=0;j<vec[nowN].size();j++){
int nextN=vec[nowN][j];
if(visited[nextN]){
continue;
}
visited[nextN]=true;
maxInt=max(maxInt,dfs2(nextN));
}
return maxInt+1;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

int n;

cin >> n;

for(int i=1;i<=n-1;i++){
int temp1, temp2;
cin >> temp1 >> temp2;
vec[temp1].push_back(temp2);
vec[temp2].push_back(temp1);
}
for(int i=0;i<100001;i++){
visited[i]=false;
}
dfs(1, 0, 0, 0, n);
max_len=-1;
dfs(twoV[0], 0, 0, 1, n);
route(twoV[0], twoV[1], 0, 0);

for(int i=0;i<=max_len;i++){
visited[board[i]]=true;
}
int maxInt=1;
for(int i=1;i<=max_len-1;i++){
maxInt=max(maxInt,dfs2(board[i]));
}
cout << (maxInt==1?0:max_len+maxInt);

return 0;
}
42 changes: 42 additions & 0 deletions 12_트리/5639.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <vector>

using namespace std;

vector<int> tree;

void searchTree(int start, int end){
int i;

if(start>=end){
return;
}
if(start==end-1){
cout << tree[start] << '\n';
return;
}

for(i=start+1;i<end;i++){
if(tree[start]<tree[i]){
break;
}
}

searchTree(start+1,i);
searchTree(i,end);
cout << tree[start] << '\n';
}
Comment on lines +8 to +28
Copy link

Choose a reason for hiding this comment

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

하린님만의 방법으로 잘 구현해 주셨습니다. 해설강의에서 언급한 dfs를 이용한 방법도 한 번 고려해 보시면 좋을 것 같습니다. 수고하셨어요.😍


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

int n;
while(cin >> n){
tree.push_back(n);
}

searchTree(0,tree.size());

return 0;
}
1 change: 1 addition & 0 deletions Altu-Bitu-HarinLee
Submodule Altu-Bitu-HarinLee added at f130cd