6th annual Shandong Province Tidal Cup Circle of Friends SDUT3262

Source: Internet
Author: User

Circle of Friends Time limit:2000ms Memory limit:65536k have questions? Dot here ^_^
strong connected Shrinking point +bfs (Find the shortest distance);Title Descriptionnowadays, "Circle of Friends" is a very popular social networking platform in WeChat. We can share the friends through it or get other ' s situation.Similarly, in real life, there is also a circle of friends, friends would often get together communicating and playing To maintain friendship. And when you have difficulties, friends'll generally come to help and ask nothing for return.However, the friendship above is true friend relationship while sometimes if regard someone as your friend but he Doesn ' t agree. In this is the if you ask him to help, he often asks to a meal, and then he 'll help you.If People think they is friends Mutually,they would become true Friend,then once one of the them have a problem or make s a query, the other one is offer to help for free. What's more,if one relationship is similar to "A regards B as friend, B regards C as friend and C regards A as friend", the Y'll make a friends circle and become true friends too with each other. Besides, people would not ask those who they don ' t regard as friends for help. If one person received a question and he can not solve it, he'll ask his friends for help. Now , the Nias encounters a big problem, and he wants to the look for Selina's help. Given the network of Friends, please return the minimum number of meals Nias must offer. Of course Nias is lavish enough, so he'll pay for all the meals in the network of friends. inputthe first line of input contains an integer T, indicating the number of test cases (t<=30).For each test case, the first line contains integers, N and M represent the number of friends in the Nias ' s networ K and the number of relationships in that network. N and M are less than 100000 and can assume that 0 is Nias and n-1 is Selina.Next M Lines each contains the integers a and B, represent a relationship that a regards B as his friend, A and B is Between 0 and N-1. Outputfor each test case, pls output the minimum number of meals Nias need to offer; if Nias can ' t get Selina ' s help, pl Ease output-1.Sample Input
3 4 4 0 2 2 1 2 3  3 3 0 1 1 2 2 1 3  1 0 1
Sample Output
2 1-1
Tips Source Sample Program
#include <cstring> #include <bits/stdc++.h>using namespace std;const int n=1e5+10;const int INF =0x3f3f3f3f; int Low[n];int Dfn[n];int Instack[n];stack<int>s;int incomp[n];vector<int>gra[n];vector<int>    Newgra[n];int numcomp,cc;int n,m;void init () {memset (low,0,sizeof (Low));    memset (dfn,0,sizeof (DFN));    memset (instack,0,sizeof (instack));    numcomp=cc=0;    for (int i=0;i<n;i++) gra[i].clear ();        while (!s.empty ()) S.pop ();    int u,v;    scanf ("%d%d", &n,&m);        for (int i=0;i<m;i++) {scanf ("%d%d", &u,&v);    Gra[u].push_back (v);    }}void Tarjan (int u) {low[u]=dfn[u]=++cc;    S.push (U);    Instack[u]=1;    int len=gra[u].size ();    int V;        for (int i=0;i<len;i++) {v=gra[u][i]; if (!            Dfn[v]) {Tarjan (v);        Low[u]=min (Low[v],low[u]);    } else if (Instack[v]) low[u]=min (Low[u],low[v]); } if (Low[u]==dfn[u]) {numcomp++;            while (!s.empty ()) {v=s.top (), S.pop ();            Incomp[v]=numcomp;            instack[v]=0;        if (u==v) break;    }}}int dp (int s) {if (s==incomp[n-1]) return 0;    int ans=inf;    int len=newgra[s].size ();    for (int i=0;i<len;i++) ans=min (ANS,DP (Newgra[s][i]) +1); return ans;} void Solve () {for (int i=0;i<n;i++) {if (!    Dfn[i]) Tarjan (i);        } if (Incomp[0]==incomp[n-1]) {printf ("0\n");    return;    } int u,v;    for (int i=0;i<n;i++) newgra[i].clear ();        for (int i=0;i<n;i++) {int len=gra[i].size ();        U=incomp[i];            for (int j=0;j<len;j++) {v=incomp[gra[i][j]];            if (u!=v) {newgra[u].push_back (v);    }}} int gg=dp (incomp[0]);    if (gg==inf) printf (" -1\n"); else printf ("%d\n", GG);}    int main () {int t;    scanf ("%d", &t); While(t--)        {init ();    Solve (); } return 0;}



6th annual Shandong Province Tidal Cup Circle of Friends SDUT3262

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.