[刷题]算法竞赛入门经典/第五章/交换学生(习题5-4)

Foreign Exchange (UVA原题的链接单击这里哦???)


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 
-------------------------------------------------
Sample Output:?
-------------------------------------------------
YES
NO
-------------------------------------------------
#include 

using namespace std;
const int MAX=500000;
int main() {
    int t;
    while (cin >> t && t != 0) {
        int x, y, stu[MAX];
        for (int i = 0; i < MAX; ++i) {
            stu[i] = i; // 表示每个同学对应学校
        }
        for (int j = 0; j < t; ++j) {
            cin >> x >> y;
            swap(stu[x], stu[y]);
        }
        bool flag = true;
        for (int k = 0; k < MAX; ++k) {
            if (stu[k] != k) {
                flag = false;
                break;
            }
        }
        printf(flag?"YES\n":"NO\n");
    }
    return 0;
}

 

 

你可能感兴趣的:(LeetCode)