
这个题是前面某个题的升级版一样的方法都是要建立两个 map因为 map 查询快。但这个题的 map 复杂一些mapstring, vectorstring mapp1;因为一个危险品可能与这个危险品会发生化学反应的产品不止一种去仔细观察题目。所以不能直接写等于号。这种 map 怎么遍历那就要写万能不会错的遍历方法也就是遍历 set 的方法。for ( auto it mapp1[v[j]].begin(); it ! mapp1[v[j]].end(); it )if ( mapp2.count(*it) )还有一个小总结一般编译器输出为空有两种可能一是自己输入写的不合适也就是写错了二是代码中有一些手误导致无法运行。#includebits/stdc.h using namespace std; int main() { int n, m, k; cin n m; string s1, s2; mapstring, vectorstring mapp1; for(int i 0; i n; i ) { cin s1 s2; mapp1[s1].push_back(s2); mapp1[s2].push_back(s1); } for(int i 0; i m; i ) { cin k; vectorstring v(k); mapstring, int mapp2; int sum 0; for(int j 0; j k; j ) { cin v[j]; mapp2[v[j]] 2; } for(int j 0; j k; j ) { if(mapp1.count(v[j])) { for(auto it mapp1[v[j]].begin(); it ! mapp1[v[j]].end(); it ) { if(mapp2.count(*it)) { sum ; cout No endl; break; } } } if(sum 1) break; } if(sum 0) cout Yes endl; } return 0; }