sdut 3262 Circle of Friends tarjan

Circle of Friends

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Nowadays, "Circle of Friends" is a very popular social networking platform in WeChat. We can share our life to 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 will generally come to help and ask nothing for return.
However, the friendship above is true friend relationship while sometimes you may regard someone as your friend but he doesn't agree.In this way when you ask him for help, he often asks you for a meal, and then he will help you.
If two people think they are friends mutually,they will become true friend,then once one of them has a problem or makes a query, the other one will offer 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”,they will make a friends circle and become true friends too with each other. Besides, people will 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 will ask his friends for help. 
Now, Nias encounters a big problem, and he wants to 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 will pay for all the meals in the network of friends.
 

输入

 The first line of input contains an integer T, indicating the number of test cases (T<=30).
For each test case, the first line contains two integers, N and M represent the number of friends in the Nias’s network and the number of relationships in that network. N and M are less than 100000 and you can assume that 0 is Nias and n-1 is Selina.
Next M lines each contains two integers A and B, represent a relationship that A regards B as his friend, A and B are between 0 and n-1.
 

输出

 For each test case, please output the minimum number of meals Nias need to offer; if Nias can’t get Selina’s help, please output -1.

示例输入

3 
4 4 
0 1
1 2 
2 1 
2 3  

3 3 
0 1 
1 2 
2 1 
 
3 1 
0 1

示例输出

2 
1 
-1

提示

 

来源

 

示例程序

 

  • 提交 
  • 状态 
  • 讨论
    • 缩点后新建一个图在跑一发dfs就好了
    • ACcode:
    #include 
    #include 
    #include 
    #include 
    #define maxn 100100
    using namespace std;
    inline void Scan(int &x) {
          char c;while((c=getchar())<'0' || c>'9');x=c-'0';
          while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
    }
    struct Edge{int to,next;}my[maxn];
    Edge myy[maxn];
    int head[maxn],tot;
    int head2[maxn],tot2;
    void init(){memset(head,-1,sizeof(head));tot=0;memset(head2,-1,sizeof(head2));tot2=0;}
    inline void add(int u,int v){my[tot].to=v;my[tot].next=head[u];head[u]=tot++;}
    inline void add2(int u,int v){myy[tot2].to=v;myy[tot2].next=head2[u];head2[u]=tot2++;}
    int low[maxn],dfn[maxn],sta[maxn],belong[maxn];
    int scc,Index,top;
    bool insta[maxn];
    void tarjan(int u){
        int v;
        low[u]=dfn[u]=++Index;
        sta[top++]=u;
        insta[u]=1;
        for(int i=head[u];i!=-1;i=my[i].next){
            v=my[i].to;
            if(!dfn[v]){
                tarjan(v);
                low[u]=low[u]>low[v]?low[v]:low[u];
            }
            else if(insta[v]&&low[u]>dfn[v])
                low[u]=dfn[v];
        }
        if(low[u]==dfn[u]){
            scc++;
            do{
                v=sta[--top];
                insta[v]=false;
                belong[v]=scc;
            }
            while(v!=u);
        }
    }
    int n,m,out;
    void create(){
        for(int i=0;i


你可能感兴趣的:(图论一顿套模板,图的连通性,省赛真题)