Algorithms Exercises --- 5-4 exchange student (UVa10763)

A: Title

There are a group of students, their hands each have their own schools and targeted schools (A, B) want to go. In order to successfully exchange student, the student must ensure that this group must be met between every two 
s1 (A, B) and s2 (B, A). That is both original and target schools corresponding to the exchange can be achieved

(A) Sample Input

10
1 2
2 1
3 4
4 3
100 200
200 100
57 2
2 57
1 2
2 1
10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
0

(B) Sample Output

YES
NO

Two: code implementation

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <map>usingnamespace std;int main()
{
    freopen("data5_4_h.in", "r", stdin);
    freopen("data5_4_h.out", "w", stdout);int num;
    while (cin >> num && num!=0)
    {        map
        vector<int> first_id_vec;access to information//

 



    
<int, int> first_id_map, second_id_map;

        
        for (int i = 0; i < num; i++)
        {
            int f, s;
            cin >> f >> s;
            first_id_map[f] = s;
            second_id_map[s] = f;
            first_id_vec.push_back(f);
        }
        //信息匹配
        bool flag = true;
        for (int i = 0; i < num; i++)
            if (first_id_map[first_id_vec[i]] != second_id_map[first_id_vec[i]])
            {
                flag = false;
                break;
            }
        if (flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    freopen("CON", "r", stdin);
    freopen("CON", "w", stdout);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ssyfj/p/11539624.html