heibang

heibang


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#define INF 0x3f3f3f3f
 
using namespace std;
typedef long long ll;
const int maxn = 2010;
int per[maxn],num[maxn];
map<string,int> names;
queue<pair<string,string> > que;
string _next[maxn];
 
int n,k;
struct people
{
    string name;
    int lenth;
}peo[maxn];
 
 
void Init()
{
    for(int i = 1;i <= 2*n;i ++) per[i] = i;
}
int Find(int x)
{
    return  x == per[x] ? x:per[x] = Find(per[x]);
}
 
void Union(int x,int y)
{
    x = Find(x);
    y = Find(y);
    if(x != y)
    {
        if(peo[x].lenth > peo[y].lenth)
            per[y] = x;
        else
            per[x] = y;
    }
}
 
int main()
{
    scanf("%d%d",&n,&k);
    Init();
    getchar();
    string name1,name2;
    int len,index = 0;
    for(int i = 1;i <= n;i ++)
    {
        cin>>name1>>name2>>len;
        if(!names[name1]){
            names[name1] = ++index;
            peo[index].name = name1;
            peo[index].lenth = len;
        }
        else
            peo[names[name1]].lenth += len;
        if(!names[name2]){
            names[name2] = ++index;
            peo[index].name = name2;
            peo[index].lenth = len;
        }
        else
            peo[names[name2]].lenth += len;
        que.push(make_pair(name1,name2));
    }
//    for(int i = 1;i <= index;i ++)
//        cout << peo[i].name<<" "<<peo[i].lenth<<endl;
    pair<string,string> p;
    while(!que.empty())
    {
        p = que.front();que.pop();
        Union(names[p.first],names[p.second]);
    }
    for(int i = 1;i <= index;i ++)
    {
        int x = Find(i);
        if(per[i] != i)
            peo[x].lenth += peo[i].lenth;
        num[x] ++;
    }
//    for(int i = 1;i <= index;i ++)
//        cout << peo[i].name<<" "<<peo[i].lenth<<endl;
 
    int pos = 0;
    for(int i = 1;i <= index;i ++)
    {
        if(num[i] > 2 && peo[i].lenth > k*2)
            _next[++pos] = peo[i].name;
    }
    if(pos == 0)
        printf("0\n");
    else{
        printf("%d\n",pos);
        sort(_next+1,_next+pos+1,less<string>());
        for(int i = 1;i <= pos;i ++)
            cout << _next[i] << " "<<num[names[_next[i]]]<<endl;
    }

#include<iostream>
using namespace std;
#include<map>
#include<string>
const int MAXN=2010;
const int INF=0x3f3f3f3f;
int maritx[MAXN][MAXN];
int weight[MAXN];
int numPerson=0;
bool vis[MAXN]={false};
map<string,int>crime;
map<string,int>stringToInt;
map<int,string>intToString;
void dfs(int nowVis,int &head,int &numMember,int &totalweight){
    numMember++;
    vis[nowVis]=true;
    if(weight[nowVis]>weight[head]){
        head=nowVis;
    }
    for(int i=0;i<numPerson;i++){
        if(maritx[nowVis][i]>0){
            totalweight+=maritx[nowVis][i];
            maritx[nowVis][i]=maritx[i][nowVis]=0;
            //防止下次递归遇见前面已经访问到的结点,totalweight被再次加入
            if(vis[i]==false){
                dfs(i,head,numMember,totalweight);
            }
        }
    }
}
void GraphTrave(int k){
    for(int i=0;i<numPerson;i++){
        if(vis[i]==false){
            int head=i,numMember=0,totalweight=0;
            dfs(i,head,numMember,totalweight);
            if(totalweight>k&&numMember>2){
                crime[intToString[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 n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        string name1,name2;int t;
        cin>>name1>>name2>>t;
        int id1=change(name1);
        int id2=change(name2);
        maritx[id1][id2]+=t;//无向图
        maritx[id2][id1]+=t;
        weight[id1]+=t;
        weight[id2]+=t;
    }
    GraphTrave(k);
    cout<<crime.size()<<endl;
    map<string,int>::iterator it;
    for(it=crime.begin();it!=crime.end();it++){
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值