第三题AC 答案 ```c++ #include<iostream> (30316)#include<vector> #include<unordered_map> using namespace std; int main() { int n; cin >> n; string s; cin >> s; unordered_map<int, pair<long long, long long>> hash; // 核心思想是分别记录每一个点的邻居中, 与之相同的节点数量和与之不同的节点数量 // 之后两边相乘,就是经过该点的“abb" 路径数量 while(--n) { int x, y; cin >> x >> y; if(s[x - 1] == s[y - 1]) { hash[x].first++; hash[y].first++; } else { hash[x].second++; hash[y].second++; } } long long res = 0; for(auto [k, p] : hash) { res += (p.first * p.second); } cout << res << endl; return 0; } ```

相关推荐

点赞 评论 收藏
分享
牛客73617529...:无端端被你骂一句
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务