PAT1034

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1034

思路:用DFS, 一次DFS遍历可得到一个连通域,用vector<string> 记录下。在判断即可,借助C++的容器,邻接表很容易实现。

 

  1 #include<iostream>
  2 #include<map>
  3 #include<iterator>
  4 #include<vector>
  5 #include<string>
  6 #include<algorithm>
  7 using namespace std;
  8 
  9 struct Node
 10 {
 11     string head;
 12     int members;
 13 };
 14 
 15 bool comp(Node n1, Node n2)
 16 {
 17     if(n1.head < n2.head)
 18         return true;
 19     else
 20         return false;
 21 }
 22 
 23 void DFS_visit(map<string, vector<string>> &gra, map<string, int> &used, 
 24                         vector<string> &s, string start)
 25 {
 26     s.push_back(start);
 27     used[start] = 1;
 28     vector<string>::iterator iter = gra[start].begin();
 29     for(; iter != gra[start].end(); ++iter)
 30         if(used[*iter] == 0)
 31             DFS_visit(gra, used, s, *iter);
 32 }
 33 
 34 void DFS_version(map<string, int> &mem, map<string, vector<string>> &gra, 
 35                             map<string, int> used, int K)
 36 {
 37     vector<Node> outcome;
 38     map<string, vector<string>>::iterator  iter= gra.begin();
 39     for(; iter != gra.end(); ++iter)
 40     {
 41         /*保存DFS时路径*/
 42         vector<string> s;
 43         if(used[iter->first] == 0)
 44         {
 45             /*一次DFS可得一个连通域,用s记录*/
 46             DFS_visit(gra, used, s, iter->first);
 47             if( s.size() > 2)
 48             {
 49                 int relation(0);
 50                 for(int i=0; i<s.size(); ++i)
 51                     relation += mem[s[i]];
 52                 /*注意除2,因为之前mem存的是点所有边权重之和*/
 53                 if(relation/2 > K)
 54                 {
 55                     Node n; n.members = s.size();
 56                     int max(-1);
 57                     string who;
 58                     for(int j=0; j<s.size(); ++j)
 59                         if(mem[s[j]] > max)
 60                         {
 61                             max = mem[s[j]];
 62                             who = s[j];
 63                         }
 64                     n.head = who;
 65                     outcome.push_back(n);
 66                 }
 67             }
 68         }
 69     }
 70     sort(outcome.begin(), outcome.end(), comp);
 71     cout<<outcome.size()<<endl;
 72     for(int i=0; i<outcome.size(); ++i)
 73         cout<<outcome[i].head<<" "<<outcome[i].members<<endl;
 74 }
 75 
 76 int main()
 77 {
 78     int N, K;
 79     while(cin>>N>>K)
 80     {
 81         /*存每个点的总权重*/
 82         map<string, int> mem;
 83         /*邻接表*/
 84         map<string, vector<string>> gra;
 85         /*标记是否DFS访问过*/
 86         map<string, int> used;
 87         string s1, s2; int m;
 88         for(int i=0; i<N; ++i)
 89         {
 90             cin>>s1>>s2>>m;
 91             mem[s1]+=m;
 92             mem[s2]+=m;
 93             gra[s1].push_back(s2);
 94             gra[s2].push_back(s1);
 95             used[s1] = 0;
 96             used[s2] = 0;
 97         }
 98         DFS_version(mem, gra, used, K);
 99     }
100     return 0;
101 }

posted @ 2013-10-21 16:49  coding_monkey  阅读(181)  评论(0编辑  收藏  举报