机试指南第十一章 图论

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(){
    //freopen("D://case.txt","r",stdin);
    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(){
    //freopen("D://case.txt","r",stdin);
    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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值