11437 LCA
์๊ณ ๋ฆฌ์ฆ (BFS, LCA)
1. ํธ๋ฆฌ๋ง๋ค๊ธฐ
1. check ๋ฐฐ์ด์ ๋ง๋ค๊ณ , 1๋ฒ ๋
ธ๋๋ง true ํ์ํจ
2. ์ ์ ์์ด ๋ค์ด์์ ๋, ๋์ค์ check ํ์๋ ๊ฒ์ด A, ์๋ ๊ฒ์ด B๋ผ๊ณ ํ ๋, B์ ๋ถ๋ชจ์ A๋ฅผ, ๊น์ด์ A+1์ ๋ฃ๊ณ , check[B] ์ true ํ์ํจ
3. true ํ์๊ฐ ๋๋๊ณ , B์ ์ธ์ ํ ์ ์ ๋ค์ BFS๋ก ๋๊น์ง ๋ฐฉ๋ฌธํ๋ฉด์ ๊น์ด๋ฅผ ๋ถ์ฌํ๊ณ check ํ์ํจ.
4. ๋ง์ฝ ๋ ๋ค check์ ํ์๊ฐ ์๋ผ์์ผ๋ฉด, ๋ ์ ์ ์ ์ธ์ ํ ์ ์ ๋ฒกํฐ์ ๋๋ค ์ถ๊ฐํ๋ค.
2. LCA ์ฐพ๊ธฐ
1. log2์ ๋ถ๋ชจ๋ฅผ ์ฐพ๋ ๋ฐฉ๋ฒ์ผ๋ก ํ๋ฉด ์์ฃผ ๋น ๋ฅด๊ฒ ๊ฐ๋ฅ. ํ์ง๋ง, ์ด ๋ฐฉ๋ฒ์ ์ด๋ ต๋ค
2. ๊ฐ๋จํ๊ฒ ๋ ์ ์ ์ค ๊น์ด๊ฐ ๋ ๊น์ ๊ฒ์ ๋ถ๋ชจ๋ฅผ ์ฐพ์๊ฐ๋ฉด์ ๋ ๊น์ ๊ฒ๊ณผ ๊น์ด๋ฅผ ๋๊ฐ์ด ๋ง์ถค
3. ๊น์ด๊ฐ ๊ฐ์์ง๋ฉด, ์ ์ ์ด ๊ฐ์์ง๋๊น์ง ๋๋ค ๋ถ๋ชจ๋ฅผ ์ฐพ์๊ฐ
-> 1์ด๋.
์ฝ๋
#include <iostream>
#include <vector>
#include <algorithm>
#define MAX_NODE 50001
using namespace std;
typedef struct Node {
int depth;
int parent;
vector<int> closeNode;
} Node;
Node node[MAX_NODE];
int numOfNode, list[MAX_NODE];
bool check[MAX_NODE];
int findLCS(int first, int second);
void makeTree(int root);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> numOfNode;
check[1] = true;
int temp1, temp2, numOfPair;
for (int i = 0; i < numOfNode-1; i++) {
cin >> temp1 >> temp2;
if (check[temp1]) {
node[temp2].depth = node[temp1].depth + 1;
node[temp2].parent = temp1;
check[temp2] = true;
if(node[temp2].closeNode.size() > 0)
makeTree(temp2);
}
else if (check[temp2]) {
node[temp1].depth = node[temp2].depth + 1;
node[temp1].parent = temp2;
check[temp1] = true;
if (node[temp1].closeNode.size() > 0)
makeTree(temp1);
}
else {
node[temp1].closeNode.push_back(temp2);
node[temp2].closeNode.push_back(temp1);
}
}
cin >> numOfPair;
for (int i = 0; i < numOfPair; i++) {
cin >> temp1 >> temp2;
int curResult = node[temp1].depth < node[temp2].depth ? findLCS(temp1, temp2) : findLCS(temp2, temp1);
cout << curResult << "\n";
}
}
void makeTree(int root) {
int index = 0, length = 0;
list[length++] = root;
while (index < length) {
int cur = list[index++];
int depth = node[cur].depth + 1;
for (int i = 0; i < node[cur].closeNode.size(); i++) {
int t = node[cur].closeNode[i];
if (!check[t]) {
node[t].depth = depth;
node[t].parent = cur;
check[t] = true;
list[length++] = t;
}
}
node[cur].closeNode.clear();
}
return;
}
int findLCS(int first, int second) {
while (node[first].depth != node[second].depth)
second = node[second].parent;
while (first != second) {
first = node[first].parent;
second = node[second].parent;
}
return first;
}