Love _tarjan in the heart

Source: Internet
Author: User

Topic description Description
"Everyone has a dream, even if not the same, can share with you, regardless of success will be touched." Love because in the heart, ordinary and not mediocre, the world is like a maze, but let us now meet our home. "
There are n individuals in the Kingdom of love, and in their hearts there is a list of love, which records the person he loves (no self-love). Love is transitive, that is, if a loves B,b love C, then a also loves C.
If there are such a group of people, they love each other, they are beyond all limitations, with the collective love Incarnate into a loving angel.
Now, we want to know how many loving angels will appear in this kingdom of love. And if an angel of love is loved by all other people or loving angels, please output This Love Angel is composed of who, otherwise output-1.
Input Description Input Description
Line 1th, two number n, M, on behalf of the Kingdom of love there are n individuals, love has a relationship with M.
2nd to M+1 line, two number A, B per line, representing a love B.
Output description outputs Description
Line 1th, a number, on behalf of love in the kingdom there are many love angel.
Line 2nd, if a loving Angel is loved by everyone else and the Angel of love, please export This Love Angel is composed of who (from small to large sort), otherwise output-1.
Sample Input Sample Input
Sample input 1:
6 7
1 2
2 3
3 2
4 2
4 5
5 6
6 4
Sample input 2:
3 3
1 2
2 1
2 3
Sample output
Sample Output 1:
2
2 3
Sample Output 2:
1
-1
Data range and Tip Data Size & Hint
each test point 1s ideas

The first question is relatively simple, at the same time, the total number of strongly connected components is BELONGX, and then the number of strong connected components is subtracted, that is, the first answer is Tarjan. For the second question, first notice that if multiple angels meet this condition, then also output-1, or WA a point, Fortunately WA point is not much; for a strong connected component that meets the requirements, all points can reach it, but it is 0, because once there is a degree, there is a point of the side can go out from the angel, but will not be the angel (because has been reduced to the point). So scan all the strong connected components, and then sort out the angel with a degree of 0. Code

Type Point=^rec;
   Rec=record E:longint;
   S:point;
  End
Ar=array[0..100]of Longint;
    var dfn,low,belong,stack:array[1..10000]of longint;
    Dot:array[1..1000]of Point;
    Use:array[1..10000]of Boolean;
    Link:array[1..1000]of ar;
    Num:array[1..1000]of Longint;
    Outs:array[1..1000]of integer;
    X,sum,total,top,step,belongx,a,b,i,j,n,m:longint;
P:point;
Procedure Insert (A,b:longint);
var p:point;
  Begin new (P);
  P^.e:=b;
  P^.s:=dot[a];
Dot[a]:=p;

End
Procedure Tarjan (I:longint);
    var j:longint;
P:point;
  Begin Inc (STEP);
  Low[i]:=step;
  Dfn[i]:=step;
  Inc. (top);
  Stack[top]:=i;
  Use[i]:=true;
  P:=dot[i];
      While p<>nil do begin J:=P^.E;
          If Dfn[j]=0 then begin Tarjan (j);
        If low[j]<low[i] then low[i]:=low[j];
     End else if (Use[j]) and (Low[i]>low[j]) then low[i]:=low[j];
    P:=P^.S;
  End If Dfn[i]=low[i] THEN BEGIN Inc (BelonGX);
          while (I&LT;&GT;J) do begin j:=stack[top];
          Inc (NUM[BELONGX]);
          BELONG[J]:=BELONGX;
          Link[belongx,num[belongx]]:=j;
          Dec (top);
         Use[j]:=false;
     End
End

End
Procedure Qsort (B,e:longint);
var t,l,r,m:longint;
  Begin L:=b;r:=e;m:=link[x, (L+R) >>1];
      While L<r does begin while Link[x,l]<m do Inc (L);
      While M<link[x,r] do Dec (r);
          If L<=r then begin t:=link[x,l];
          LINK[X,L]:=LINK[X,R];
          link[x,r]:=t;
          Inc (L);
        Dec (r);
    End
  End
  If L<e then Qsort (l,e);
If B<r then Qsort (b,r);

End
  Begin READLN (N,M);
      For I:=1 to M do begin READLN (A,B);
    Insert (A,B);
  End
  For I:=1 to n do if Dfn[i]=0 then Tarjan (i);
     For I:=1 to n do begin p:=dot[i];
         While p<>nil do BEGIN if BELONG[I]&LT;&GT;BELONG[P^.E] then BEGIN     Inc (Outs[belong[i]]);
         End
       P:=P^.S;
   End
  End
  SUM:=BELONGX;
  For i:=1 to Belongx do if Num[i]=1 then Dec (sum);
  Writeln (sum);
          For i:=1 to BELONGX does begin if (outs[i]=0) and (num[i]<>1) THEN BEGIN Inc (total);
          If Total>1 then begin write ( -1); halt;end;
        X:=i;
    End
  End
  if (total=0) then begin write ( -1); halt;end;
  Qsort (1,num[x]);
  For I:=1 to Num[x] do write (Link[x,i], "");
Close (input);


End.
Testing through accepted total time consuming: 2 ms 0/0 data passed the test. Run results test point #data0.in results: AC memory Usage: 256kB time usage: 0ms test point #data1.in Result: AC memory usage: 256kB time usage: 0ms test Pilot #data2.in results: AC memory Usage: 256kB time usage: 0ms test point #data3.in results: AC memory Usage: 256kB time usage: 1ms test point #dat  A4.in results: AC memory Usage: 256kB time usage: 0ms test point #data5.in results: AC memory Usage: 256kB time usage: 0ms test point #data6.in Results: AC Memory Usage: 256kB time usage: 0ms test point #data7.in Result: AC Memory usage: 256kB    Time usage: 0ms test point #data8.in Result: AC memory usage: 364kB time usage: 1ms test point #data9.in Result: AC memory usage: 364kB time      Use quantity: 0ms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.