Skip to content

220724/이현동 #119

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

Merged
merged 3 commits into from
Jul 24, 2022
Merged
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
71 changes: 71 additions & 0 deletions [Week14 - LCA]/이현동/11437.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <bits/stdc++.h>
using namespace std;

int N, M;
int a, b;
queue<int> q;
vector<int> node[50001];
bool check[50001];
int parent[50001];
int depth[50001];

int LCA(int u, int v)
{
// v가 u보다 더 깊게 설정
if (depth[u] > depth[v]) swap(u, v);
while (depth[u] != depth[v]) v = parent[v];

// 두 노드가 같아질때 까지 위로 거슬러 올라감
while (u != v)
{
u = parent[u];
v = parent[v];
}
return u;
}


int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);

cin >> N;

for (int i = 0; i < N-1; i++)
{
cin >> a >> b;
node[a].push_back(b);
node[b].push_back(a);
}

//root node
check[1] = true;
q.push(1);

while (!q.empty())
{
int x = q.front();
q.pop();

for (int i = 0; i < node[x].size(); i++)
{
if (!check[node[x][i]])
{
depth[node[x][i]] = depth[x] + 1; // 깊이 +1
check[node[x][i]] = true; // 방문표시
parent[node[x][i]] = x; // 부모노드 입력
q.push(node[x][i]); // 큐에 추가
}
}
}

cin >> M;

for (int i = 0; i < M; i++)
{
cin >> a >> b;
cout << LCA(a, b) << '\n';
}

}
Binary file added [Week14 - LCA]/이현동/11812
Binary file not shown.
38 changes: 38 additions & 0 deletions [Week14 - LCA]/이현동/11812.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll N;
int K, Q;
ll u, v;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> K >> Q;

for (int i = 0; i < Q;i++){
cin >> u >> v;
ll depth = 0;
if(K == 1)
depth = abs(u - v);
else{
while(u != v){
// cout << "u, v :" << u << ' ' << v << '\n';
if (u > v){
u = (u - 2) / K + 1;
}else{
v = (v - 2) / K + 1;
}
++depth;
}
}
// cout << u << ' ' << v << '\n';
cout << depth << '\n';
// cout << "----------\n";
}

return 0;
}
Empty file removed [Week14 - LCA]/이현동/temp
Empty file.