Algorithm
One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something.
The following statement caught his attention: "Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people"
Igor just couldn't get why the required minimum is 6 people. "Well, that's the same for five people, too!" — he kept on repeating in his mind. — "Let's take, say, Max, Ilya, Vova — here, they all know each other! And now let's add Dima and Oleg to Vova — none of them is acquainted with each other! Now, that math is just rubbish!"
Igor K. took 5 friends of his and wrote down who of them is friends with whom. Now he wants to check whether it is true for the five people that among them there are either three pairwise acquainted or three pairwise not acquainted people.
The first line contains an integer m (0 ≤ m ≤ 10), which is the number of relations of acquaintances among the five friends of Igor's.
Each of the following m lines contains two integers ai and bi (1 ≤ ai, bi ≤ 5;ai ≠ bi), where (ai, bi) is a pair of acquainted people. It is guaranteed that each pair of the acquaintances is described exactly once. The acquaintance relation is symmetrical, i.e. if x is acquainted with y, then y is also acquainted with x.
Print "FAIL", if among those five people there are no either three pairwise acquainted or three pairwise unacquainted people. Otherwise print "WIN".
4
1 3
2 3
1 4
5 3
WIN
5
1 2
2 3
3 4
4 5
5 1
FAIL
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
int main(){
int n(0); scanf("%d", &n);
bool output(1);
if(n == 5){
output = 0;
int array[5] = {0};
for(int k = 0; k < n; k++){
int a(0), b(0); scanf("%d %d", &a, &b);
++array[a - 1]; ++ array[b - 1];
}
for(int k = 0; k > 5; k++){if(array[k] != 2){output = 1; break;}}
}
if(output){puts("WIN");} else{puts("FAIL");}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 3
2 3
1 4
5 3
Output
Demonstration
Codeforces Solution-B. Friends-Solution in C, C++, Java, Python