洛谷P2814题解

Posted 李发?

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷P2814题解相关的知识,希望对你有一定的参考价值。

标准并查集题目,开map做数组。

每次遇到’&‘,’#‘,’+‘ ,’?\'就做不同的处理。

为了好做,就用了函数(为了递归),

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define ctx cout << "xxxxx" << endl
#define inf 0x3f3f3f3f
const int INF = 0x3f3f3f3f;
const double pai = 3.141592653589;
const int maxn = 1e5 + 10;
int b[maxn];

map<string, string> mp;
inline void init(string a){
mp[a] = a;
}
string find(string x)
{
if(x == mp[x]){
return x;
}else{
return find(mp[x]);
}
}
/*
inline void merge(string a, string b){

}
*/

void solve(string a){
if(a == "&") return ;
string tem2 = a.substr(1,6);
if(!mp.count(tem2)){
mp[tem2] = tem2;
}
string b;
cin >> b;
char s = b[0];
while(s == \'+\'){
string tem1 = b.substr(1,6);
mp[tem1] = tem2;
cin >> b;
s = b[0];
}
while(s == \'?\'){
string tem3 = b.substr(1,6);
cout << tem3 << " " << find(tem3) << endl;
cin >> b;
s = b[0];
}
if(b[0] == \'#\')solve(b);
return ;
}


int main(){
while(1){
string a;
cin >> a;
solve(a);
break;
}

return 0;
}

以上是关于洛谷P2814题解的主要内容,如果未能解决你的问题,请参考以下文章

洛谷P2814 家谱

家谱(gen)——洛谷P2814

洛谷P2814 家谱

洛谷 P2814 家谱

洛谷题目怎么看答案

题解目录