BOJ 5021
λ¬Έμ
μμ κ³μΉ μ±κ³΅λ€κ΅μ΄
1 μ΄ 128 MB 1005 340 255 34.884% λ¬Έμ
μ ν νΌμμ μμ΄ μ¬λ§νλ€. μμ μμμ λ¨κΈ°μ§ μκ³ μ¬λ§νκΈ° λλ¬Έμ, μμλ₯Ό κ³μΉν μ¬λμ μ§λͺ©νμ§ μμλ€. μμ€ κ·μ‘±μ μμλ₯Ό μ£Όμ₯νκΈ° μμνλ€. μ ν νΌμμ λ²μλ μμ κ³μΉμκ° μλ κ²½μ°μ, λλΌλ₯Ό μΈμ΄ μ¬λκ³Ό νν΅μ΄ κ°κΉμ΄ μ¬λμ΄ μ ν νΌμλ₯Ό ν΅μΉνλ€λ μ‘°νμ΄ μλ€.
λλΌλ₯Ό μΈμ΄ μ¬λκ³Ό νν΅μ΄ κ°μ₯ κ°κΉμ΄ μ¬λμ κ·Έμ μμμ΄ μλ μ¬λκ³Ό νΌκ° λ μμΈ μ¬λμ΄λ€. λͺ¨λ μ¬λμ μλ²μ§μ νν΅κ³Ό μ΄λ¨Έλμ νν΅μ λ° μ© λ°κ² λλ€. λλΌλ₯Ό μΈμ΄ μ¬λμ μμμ 1/2 μμ‘±μ΄λ©°, κ·Έ μλ€μ΄ μμ‘±μ΄ μλ μ¬λκ³Ό κ²°νΌν κ²½μ°μλ μλ€μ μμμ 1/4 μμ‘±μ΄ λλ€.
κ°μ₯ λλΌλ₯Ό μΈμ΄ μ¬λκ³Ό νν΅μ΄ κ°κΉμ΄ μ¬λμ μ°Ύλ νλ‘κ·Έλ¨μ μμ±νμμ€.
μ λ ₯
첫째 μ€μ Nκ³Ό Mμ΄ μ£Όμ΄μ§λ€. (2 ≤ N, M ≤ 50)
λμ§Έ μ€μ μ ν νΌμλ₯Ό μΈμ΄ μ¬λμ μ΄λ¦μ΄ μ£Όμ΄μ§λ€.
λ€μ Nκ° μ€μλ κ°μ‘± μ λ³΄κ° μ£Όμ΄μ§λ€. μ 보λ μ΄λ¦ μΈ κ°λ‘ μ΄λ£¨μ΄μ Έ μκ³ , 곡백μΌλ‘ ꡬλΆλμ΄μ Έ μλ€. 첫 λ²μ§Έ μ΄λ¦μ μμμ μ΄λ¦μ΄κ³ , λ€μ λ μ΄λ¦μ λΆλͺ¨μ μ΄λ¦μ΄λ€.
λ€μ Mκ° μ€μλ μμλ₯Ό κ³μΉλ°κΈ°λ₯Ό μ£Όμ₯νλ μ¬λμ μ΄λ¦μ΄ ν μ€μ νλμ© μ£Όμ΄μ§λ€.
λͺ¨λ μ΄λ¦μ 1~10κΈμμ΄λ©°, μνλ²³ μλ¬Έμλ‘λ§ μ΄λ£¨μ΄μ Έ μλ€. λλΌλ₯Ό μΈμ΄ μ¬λμ΄ μμλ₯Ό κ³μΉνλ κ²½μ°λ, λκ΅°κ°μ μμμΈ κ²½μ°λ μλ€.
μΆλ ₯
첫째 μ€μ μ ν νΌμλ₯Ό μΈμ΄ μ¬λκ³Ό κ°μ₯ νν΅μ΄ κ°κΉμ΄ μ¬λμ μ΄λ¦μ μΆλ ₯νλ€. νμ λ΅μ΄ μ μΌν κ²½μ°λ§ μ λ ₯μΌλ‘ μ£Όμ΄μ§λ€.
λ¬Έμ μ μ£Όμ΄μ§λ κ°μ‘± κ΄κ³λ μ±λ³, λμ΄λ₯Ό κ³ λ €νμ§ μκ³ λ§λ€μκΈ° λλ¬Έμ, μ€μ λ‘λ λ§μ΄ μλλ κ²½μ°κ° λμ¬ μλ μλ€. νμ§λ§, λͺ¨λ μμμ λΆλͺ¨λ μ μΌνλ©°, λ€μ μμμ΄ λΆλͺ¨μ λΆλͺ¨κ° λλ κ²½μ°λ μλ€. λ, ν μ¬λμ΄ μ¬λ¬ λͺ μ μμμ΄ λ μ λ μλ€.
μμ μ λ ₯ 1 볡μ¬
9 2 edwardi charlesi edwardi diana philip charlesi mistress wilhelm mary philip matthew wilhelm helen edwardii charlesi laura alice laura charlesi helen alice bernard henrii edwardii roxane charlesii elizabeth henrii charlesii matthew
μμ μΆλ ₯ 1 볡μ¬
matthew
μμ μ λ ₯ 2 볡μ¬
4 5 andrew betsy andrew flora carol andrew betsy dora andrew carol elena andrew dora carol dora elena flora gloria
μμ μΆλ ₯ 2 볡μ¬
elenaβ
νμ΄
νμ΄ μ체λ κ°λ¨νλ€.
λ¬Έμ μμ λͺ μλμ΄ μμ§λ μμ§λ§ λΆλͺ¨ λμ μλ²μ§, μ΄λ¨Έλλ‘ λκ³
(μλ²μ§μ νν΅κ° + μ΄λ¨Έλμ νν΅κ°) * 1/2 λ₯Ό ν κ°μ΄ μλ μ νν΅μ κ°μ΄ λλ€.
μ΄λ₯Ό λͺ¨λ μλ μ λν΄ κ³μ°ν΄μ£Όλ©΄ λλ λ¬Έμ μ΄λ€.
μλ μ λΆλͺ¨μ λν μ λ³΄κ° νλ²μ μ λ ₯λλ―λ‘ κ΅¬μ‘°μ²΄λ‘ μ μΈν΄μ€λ€.
struct p{
string name, parent1, parent2;
};
κ·Έλ¦¬κ³ κ°κ°μ λ Έλ(μλ , λΆλͺ¨ κ°κ°μ μ΄λ¦) μ νν΅μ μ΄κΈ°νν΄μ£ΌκΈ° μν΄ key-value tableμ μμ±ν΄μ€λ€.
λΆλͺ¨μ μλ μ μ΄λ¦μ΄ μ€λ³΅λ μ μκΈ° λλ¬Έμ, μ€λ³΅μΌ κ²½μ°μ insertλ₯Ό μ ννλ map stlμ μ¬μ©νμλ€.
map<string,double>m;
μ€λ¦½μμ νν΅μ 1λ‘ μ΄κΈ°ν ν΄μ£Όκ³ , λλ¨Έμ§λ λ€ 0μΌλ‘ μ΄κΈ°ν ν΄μ€λ€.
cin>>founder;
m.insert({founder, 1});
for(int i=0; i<N; i++){
p new_p;
cin>>new_p.name>>new_p.parent1>>new_p.parent2;
v.push_back(new_p);
m.insert({new_p.name, 0});
m.insert({new_p.parent1, 0});
m.insert({new_p.parent2, 0});
}
νν΅μ΄ 0μ΄ μλ κ²½μ°λ 무쑰건 μ€λ¦½μμ νμ°κ΄κ³κ° μμ΄μΌ νλ―λ‘ queueλ₯Ό νλ λ§λ€μ΄ μ€λ¦½μλ₯Ό λ£μ΄μ£Όκ³ ,
λΆλͺ¨κ° μ€λ¦½μμΈ κ²½μ°λΆν° νμν΄ λκ°λλ‘ νλ€.
λλͺ μ λΆλͺ¨ μ€ νλλΌλ μ€λ¦½μμ΄λ©΄ νμ°κ΄κ³λ₯Ό κ³μ°νλ λ°©μμΌλ‘ μ΄μ΄μ§λ κ²μ΄λ€.
λ€λ§ μ€λ¦½μμ μλ κ° μ€λ¦½μμ λ°°μ°μκ° λλ―μ΄ μ€λ¦½μμ νμ°κ΄κ³κ° μλ μ¬λμ΄ μ€λ¦½μμ νμ°κ΄κ³κ° μλ μ¬λμ λ°°μ°μκ° λ μ μμΌλ―λ‘ μ€λ¦½μμ μλ , μ€λ¦½μμ μλ μ μλ ~~~ λ₯Ό queueμ κ³μ λ£μ΄μ£Όλ©΄μ κ³μ°μ΄ λ°λλ κ²½μ°λ λͺ¨λ νμν΄ μ£Όλλ‘ νλ°.
queue<string>q;
q.push(founder);
while(!q.empty()){
string cur = q.front();
q.pop();
for(auto node : v){
if(node.parent1 == cur || node.parent2 == cur){
string p1 = node.parent1, p2 = node.parent2;
double p1_rate = m.find(p1)->second, p2_rate = m.find(p2)->second;
m.find(node.name)->second = (p1_rate+p2_rate)/2;
q.push(node.name);
}
}
}
μ΄λ¬λ©΄ λͺ¨λ μ¬λμ νμ°κ΄κ³κ° ꡬν΄μ§κ² λκ³ , λ¬Έμ κ° νλ¦°λ€.
λ€λ₯Έ νμ΄
λ€ νκ³ λ€λ₯Έ νμ΄λ₯Ό μ°Ύμ보μμλ
ν보μλ₯Ό μ λ ₯νλ κ²½μ°μ DFSλ‘ νμνλ κ²μ΄ λ μ½λκ° λͺ λ£νλ€λ κ²μ μκ² λμλ€.
λΆλͺ¨μ νμ°κ΄κ³λ₯Ό λΆλͺ¨μ λΆλͺ¨κ° μμλκΉμ§ κ³μ°ν΄ μλ λ₯Ό κ³μ°νλ©΄ λλ€.
double dfs(string s){
for(auto node : v){
if(node.name == s){
string p1 = node.parent1, p2 = node.parent2;
double rate = (dfs(p1)+dfs(p2))/2;
m[s] = rate;
return rate;
}
}
return m[s];
}
μ΄ μ½λμμλ ꡬ쑰체 λ²‘ν° vμ μμμ nameμ΄ μμΌλ©΄ λΆλͺ¨κ° μλ κ²μ΄κΈ° λλ¬Έμ, κ·Έλ mapμ μμλ₯Ό λ°ννλλ‘ νλ€.
μ 체 μ½λ
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;
struct p{
string name, parent1, parent2;
};
map<string,double>m;
vector<p>v;
int N, M;
double ans;
string founder, ans_s;
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>N>>M;
cin>>founder;
m.insert({founder, 1});
for(int i=0; i<N; i++){
p new_p;
cin>>new_p.name>>new_p.parent1>>new_p.parent2;
v.push_back(new_p);
m.insert({new_p.name, 0});
m.insert({new_p.parent1, 0});
m.insert({new_p.parent2, 0});
}
queue<string>q;
q.push(founder);
while(!q.empty()){
string cur = q.front();
q.pop();
for(auto node : v){
if(node.parent1 == cur || node.parent2 == cur){
string p1 = node.parent1, p2 = node.parent2;
double p1_rate = m.find(p1)->second, p2_rate = m.find(p2)->second;
m.find(node.name)->second = (p1_rate+p2_rate)/2;
q.push(node.name);
}
}
}
for(int i=0; i<M; i++){
string s1;
cin>>s1;
for(auto iter = m.begin(); iter != m.end(); iter++){
if(iter->first == s1){
if(iter->second > ans){
ans = iter->second;
ans_s = iter->first;
}
}
}
}
cout<<ans_s;
}
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;
struct p{
string name, parent1, parent2;
};
map<string,double>m;
vector<p>v;
int N, M;
double ans;
string founder, ans_s;
double dfs(string s){
for(auto node : v){
if(node.name == s){
string p1 = node.parent1, p2 = node.parent2;
double rate = (dfs(p1)+dfs(p2))/2;
m[s] = rate;
return rate;
}
}
return m[s];
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>N>>M;
cin>>founder;
m.insert({founder, 1});
for(int i=0; i<N; i++){
p new_p;
cin>>new_p.name>>new_p.parent1>>new_p.parent2;
v.push_back(new_p);
m.insert({new_p.name, 0});
m.insert({new_p.parent1, 0});
m.insert({new_p.parent2, 0});
}
for(int i=0; i<M; i++){
string s1;
cin>>s1;
double r=dfs(s1);
if(r > ans){
ans_s = s1;
ans = r;
}
}
cout<<ans_s;
}
ν¬μΈνΈ
νμ°κ΄κ³μ κ΄λ ¨λ λͺ¨λ λ³μλ doubleλ‘ μμ±ν΄μ£Όμ΄μΌ νλ€.
ansλ³μλ₯Ό intλ‘ μμ±ν΄μ λλ² νλ Έλ€.
map stlμ findν¨μκ° keyλ₯Ό μ°Ύμ§ λͺ»νμ κ²½μ°, map.end()λ₯Ό λ°ννλ€λ κ²μ μκΈ°νμ.
map.end()λ λ§μ§λ§ μμλ₯Ό μ°Έμ‘°νλ κ²μ΄ μλ λ°λ³΅μμ λ§μ§λ§μ κ°λ¦¬ν€λ κ²μ΄λ€.