P2814-家谱【图论,并查集,std map库】
生活随笔
收集整理的這篇文章主要介紹了
P2814-家谱【图论,并查集,std map库】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
題目鏈接:
https://www.luogu.org/problemnew/show/P2814
大意
有n個父子關系(可能不止一個孩子),詢問一些人最大的祖先
代碼
#include<cstdio> #include<map> #include<string> #include<iostream> using namespace std; map<string,string> father;//map庫 string name,fathers; char c; string find(string x) {if (x!=father[x]) return father[x]=find(father[x]);return father[x]; } void unionn(string x,string y) {string fa=find(x),fb=find(y);father[fa]=fb; } int main() {while (true){cin>>c;if (c=='$') break;cin>>name;if (father[name]=="") father[name]=name;if (c=='#'){fathers=name;//記錄父親}if (c=='+'){unionn(name,fathers);//連接}if (c=='?'){cout<<name<<' '<<find(name)<<endl;//詢問}} }總結
以上是生活随笔為你收集整理的P2814-家谱【图论,并查集,std map库】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P1455-搭配购买【图论,并查集,dp
- 下一篇: 【2018.3.24】模拟赛之一-ssl