Codeforces 467D - Fedor and Essay (反向建图 + DFS)

题意

给出原文的几个单词和几组替换词,要求最后包含的r最少,长度越短越好。

思路

因为替换的终点肯定可以确定(R最少的单词),所以可以反向建图,把所有单词按照r排序以后DFS一遍,把它们能走到的点的r和size都更新为自己的r、size。

然后直接统计文章中的单词的r和sz

代码

  
  
  
  
  1. #include <stack>
  2. #include <cstdio>
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <cstring>
  11. #include <algorithm>
  12. #include <cctype>
  13. #include <string>
  14. #include <map>
  15. #include <cmath>
  16. using namespace std;
  17. #define LL long long
  18. #define ULL unsigned long long
  19. #define SZ(x) (int)x.size()
  20. #define Lowbit(x) ((x) & (-x))
  21. #define MP(a, b) make_pair(a, b)
  22. #define MS(arr, num) memset(arr, num, sizeof(arr))
  23. #define PB push_back
  24. #define X first
  25. #define Y second
  26. #define ROP freopen("input.txt", "r", stdin);
  27. #define MID(a, b) (a + ((b - a) >> 1))
  28. #define LC rt << 1, l, mid
  29. #define RC rt << 1|1, mid + 1, r
  30. #define LRT rt << 1
  31. #define RRT rt << 1|1
  32. #define BitCount(x) __builtin_popcount(x)
  33. #define BitCountll(x) __builtin_popcountll(x)
  34. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  35. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  36. const double PI = acos(-1.0);
  37. const int INF = 0x3f3f3f3f;
  38. const double eps = 1e-8;
  39. const int MAXN = 2e5 + 10;
  40. const int MOD = 29;
  41. const int dir[][2] = { {1, 0}, {0, 1} };
  42. int cases = 0;
  43. typedef pair<int, int> pii;
  44. struct NODE
  45. {
  46. int id, rNum, sz;
  47. bool operator < (const NODE &a) const
  48. {
  49. if (rNum != a.rNum) return rNum < a.rNum;
  50. return sz < a.sz;
  51. }
  52. };
  53. void ChangeToLower(string &str)
  54. {
  55. for (int i = 0; i < SZ(str); i++) str[i] = tolower(str[i]);
  56. }
  57. int CountR(const string &str)
  58. {
  59. int cnt = 0;
  60. for (int i = 0; i < SZ(str); i++)
  61. if (str[i] == 'r') cnt++;
  62. return cnt;
  63. }
  64. map<string, int> oddId;
  65. map<int, int> newId;
  66. int cnt;
  67. vector<NODE> words;
  68. int GetNumber(string &tmp)
  69. {
  70. ChangeToLower(tmp);
  71. if (oddId.count(tmp)) return oddId[tmp];
  72. oddId[tmp] = cnt;
  73. int r = CountR(tmp);
  74. words.PB((NODE){cnt, r, SZ(tmp)});
  75. cnt++;
  76. return cnt - 1;
  77. }
  78. vector<int> G[MAXN], article;
  79. int vis[MAXN];
  80. void DFS(int curId, int r, int sz)
  81. {
  82. vis[curId] = 1;
  83. for (int i = 0; i < SZ(G[curId]); i++)
  84. {
  85. int tmpId = G[curId][i];
  86. if (!vis[tmpId])
  87. {
  88. vis[tmpId] = 1;
  89. words[newId[tmpId]].rNum = r;
  90. words[newId[tmpId]].sz = sz;
  91. DFS(tmpId, r, sz);
  92. }
  93. }
  94. }
  95. int main()
  96. {
  97. //ROP;
  98. int n;
  99. cin >> n;
  100. for (int i = 0; i < n; i++)
  101. {
  102. string tmp;
  103. cin >> tmp;
  104. int curNumber = GetNumber(tmp);
  105. article.PB(curNumber);
  106. }
  107. int ncnv;
  108. cin >> ncnv;
  109. for (int i = 0; i < ncnv; i++)
  110. {
  111. string tmp;
  112. cin >> tmp;
  113. int u = GetNumber(tmp);
  114. cin >> tmp;
  115. int v = GetNumber(tmp);
  116. G[v].PB(u);
  117. }
  118. sort(words.begin(), words.end());
  119. for (int i = 0; i < SZ(words); i++) newId[words[i].id] = i;
  120. for (int i = 0; i < SZ(words); i++)
  121. if (!vis[words[i].id]) DFS(words[i].id, words[i].rNum, words[i].sz);
  122. LL rAns = 0, szAns = 0;
  123. for (int i = 0; i < SZ(article); i++)
  124. rAns += words[newId[article[i]]].rNum, szAns += words[newId[article[i]]].sz;
  125. cout << rAns << " " << szAns << endl;
  126. return 0;
  127. }

你可能感兴趣的:(ACM,codeforces)