1034 Head of a Gang (30 分)(图的遍历or并查集)

dfs

#include<bits/stdc++.h>

using namespace std;
const int N=3000;
int mp[N][N];
int weight[N];
int vis[N];
map<string,int>si;
map<int,string>is;
map<string,int>gang;
int cnt;
//进行转换
int solve(string x) { if(si.find(x)!=si.end()){ return si[x]; } else{ si[x]=cnt; is[cnt]=x; return cnt++; } } void dfs(int now,int &head,int &number,int &num) { if(weight[now]>weight[head]){ head=now;//判断头目 } number++;//增加数量 vis[now]=true; for(int i=0;i<cnt;i++){ if(mp[now][i]>0){ num+=mp[now][i];//总和 mp[now][i]=mp[i][now]=0; if(!vis[i]){ vis[i]=true; dfs(i,head,number,num); } } } } int main() { fill(weight,weight+N,0); fill(mp[0],mp[0]+N*N,0); fill(vis,vis+N,false); int n,k; cin>>n>>k; for(int i=0;i<n;i++){ string a,b; int Num; cin>>a>>b>>Num; int x=solve(a); int y=solve(b); weight[x]+=Num;//每个点都需要 weight[y]+=Num; mp[x][y]+=Num; mp[y][x]+=Num; } for(int i=0;i<cnt;i++){ if(!vis[i]){ int head=i; int number=0; int num=0; vis[i]=true; dfs(i,head,number,num); if(number>2&&num>k){ gang[is[head]]=number; } } } cout<<gang.size()<<endl; for(auto &it:gang){ cout<<it.first<<" "<<it.second<<endl; } return 0; }

 

 

并查集

自己做的时候用的是并查集 AC了 就是暴力找 后面的代码自己不仔细看也不知道都啥意思 反正很乱

#include<bits/stdc++.h>

using namespace std;
const int N=3000;
int weight[N];
int p[N];
int sum[N];
int mp[N][N];
int findth(int x)
{
    if(x==p[x]) return x;
    return p[x]=findth(p[x]);
}
void unionn(int x,int y)
{
    int xx=findth(x);
    int yy=findth(y);
    if(xx!=yy){
        p[yy]=xx;
        sum[xx]+=sum[yy];
    }
}

map<string,int>si;
map<int,string>is;
map<string,int>gang;
int cnt;
int solve(string a)
{
    if(si.find(a)!=si.end()) return si[a];
    else{
        si[a]=cnt;
        is[cnt]=a;
        return cnt++;
    }
}
struct node
{
    int id;
    int sum1;
    node(int _id,int _sum):id(_id),sum1(_sum){}
};

int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        p[i]=i;
        sum[i]=1;
    }
    cnt=0;
    memset(mp,0,sizeof(mp));
    for(int i=0;i<n;i++){
        string a,b;
        cin>>a>>b;
        int num;
        cin>>num;
        int x=solve(a);
        int y=solve(b);
        weight[x]+=num;
        weight[y]+=num;
        mp[x][y]+=num;
        unionn(x,y);
    }
    set<int>st;
    for(int i=0;i<cnt;i++){
        int x=findth(i);
        st.insert(x);
    }
    vector<int>vec;
    for(auto it:st){
        if(sum[it]>2){
            vec.push_back(it);
        }
    }
    if(vec.size()==0){
        cout<<"0"<<endl;
        return 0;
    }
    vector<int>ve[N];
    for(int i=0;i<vec.size();i++){
        for(int j=0;j<cnt;j++){
            if(p[j]==vec[i]){
                ve[i].push_back(j);
            }
        }
    }
    int su=0;

    vector<int>pp;
    map<int,int>mm;
    vector<node>no;
    for(int i=0;i<vec.size();i++){
        vector<int>tt;
        for(int j=0;j<ve[i].size();j++){
            tt.push_back(ve[i][j]);
        }
        for(int z=0;z<tt.size();z++){
            for(int w=0;w<tt.size();w++){
                su+=mp[tt[z]][tt[w]];
            }
        }
        if(su>k){
            no.push_back(node(i,su));
        }
        su=0;
    }
    if(no.size()==0){
        cout<<"0"<<endl;
        return 0;
    }
    cout<<no.size()<<endl;
    for(int i=0;i<no.size();i++){
        int maxn=-1;
        int idd=0;
        for(int j=0;j<ve[no[i].id].size();j++){
            if(weight[ve[no[i].id][j]]>maxn){
                maxn=weight[ve[no[i].id][j]];
                idd=ve[no[i].id][j];
            }
        }
        gang[is[idd]]=sum[findth(idd)];
    }
    for(auto it:gang){
        cout<<it.first<<" "<<it.second<<endl;
    }
    return 0;
}

 

posted @ 2018-12-13 19:27  12-num  阅读(761)  评论(0编辑  收藏  举报