P193 习题11.1
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
const int MAXN = 30;
int father[MAXN];
void initial(){
for (int i=0;i<MAXN;i++){
father[i] = -1;
}
}
int Find(char x,char y){
int counter = 0;
if (father[x-'A'] == y-'A'){return 1;}
while(father[x-'A'] != y-'A'){
if (father[x-'A'] == -1){return 0;}
x = father[x-'A']+'A';
counter++;
}
return counter+1;
}
int main(){
int n,m;
cin>>n;
cin>>m;
string str;
initial();
while(n--){
cin>>str;
if (str[1]!='-'){father[str[1]-'A'] = str[0]-'A';}
if (str[2]!='-'){father[str[2]-'A'] = str[0]-'A';}
}
while(m--){
cin>>str;
int counter = 0;
char x = str[0];
char y = str[1];
int bi = Find(x,y);
if (bi!=0){
if (bi == 1){cout<<"parent"<<endl;}
else {
while(bi>2){cout<<"great-";bi--;}
cout<<"grandparent"<<endl;
}
}
else{
bi = Find(y,x);
if (bi == 0){cout<<'-'<<endl;}
else if (bi == 1){cout<<"child"<<endl;}
else {
while(bi>2){cout<<"great-";bi--;}
cout<<"grandchild"<<endl;
}
}
}
return 0;
}
P193 习题11.2
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
const int MAXN = 1000010;
int father[MAXN];
int height[MAXN];
int visit[MAXN];
void initial(){
for (int i=0;i<MAXN;i++){
father[i] = i;
height[i] = 0;
visit[i] = false;
}
}
int Find(int x){
if (x!=father[x]){
father[x] = Find(father[x]);
}
return father[x];
}
void Union(int x,int y){
x = Find(x);
y = Find(y);
if (x!=y){
if (height[x]<height[y]){
father[x] = y;
}
else if (height[x]>height[y]){
father[y] = x;
}
else{
father[y] = x;
height[x]++;
}
}
}
void judge(){
int component = 0;
for (int i=0;i<MAXN;i++){
if (!visit[i]){continue;}
if (father[i] == i){component++;}
}
cout<<component;
}
int main(){
int x,y;
initial();
while(cin>>x>>y){
visit[x] = true;
visit[y] = true;
Union(x,y);
}
judge();
return 0;
}
P193 习题11.3
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 30;
int father[MAXN];
int height[MAXN];
int visit[MAXN];
int weight[MAXN];
int member[MAXN];
int timea[MAXN];
map <char,int> mp;
map <char,string> store;
vector <int> vec;
struct r{
string head;
int member;
};
vector <r> re;
void initial(){
for (int i=0;i<MAXN;i++){
father[i] = i;
height[i] = 0;
visit[i] = false;
weight[i] = 0;
member[i] = 0;
timea[i] = 0;
}
}
int Find(int x){
if (x!=father[x]){
father[x] = Find(father[x]);
}
return father[x];
}
void Union(string a,string b){
int x = a[0]-'A';
int y = b[0]-'A';
x = Find(x);
y = Find(y);
if (x!=y){
if (height[x]<height[y]){
father[x] = y;
}
else if (height[x]>height[y]){
father[y] = x;
}
else{
father[y] = x;
height[x]++;
}
}
}
bool compare(r r1,r r2){
return r1.head<r2.head;
}
void judge(){
for (int i=0;i<MAXN;i++){
if (!visit[i]){continue;}
if (father[i] == i){vec.push_back(i);}
weight[father[i]] += mp[i+'A'];
member[father[i]]++;
}
}
int main(){
freopen("D://case.txt","r",stdin);
int n,k;
initial();
cin>>n>>k;
string name1;
string name2;
int timee;
while(n--){
cin>>name1>>name2>>timee;
mp[name1[0]] += timee;
Union(name1,name2);
visit[name1[0]-'A'] = true;
visit[name2[0]-'A'] = true;
timea[name1[0]-'A'] += timee;
timea[name2[0]-'A'] += timee;
store[name1[0]] = name1;
store[name2[0]] = name2;
}
judge();
vector <int> result;
for(int i=0;i<vec.size();i++){
if (weight[vec[i]]>k&&member[vec[i]]>2){result.push_back(vec[i]);}
}
cout<<result.size()<<endl;
for (int i=0;i<result.size();i++){
int maxhead = 0;
int maxtime = 0;
for (int j=0;j<MAXN;j++){
if (!visit[j]){continue;}
if (father[j] == result[i]){
if (timea[j]>maxtime){
maxhead = j;
maxtime = timea[j];
}
}
}
r r1;
r1.head = store[maxhead+'A'];
r1.member = member[father[maxhead]];
re.push_back(r1);
}
sort(re.begin(),re.end(),compare);
for (int i=0;i<re.size();i++){
cout<<re[i].head<<' '<<re[i].member<<endl;
}
return 0;
}