1034 Head of a Gang (30分) (DFS 图)

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
using namespace std;
const int maxn = 2010;
int G[maxn][maxn] = {0},weight[maxn] = {0};
map<string,int> stringtoint;
map<int,string> inttostring;
map<string,int> gang;
bool vist[maxn]={0};
int n,k,numperson=0;
int getname(string a) //给每个人编号,字符串转化为数字
{
    if(stringtoint.find(a) != stringtoint.end()) //如果没找到记录
    {
        return stringtoint[a]; //如果不是第一次,直接输出编号
    }
    else
    {
        numperson++; //人数++
        stringtoint[a] = numperson; //给与他的编号
        inttostring[numperson] = a;
        return numperson;
    }
}

void DFS(int now,int& head,int& groupper,int& total) //对一个团体DFS,now现在访问节点,head大哥节点,groupper目前团体人数,total整个团体的边权
{
    vist[now] = true; //已经访问
    groupper++;
    if(weight[now] > weight[head]) //点权重更大的是大哥
        head = now;
    for(int i = 1;i<=numperson;i++) //对他的每一个相连的点
    {
        if(G[now][i] > 0) //如果相连
        {
            total += G[now][i];
            G[i][now] = 0; //切断返回路径,防止成环的团体重复计算
            G[now][i] = 0;
            if(vist[i] == false)
                DFS(i,head,groupper,total);
        }
    }
}

void DFSTra()
{
    int head,grouper,total=0;
    for(int i = 1;i<=numperson;i++)
    {
        if(vist[i] == false)
        {
            head = i;
            grouper =0;
            total = 0;
            DFS(i,head,grouper,total);
            if(total > k && grouper > 2)
            {
                gang[inttostring[head]] = grouper;
            }
        }

    }
}

int main()
{
    freopen("1.txt","r",stdin);
    scanf("%d %d",&n,&k);
    for(int i = 0;i<n;i++)
    {
        string a,b;
        int a1,b1,time;
//        scanf("%s %s %d",a.c_str(),b.c_str(),&time);
//        printf("%s",a.c_str());
        cin>>a>>b>>time;
        a1 = getname(a);
        b1 = getname(b);
        G[a1][b1] += time;
        G[b1][a1] += time;
        weight[a1] += time;
        weight[b1] += time;
    }
    DFSTra();
    printf("%d\n",gang.size());
    map<string,int>::iterator it;
    for(it = gang.begin();it!=gang.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、付费专栏及课程。

余额充值