2822 Love in the heart
time limit: 1 sspace limit: 128000 KB title level: Diamonds Diamond
Title Description
Description
"Everyone has a dream, even if they are not the same, can share with you, regardless of success will be touched. Love because in the heart, ordinary but not mediocre, the world is like a maze, but let us now meet our Home. ”
There are n people in the Kingdom of love, in their hearts there is a list of love, which records the person he loves (no self-love situation). Love is transitive, that is, if a love b,b love C, then a also love C.
If there is such a part of people, they love each other, they are beyond all limits, with the collective love Incarnate into a loving angel.
Now, we want to know how many loving angels there will be in this kingdom of love. And if a loving Angel is loved by all others or angels of love, please output this angel of love who is made up of, otherwise output-1.
Enter a description
Input Description
Line 1th, two number n, M, representing the Kingdom of love there are N people, love relations have M.
2nd to m+1, two numbers A, B, each line represents a love B.
Output description
Output Description
The 1th line, a number, represents the love of the country there are many love angels.
Line 2nd, if a loving Angel is loved by all others and loving angels, please output This love Angel is made up 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
5 S
4 5
5 6
6 4
Sample Input 2:
7 ·
1 2
2 1
2 3
Sample output
Sample Output
Sample Output 1:
2
2 3
Sample Output 2:
1
-1
Data range and Tips
Data Size & Hint
Each test point 1s
--------------------------------------------------------------------------------------------------------------- ----
The first question of this question, first use Tarjan to find all the strong link in the figure, find out two points and above the strong Unicom component is the Love Angel in the question
What about the second question?
We need a step-down operation .
is to shrink each of the components into a single point and build another new one, and this figure is definitely not with a ring (tree or forest)
Then find out the "Angel of Love" in the degree of 0 points, starting from this point to traverse the map of the new map, if all points can be traversed, it means "This love Angel is loved by all others or Love angels", otherwise
So why do we have to find the point of 0?
Because according to the definition of the topic, this Love Angel to be able to reach all points, and new pictures without rings, so there can be no degree; From here it is also possible to see that there is at most one
AC Code:
1#include <stdio.h>2#include <string.h>3 #defineMAXN 100104 structnode{5 intTo,next,fr;6 };7 node E[MAXN],TR[MAXN];8 intN,M,BELONG[MAXN],DFN[MAXN],LOW[MAXN],STACK[MAXN],CNT,PRE[MAXN],TIME,TOP,BCNT,PRETR[MAXN],WRT,SIZE[MAXN], DCNT,PO[MAXN],PREF[MAXN];9 BOOLINSTACK[MAXN],PD[MAXN];Ten voidBUILDTR (int,int); One voidTarjan (int); A voidBuildint,int); - voidDfsint); - intread (); the intMain () { -N=read (); M=read (); time=0; top=0; bcnt=0; wrt=0;d cnt=0; - for(intI=1; i<=m;i++){ - intA=read (), b=read (); + Build (A, b); - } + for(intI=1; i<=n;i++)if(!Dfn[i]) Tarjan (i); A for(intI=1; i<=bcnt;i++)if(size[i]>1) Po[++dcnt]=i;//First question atprintf"%d\n", dcnt); -Cnt=0; - for(intI=1; i<=m;i++) - if(belong[e[i].to]!=belong[e[i].fr]) - BUILDTR (belong[e[i].fr],belong[e[i].to]); - for(intI=1; i<=dcnt;i++) in if(!Pretr[po[i]]) { -memset (PD,0,sizeof(PD)); to DFS (Po[i]); + BOOLflag=true; - for(intj=1; j<=bcnt;j++) the if(!Pd[j]) { *flag=false; $ Break;Panax Notoginseng } - if(flag) wrt=Po[i]; the Break;//because there is only one point to meet the conditions, and if this point is not satisfied, and then not satisfied, so find a point out of 0 when you can exit, + } A if(!WRT) printf ("-1"); the Else + for(intI=1; i<=n;i++) - if(belong[i]==wrt) $printf"%d", i); $ return 0; - } - voidDfsintx) { thepd[x]=1; - for(intI=pref[x];i;i=tr[i].next) DFS (tr[i].to);Wuyi } the voidBUILDTR (intXinty) { -TR[++CNT].TO=Y;TR[CNT].NEXT=PRETR[X];p retr[x]=cnt;//New Map WuTr[++cnt].to=x;tr[cnt].next=pref[y];p ref[y]=cnt;//the inverse diagram of the new diagram - } About voidTarjan (intx) { $dfn[x]=low[x]=++Time ; -instack[x]=1; stack[++top]=x; - for(intI=pre[x];i;i=E[i].next) { - intto=e[i].to; A if(!Dfn[to]) { + Tarjan (to); the if(Low[to]<low[x]) low[x]=Low[to]; - } $ Else if(Instack[to]&&dfn[to]<low[x]) low[x]=Dfn[to]; the } the if(dfn[x]==Low[x]) { thebcnt++; the intK; - Do{ ink=stack[top--]; theinstack[k]=0; thebelong[k]=bcnt; Aboutsize[bcnt]++; the} while(k!=x); the } the } + voidBuildintXinty) { -E[++CNT].TO=Y;E[CNT].NEXT=PRE[X];p re[x]=cnt;e[cnt].fr=x; the }Bayi intRead () { the intans=0, f=1;CharC=GetChar (); the while('0'>c| | C>'9'){if(c=='-') f=-1; c=GetChar ();} - while('0'<=c&&c<='9') ans=ans*Ten+c- -, C=getchar ();returnans*F; -}Tarjan
Codevs 2822 Love in mind Tarjan (strong unicom component)