UVa Problem Solution: 10044 - Erdos Number


The unclear specifications on the input format and size lead to the undeserving difficulties to solve this problem.  After numerous frustrating REs and WAs, I was forced to switch to C++. I'm still not sure about the judge's data format, but I believe its size is smaller 5000.

The follow code uses a simple BFS on the coauthor graph. The parsing of paper database is relatively simple but tricky. It could be surely much simpler if the judge's data format strictly conforming to the specification.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10044 C++ "Erdos Number" */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <vector>
  10. #include <map>
  11. #include <queue>
  12. #include <limits>
  13. using namespace std;
  14. void get_authors(vector<string> const& papers, 
  15.                  vector<vector<size_t> >& authors,
  16.                  map<string, size_t>&namelist)
  17. {
  18.   for (int i = 0; i < papers.size(); ++i) {
  19.     string const&p = papers[i];
  20.     for (int fb, fe = 0, lb, le = 0; p[fe] != ':' && p[le] != ':'; ) {
  21.       fb = p.find_first_not_of(" ,", le);
  22.       fe = p.find_first_of(" ,:", fb);
  23.       lb = p.find_first_not_of(" ,", fe);
  24.       le = p.find_first_of(" ,:", lb);
  25.       if (fb == string::npos || fe == string::npos || 
  26.           lb == string::npos || le == string::npos) {
  27.         break;
  28.       }
  29.       string name = p.substr(fb, fe-fb) + ", " + p.substr(lb, le-lb);
  30.       map<string, size_t>::iterator it = namelist.find(name);
  31.       if (it == namelist.end()) {
  32.         it = namelist.insert(it, make_pair(name, namelist.size()));
  33.       }
  34.       authors[i].push_back(it->second);
  35.     }
  36.   }
  37. }
  38. void get_coauthors(vector<vector<size_t> > const& authors,
  39.                    vector<vector<size_t> >& coauthors)
  40. {
  41.   for (int i = 0; i < authors.size(); ++i) {
  42.     vector<size_tconst& a = authors[i];
  43.     if (a.empty()) {
  44.       continue;
  45.     }
  46.     for (int a1 = 0; a1 < a.size() - 1; ++a1) {
  47.       for (int a2 = a1 + 1; a2 < a.size(); ++a2) {
  48.         coauthors[a[a1]].push_back(a[a2]);
  49.         coauthors[a[a2]].push_back(a[a1]);
  50.       }
  51.     }
  52.   }
  53. }
  54. void get_erdos_numbers(vector<vector<size_t> > const& coauthors, 
  55.                        vector<int>& erdos_numbers, size_t s)
  56. {
  57.   vector<bool> visited(coauthors.size());
  58.   queue<pair<size_tint> > q;
  59.   size_t v = s;
  60.   int d = 0;
  61.   q.push(make_pair(v, d));
  62.   visited[v] = true;
  63.   while (!q.empty()) {
  64.     s = q.front().first;
  65.     d = q.front().second;
  66.     q.pop();
  67.     erdos_numbers[s] = d;
  68.     for (int i = 0; i < coauthors[s].size(); ++i) {
  69.       v = coauthors[s][i];
  70.       if (!visited[v]) {
  71.         q.push(make_pair(v, d+1));
  72.         visited[v] = true;
  73.       }
  74.     }
  75.   }
  76. }
  77. void put_erdos_numbers(vector<string> const& names,
  78.                        map<string, size_tconst& namelist, 
  79.                        vector<intconst& erdos_numbers)
  80. {
  81.   for (int i = 0; i < names.size(); ++i) {
  82.     if (!names[i].empty()) {
  83.       map<string, size_t>::const_iterator it = namelist.find(names[i]);
  84.       if (it != namelist.end() && erdos_numbers[it->second] != -1) {          
  85.         cout << names[i] << " " << erdos_numbers[it->second] << "/n";
  86.       } else {
  87.         cout << names[i] << " infinity/n";
  88.       }
  89.     } else {
  90.       cout << "/n";
  91.     }
  92.   }
  93. }
  94. int main(int argc, char *argv[])
  95. {
  96. #ifndef ONLINE_JUDGE
  97.   string name = argv[0];
  98.   filebuf infb, outfb;
  99.   cin.rdbuf(infb.open((name + ".in").c_str(), ios_base::in));
  100.   cout.rdbuf(outfb.open((name + ".out").c_str(), ios_base::out));
  101. #endif
  102.   int scenarios;
  103.   cin >> scenarios;
  104.   for (int s = 1; s <= scenarios; ++s) {
  105.     int p, n;
  106.     cin >> p >> n;
  107.     cin.ignore(numeric_limits<streamsize>::max(), '/n');
  108.     vector<string> papers(p);
  109.     for (int i = 0; i < p; ++i) {
  110.       getline(cin, papers[i]);
  111.     }
  112.     map<string, size_t> namelist; /* map author name to author id */
  113.     vector<vector<size_t> > authors(p); 
  114.     get_authors(papers, authors, namelist);
  115.     vector<vector<size_t> > coauthors(namelist.size());
  116.     get_coauthors(authors, coauthors);
  117.     vector<int> erdos_numbers(namelist.size(), -1);
  118.     map<string, size_t>::const_iterator it = namelist.find("Erdos, P.");
  119.     get_erdos_numbers(coauthors, erdos_numbers, it->second);
  120.     vector<string> names(n);
  121.     for (int i = 0; i < n; ++i) {
  122.       getline(cin, names[i]);
  123.     }
  124.     cout << "Scenario " << s << "/n";
  125.     put_erdos_numbers(names, namelist, erdos_numbers);
  126.   }
  127.   return 0;
  128. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值