Head of a Gang (map+邻接表+DFS)

One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:

8 59

AAA BBB 10

BBB AAA 20

AAA CCC 40

DDD EEE 5

EEE DDD 70

FFF GGG 30

GGG HHH 20

HHH FFF 10

Sample Output 1:

2

AAA 3

GGG 3

Sample Input 2:

8 70

AAA BBB 10

BBB AAA 20

AAA CCC 40

DDD EEE 5

EEE DDD 70

FFF GGG 30

GGG HHH 20

HHH FFF 10

Sample Output 2:

0

首先 要建一个 以string的 下标的 连接表,需要用map

map<string,vector<string>  > mm;

表内直接存放 string 地址就行了,权值另外保存

map<string,int> node;

再 DFS  求出极大连通图的个数,及各各极大连通图的节点数,权值之和,权值最大的节点地址

坑点:

1、“A "Gang" is a cluster of more than 2 persons ”  所以节点数要大于2

2、因为每次通话每个人都权值都加了,其实总通话时间=权值之和/2;

 #include <iostream>

 #include <string>

 #include <vector>

 #include <map>

 using namespace std;

 struct Gang

 {

    int num,sum;

 };

 string ss1[];

 string ss2[];

    map<string,vector<string>  > mm;

        map<string,int> visit;

        map<string,int> node;

          map<string,Gang> result;

 void DFS(string s,int &sum,string &max,int &num)

 {

    if(node[s]>node[max]) max=s;

     num++;

       sum=sum+node[s];

    visit[s]=;

    for(int i=;i<mm[s].size();i++)

       {

             if(visit[mm[s][i]]==)

                   DFS(mm[s][i],sum,max,num);

       }

 }

 int main()

 {

      int  n,k,t;

       string s1,s2;

       while(cin>>n)

       {

          cin>>k;

          mm.clear();

          visit.clear();

          node.clear();

          result.clear();

          int i;

          for(i=;i<n;i++)

          {

             cin>>s1>>s2>>t;

               ss1[i]=s1;

               ss2[i]=s2;

               visit[s1]=;

               visit[s2]=;

               node[s1]+=t;

               node[s2]+=t;

                mm[s1].push_back(s2);

                mm[s2].push_back(s1);

          }

          map<string,int>::iterator it;

          int num;

          int sum;

          int count=;

          string max;

          for(it=node.begin();it!=node.end();it++)

          {

                if(visit[it->first]==)

                {

                      sum=;

                      num=;

                      max=it->first;

                      DFS(it->first,sum,max,num);

                      if(sum/>k&&num>)

                      {

                  count++;

                        result[max].num=num;

                        result[max].sum=sum;

                      }

                }

          }

          cout<<count<<endl;

          map<string,Gang>::iterator it2;

          for(it2=result.begin();it2!=result.end();it2++)

          {

                cout<<it2->first<<" "<<(it2->second).num<<endl;

          }        

       }

   return ;

 }
上一篇:OAuth2的学习小结


下一篇:创建自己的github代码库