PATA1034题解

题中的n为n对,maxn要开到2010;

利用map映射的方法,将字符串转化为数字,将数字转化为字符串

再通过DFS,或BFS遍历,记录满足条件的Gang即可

DFS版:

//
//  main.cpp
//  PATA1034
//
//  Created by Phoenix on 2018/1/17.
//  Copyright © 2018年 Phoenix. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int maxn=2010;
int n,k,numperson=0;
int G[maxn][maxn]={0},weight[maxn]={0};
bool vis[maxn]={false};

map<int,string> intTostring;
map<string,int> stringToint;
map<string,int> Gang;

void DFS(int nowVisit,int& head,int& numMember,int& totalValue){
    //printf("%d\n",nowVisit);
    numMember++;
    vis[nowVisit]=true;
    if(weight[nowVisit]>weight[head]){
        head=nowVisit;
    }
    for(int i=0;i<numperson;i++){
        if(G[nowVisit][i]>0){
            totalValue += G[nowVisit][i];
            G[nowVisit][i]=G[i][nowVisit]=0;
            if(vis[i]==false){
                DFS(i, head, numMember, totalValue);
            }
        }
    }
}

void DFSTrave(){
    for(int i=0;i<numperson;i++){
        if(vis[i]==false){
            int head=i,numMember=0,totalValue=0;
            DFS(i,head,numMember,totalValue);
            if(numMember>2&&totalValue>k){
                Gang[intTostring[head]]=numMember;
                //printf("%d %d\n\n",head,numMember);
            }
        }
    }
}

int change(string str){
    if(stringToint.find(str)!=stringToint.end())
        return stringToint[str];
    else{
        stringToint[str]=numperson;
        intTostring[numperson]=str;
        return numperson++;
    }
}

int main(int argc, const char * argv[]) {
    scanf("%d %d",&n,&k);
    //printf("%d %d\n",n,k);
    for(int i=0;i<n;i++){
        string str1,str2;
        int w;
        cin>>str1>>str2>>w;
        int id1=change(str1);
        int id2=change(str2);
        //printf("%d %d %d\n",id1,id2,w);
        weight[id1] += w;
        weight[id2] += w;
        G[id1][id2] += w;
        G[id2][id1] += w;
    }
    DFSTrave();
    cout<<Gang.size()<<endl;
    map<string,int>::iterator it;
    for(it=Gang.begin();it!=Gang.end();it++){
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}


BFS版:

//
//  main.cpp
//  PATA1034
//
//  Created by Phoenix on 2018/2/10.
//  Copyright © 2018年 Phoenix. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 2010;
int n, k, num = 0;
bool vis[maxn] = {false};
map<string, int> mp;
map<int, string> mp1;

struct node {
    int data;
    int num;
    vector<int> v;
}node[maxn];

vector<int> v1;
void BFS(int v) {
    queue<int> q;
    int ans = 0, t = 0, optvalue = 0, name;
    q.push(v);
    vis[v] = true;
    while(!q.empty()) {
        int top = q.front();
        //printf("%d ", top);
        q.pop();
        t++;
        ans += node[top].data;
        if(node[top].data > optvalue) {
            optvalue = node[top].data;
            name = top;
        }
        for(int i = 0; i < node[top].v.size(); i++) {
            if(vis[node[top].v[i]] == false){
                q.push(node[top].v[i]);
                vis[node[top].v[i]] = true;
            }
        }
    }
    ans /= 2;
    if(t > 2 && ans > k) {
        v1.push_back(name);
        node[name].num = t;
    }
    //printf(" %d %d\n", t, ans);
}

void travelBFS() {
    for(int i = 0; i < num; i++) {
        if(vis[i] == false){
            BFS(i);
        }
    }
}

bool cmp(int a, int b) {
    string str1 = mp1[a];
    string str2 = mp1[b];
    return str1 < str2;
}

int main(int argc, const char * argv[]) {
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; i++) {
        string str1, str2;
        int time, name1, name2;
        char a[4], b[4];
        cin >> a >> b >> time;
        str1 = a; str2 = b;
        if(mp.find(str1) == mp.end()) {
            mp[str1] = num++;
        }
        name1 = mp[str1];
        mp1[name1] = str1;
        if(mp.find(str2) == mp.end()) {
            mp[str2] = num++;
        }
        name2 = mp[str2];
        mp1[name2] = str2;
        node[name1].data += time;
        node[name2].data += time;
        node[name1].v.push_back(name2);
        node[name2].v.push_back(name1);
    }
    travelBFS();
    printf("%d\n", v1.size());
    sort(v1.begin(), v1.end(), cmp);
    for(int i = 0; i < v1.size(); i++) {
        map<int, string>::iterator it = mp1.find(v1[i]);
        cout << it->second << " " << node[v1[i]].num << endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值