QS Parliament----------------------------------------------------------------------------------------------------------------------------------------------------------------
On a mystery island lives a tribe. What we only know about it is that the name of the resident on this island is QS. Recently, we payed attention to a special local activity among these QS's (pl.). They have an organization just like our parliament, each QS has its own power in the tribe, and no two QS's have the same power. To denote the power, each QS will gets a metal-card with a number on it, the less the number is, the bigger the power it denotes. 
When a new year is coming, all QS's will fight for a better metal-card (with a less number). At first, each QS will get a random metal-card among all cards (with the number 1 to N). Then, there will be several rounds of fight to adjust the distribution. In each round, they will select two numbers A and B (range from 1 to N), and the current owners of these two numbers will fight with each other. Then the stronger QS will get the less number, on the contrary the weaker one will get the bigger one. Assume that there will be no draw.Now, our question is that, after the adjusting, whether they can be assured that all the QS's will get their appropriate positions. It means that no such situation occurs: one QS is stronger than another but gets a bigger number finally.
InputThere are several test cases.In each test case, the first line will contains two positive numbers N (1 <= N <= 15), M (M <= 500). N is the number of QS's, M is the number of fight rounds.Each of the following M lines contains two numbers Ai and Bi, which define the numbers in this round.Process to the end of input.
OutputFor each test case, print "YES" if the adjusting is ok for any situation, otherwise "NO".
Sample Input3 3
1 2
1 3
2 33 3
1 2
2 3
1 3
Sample OutputYES
NO
Note:Let's take the second sample input to explain why it is not ok.Assume the three QS's : QS1 QS2 QS3, and QS3 is stronger than QS2, QS2 is stronger than QS1. And at first QS1 gets the metal-card with number 1 on, QS2 gets 2, QS3 gets 3.original distribution: QS1[No.1 card] QS2[No.2 card] QS3[No.3 card]Round 1:card : No.1 card <<VS>> No.2 card
QS : QS1 <<VS>> QS2
result: QS2 wins this round and gets No.1 card, QS1 gets No.2 card
distribution: QS1[No.2 card] QS2[No.1 card] QS3[No.3 card]Round 2:card : No.2 card <<VS>> No.3 card
QS : QS1 <<VS>> QS3
result: QS3 wins this round and gets No.2 card, QS1 gets No.3 card
distribution: QS1[No.3 card] QS2[No.1 card] QS3[No.2 card]Round 3:card : No.1 card <<VS>> No.3 card
QS : QS2 <<VS>> QS1
result: QS2 wins this round and gets No.1 card, QS1 gets No.3 card
final distribution: QS1[No.3 card] QS2[No.1 card] QS3[No.2 card]QS3 is stronger than QS2, but gets a worse card finally, so this adjusting is not ok for the situation we assumed above.

解决方案 »

  1.   

    我的程序为什么过不了??? program Jackie; 
    type 
     r=record 
       a1,a2:integer; 
       end; 
    var 
     n,m,i,j,e:integer; 
     c:array[1..15]of integer; 
     dat:array[1..500]of r; 
     flg:boolean; procedure main(j:integer); 
    begin 
     for i:=1 to n-j do c[i]:=i+j; 
     for i:=n-j+1 to n do c[i]:=i-n+j; 
     for i:=1 to m do 
       begin 
         if (dat[i].a1>dat[i].a2) 
         and(c[dat[i].a1]<c[dat[i].a2]) then 
                               begin 
                                 e:=c[dat[i].a1]; 
                                 c[dat[i].a1]:=c[dat[i].a2]; 
                                 c[dat[i].a2]:=e; 
                               end 
         else if (dat[i].a1<dat[i].a2) 
         and(c[dat[i].a1]>c[dat[i].a2]) then 
                               begin 
                                 e:=c[dat[i].a1]; 
                                 c[dat[i].a1]:=c[dat[i].a2]; 
                                 c[dat[i].a2]:=e; 
                               end 
       end; 
     i:=1; 
     while c[i]=i do i:=i+1; 
     if i<=n then flg:=false; end; begin 
     while not eof do 
       begin 
         readln(n,m); 
         for i:=1 to m do readln(dat[i].a1,dat[i].a2); 
         j:=n div 2; 
         flg:=true; 
         while (j<=n-1)and flg do 
           begin 
             main(j); 
             j:=j+1; 
           end; 
         c[1]:=1; 
         for i:=2 to n do c[i]:=n-i+2; 
         for i:=1 to m do 
           begin 
             if (dat[i].a1<dat[i].a2)and(c[dat[i].a1]>c[dat[i].a2]) then 
                               begin 
                                 e:=c[dat[i].a1]; 
                                 c[dat[i].a1]:=c[dat[i].a2]; 
                                 c[dat[i].a2]:=e; 
                               end 
             else  if (dat[i].a1>dat[i].a2)and(c[dat[i].a1]<c[dat[i].a2]) then 
                               begin 
                                 e:=c[dat[i].a1]; 
                                 c[dat[i].a1]:=c[dat[i].a2]; 
                                 c[dat[i].a2]:=e; 
                               end 
           end; 
         i:=1; 
         while c[i]=i do i:=i+1; 
         if i<=n then flg:=false; 
         if flg then writeln('YES') else writeln('NO'); 
         readln; 
       end; 
    end.