Topic Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1704
Original title:
Your Non-Profit organization (Icore-international Confederation of Revolver Enthusiasts) coordinates a very successful F Oreign Student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.
The program your organization runs works as Follows:all candidates are asked for their original location and the location They would like to go. The program works for only if every student has a suitable exchange partner. In the other words, the If a student wants to go from a to B, there must is another student who wants to go from B to a. This is a easy task when there were is only about candidates and however now there are up to 500000
Input
The input file contains multiple cases. Each test case would consist of a line containing n-the number of candidates (1≤n≤500000), followed by n lines Representi ng the exchange information for each candidate. Each of these lines'll contain 2 integers, separated by a, representing the candidate ' s original location A nd the candidate ' s target location respectively. Locations is represented by nonnegative integer numbers. You could assume that no candidate would have his or her original location being the same as he or her target location as th Is would fall to the domestic exchange program. The input is terminated by a case where n = 0; This case should is processed.
Output
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
For each test case, print "YES" on a single line if there are a way for the exchange program to work out, otherwise print " NO ".
The main effect of the topic:
Exchange students are now very popular, and now another organization responsible for this, often receive a large number of application forms, the application from a country to B countries. For a group of application forms, there will be different countries to apply to different countries, assuming that there are any one to B, but no application for B to a, then the batch of applications can not be processed.
Analysis and Summary:
The title can be seen, for applications A to B, then must have a corresponding from B to a, and the number must also be relative. For example, there are two A to B, but only one B to a, then it is not.
So, we can give an ascending sequence 0,1,2,3,4,5,...... N, which exists in the array arr, for each application form, assuming a to B, swap (Arr[a], arr[b).
When all is done, determine if the sequence is still the same as the original arrangement. If so, it is eligible. Otherwise, No. (because for each swap (Arr[a]), arr[b] if there is a corresponding swap (arr[b], arr[a), then the equivalent of the original arrangement is restored)
* * UVA 10763-foreign Exchange
* Time : 0.132s (UVA)
* Author: d_double
*
* * *
#include <iostream>
#include <cstdio>
#include <string>
#include < algorithm>
#define MAXN 500005
using namespace std;
int ARR[MAXN];
void swap (int a,int b) {
int t=arr[a]; Arr[a] = arr[b]; arr[b] = t;
}
void Init () {for
(int i=0; i<maxn; ++i)
arr[i] = i;
}
BOOL IsOk () {for
(int i=0; i<maxn; ++i)
if (arr[i]!=i) return false;
return true;
}
int main () {
int n, I, A, b;
while (scanf ("%d", &n), n) {
init ();
for (int i=0; i<n; ++i) {
scanf ("%d%d", &a, &b);
Swap (A, b);
}
if (IsOk ()) printf ("yes\n");
else printf ("no\n");
}
return 0;
}
Author: csdn Blog shuangde800