P3405 [USACO16DEC]Cities and States S

  • 将存在特殊关系的城市之间连边,才用哈希表统计具有特殊关系的城市对数
  • 注意:若某个城市自身到自身存在边,则忽略,因为题目要求是不同的两个城市之间存在特殊的关系
unordered_map<string,int> mp;
int n;

int main()
{
    ios;
    cin>>n;

    int ans=0;
    for(int i=0;i<n;i++)
    {
        string a,b;
        cin>>a>>b;
        a=a.substr(0,2);
        if(a != b)
        {
            ans+=mp[b+a];
            mp[a+b]++;
        }
    }

    cout<<ans<<endl;

    //system("pause");
}
原文地址:https://www.cnblogs.com/fxh0707/p/13604880.html