【題解】ZeroJudge b563: 3.魔法學校交換生問題

題目敘述:https://zerojudge.tw/ShowProblem?problemid=b563

#include <iostream>
#include <map>
using namespace std;

int main() {
    map <int, map<int,int> > mp;
    int n;
    int a, b;
    while (cin >> n){
        int ans = 0;
        for (int i = 0; i < n; i++){
            cin >> a >> b;
            if (mp.count(b) && mp[b].count(a) && mp[b][a]>0){
                mp[b][a] -= 1;
                ans += 1;
            }else{
                if (!mp.count(a) || !mp[a].count(b)){
                    mp[a][b] = 1;
                }else{
                    mp[a][b] += 1;
                }
            }
        }
        cout << ans << endl;
    }
}
分享本文 Share with friends