๋ฌธ์
์์ ๊ณ์น ์ฑ๊ณต๋ค๊ตญ์ด
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()๋ ๋ง์ง๋ง ์์๋ฅผ ์ฐธ์กฐํ๋ ๊ฒ์ด ์๋ ๋ฐ๋ณต์์ ๋ง์ง๋ง์ ๊ฐ๋ฆฌํค๋ ๊ฒ์ด๋ค.