1034. Head of a Gang (30)

采用dfs的方法来得到每一个连通分量,对每一个连通分量进行筛选。把三个字母化作为26进制的数字转化为int来存储.如果采用邻接矩阵存储在编译时会内存占用过多不通过,所以采用map来存储。

代码如下:


#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define N 26*26*26+1
map<int,vector<int> > adjlist;
map<int,int> weight;
map<int,int> mark;
int n,k;
map<int,int> res;
int maxw,idx,counter,total;
void dfs(int n){

    mark[n]=1;
    counter++;
    total+=weight[n];
   if(weight[idx]<weight[n])idx=n;
    vector<int>::iterator it = adjlist[n].begin();
    for(;it!=adjlist[n].end();it++){
        if(mark[*it]==0){
            dfs(*it);
        }
    }
}
int main(){
    //freopen("1.txt","r",stdin);
    int i;//,j;
    char from[4],to[4];
    int a,b;
    int time;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%s %s %d",from,to,&time);
        a=(from[0]-'A')*26*26+(from[1]-'A')*26+from[2]-'A';
        b = (to[0]-'A')*26*26+(to[1]-'A')*26+to[2]-'A';
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
        weight[a]+=time;
        weight[b]+=time;
        mark[a]=0;
        mark[b]=0;
    }

    map<int,int>::iterator it = mark.begin();
    for(;it!=mark.end();it++){
        if(it->second==0){
            idx=it->first;
            counter=0;
            total=0;
            dfs(it->first);
            if(total/2>k&&counter>2){
                res[idx]=counter;
            }
        }
    }
    printf("%d\n",res.size());
    it=res.begin();
    int a1,a2,a3;
   for(;it!=res.end();it++){
        a1=it->first/(26*26);
        a2=it->first%(26*26)/26;
        a3=it->first%26;
        printf("%c%c%c %d\n",a1+'A',a2+'A',a3+'A',it->second);
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值