PAT甲级-1034 Head of a Gang (30分)

题目:1034 Head of a Gang (30分)
分析:DFS的实现,本题与普通DFS关键的不同就是用字符串来对应数据,使用map即可,代码二内存超限了,无语
代码一:AC
#include <iostream>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<queue>
#include<math.h>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
#define MAX 99999999
typedef long long ll;
using namespace std;
int n,k;
map<string,int>weight;
map<string,map<string,int>>road;
map<string,int>visi;
map<string,vector<string>>people;
set<string>man;
map<string,int>ans;
int cnt;
int relation;
string head;
void dfs(string root)
{
    cnt++;
    relation += weight[root];
    visi[root] = 1;
    if(weight[root] > weight[head])
        head = root;
    for(int i = 0;i<people[root].size();i++)
        if(!visi[people[root][i]])
            dfs(people[root][i]);
}
int main()
{
    cin>>n>>k;
    for(int i = 0;i<n;i++)
    {
        string s1,s2;
        int x;
        cin>>s1>>s2>>x;
        man.insert(s1);
        man.insert(s2);
        people[s1].push_back(s2);
        people[s2].push_back(s1);
        weight[s1] += x;
        weight[s2] += x;
        road[s1][s2] += x;
    }
    int gang = 0;
    for(auto it = man.begin();it!=man.end();it++)
    {
        cnt = 0;
        relation = 0;
        if(!visi[*it]){
            head = *it;
            dfs(*it);
            if(cnt > 2 && relation/2 > k)
             {
                 gang ++;
                 ans[head] = cnt;
             }
        }
    }
    cout<<gang<<endl;
    for(auto it = ans.begin();it!=ans.end();it++)
        cout<<it->first<<" "<<it->second<<endl;
    return 0;
}
代码二
#include <iostream>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<queue>
#include<math.h>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
#define MAX 99999999
typedef long long ll;
using namespace std;
int n,k;
map<string,int>weight;
map<string,map<string,int>>road;
map<string,int>visi;
map<string,map<string,int>>visi2;
set<string>man;
map<string,int>ans;
int cnt;
int relation;
string head;
void dfs(string root)
{
    if(!visi[root])
        cnt++;
    visi[root] = 1;
    if(weight[root] > weight[head])
        head = root;
    for(auto it = man.begin();it!=man.end();it++)
    {
        if(!visi2[root][*it] && road[root][*it] != 0)
        {
            visi2[root][*it] = 1;
            relation += road[root][*it];
            dfs(*it);
        }
    }
}
int main()
{
    cin>>n>>k;
    for(int i = 0;i<n;i++)
    {
        string s1,s2;
        int x;
        cin>>s1>>s2>>x;
        man.insert(s1);
        man.insert(s2);
        road[s1][s2] += x;
        weight[s1] += x;
        weight[s2] += x;
    }
    int gang = 0;
    for(auto it = man.begin();it!=man.end();it++)
    {
        cnt = 0;
        relation = 0;
        if(!visi[*it]){
            head = *it;
            dfs(*it);
            if(cnt > 2 && relation > k)
             {
                 gang ++;
                 ans[head] = cnt;
             }
        }
    }
    cout<<gang<<endl;
    for(auto it = ans.begin();it!=ans.end();it++)
        cout<<it->first<<" "<<it->second<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值