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 
#include 
#include 
#include 
#include 
#include 
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 intTostring;
map stringToint;
map 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;i0){
            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;i2&&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>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<::iterator it;
    for(it=Gang.begin();it!=Gang.end();it++){
        cout<first<<" "<second<


BFS版:

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

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 2010;
int n, k, num = 0;
bool vis[maxn] = {false};
map mp;
map mp1;

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

vector v1;
void BFS(int v) {
    queue 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::iterator it = mp1.find(v1[i]);
        cout << it->second << " " << node[v1[i]].num << endl;
    }
    return 0;
}




你可能感兴趣的:(PAT)