SDUT 3262 Circle of Friends【强连通Tarjan+缩点染色+最短路SPFA】

来源:互联网 发布:为知笔记活动 编辑:程序博客网 时间:2024/05/20 20:03

Circle of Friends

Time Limit: 2000MS Memory Limit: 65536KB
Submit Statistic Discuss

Problem Description

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.

Input

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.

Output

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.

Example Input

3 4 4 0 11 2 2 1 2 3  3 3 0 1 1 2 2 1  3 1 0 1

Example Output

2 1 -1

Hint

Author

 “浪潮杯”山东省第六届ACM大学生程序设计竞赛

题目大意:给你N个点,M个关系,关系A B,表示A认识B.

如果A认识B而且B认识A.那么如果现在我是A我想找到B就不需要别人帮我介绍我自然认识.

如果A认识B但是B不认识A.那么我们现在就需要花费1单位来认识B.

现在让你求0想要认识n-1的最小花费。


思路:


很显然的最短路问题,问题关键在于对于有向环的处理。

如果我们现在是一个DAG图,那么很显然就不需要考虑环的问题了。

那么我们预处理图将其变成一个DAG图即可,这里Tarjan强连通缩点染色很方便就处理出来了。

问题很裸,不需要太多思考过程,主要是代码实现熟练程度问题。


Ac代码:

#include<stdio.h>#include<string.h>#include<queue>#include<vector>using namespace std;vector<int >mp2[150000];vector<int >mp[150000];int stack[100800];int vis[100800];int low[100800];int color[100800];int dfn[100800];int dist[100800];int n,m,cnt,sig,tt;void Tarjan(int u){    vis[u]=1;    low[u]=dfn[u]=cnt++;    stack[++tt]=u;    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i];        if(vis[v]==0)        {            Tarjan(v);        }        if(vis[v]==1)low[u]=min(low[u],low[v]);    }    if(dfn[u]==low[u])    {        sig++;        do        {            color[stack[tt]]=sig;            low[stack[tt]]=sig;            vis[stack[tt]]=2;        }while(stack[tt--]!=u);    }}void SPFA(int ss){    memset(vis,0,sizeof(vis));    for(int i=0;i<=n;i++)dist[i]=0x3f3f3f3f;    queue<int >s;    s.push(ss);    dist[ss]=0;    while(!s.empty())    {        int u=s.front();        s.pop();vis[u]=0;        for(int i=0;i<mp2[u].size();i++)        {            int v=mp2[u][i];            if(dist[v]>dist[u]+1)            {                dist[v]=dist[u]+1;                if(vis[v]==0)                {                    vis[v]=1;                    s.push(v);                }            }        }    }    int ennd=color[n-1];    if(dist[ennd]==0x3f3f3f3f)printf("-1\n");    else printf("%d\n",dist[ennd]);}void Slove(){    cnt=1;    sig=0,tt=-1;    for(int i=0;i<=n;i++)mp2[i].clear();    memset(vis,0,sizeof(vis));    for(int i=0;i<n;i++)    {        if(vis[i]==0)        {            Tarjan(i);        }    }    for(int i=0;i<n;i++)    {        for(int j=0;j<mp[i].size();j++)        {            int u=color[i];            int v=color[mp[i][j]];            if(u!=v)            {             //   printf("--%d %d\n",u,v);                mp2[u].push_back(v);            }        }    }    int root=color[0];    SPFA(root);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=0;i<=n;i++)mp[i].clear();        for(int i=0;i<m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            mp[x].push_back(y);        }        Slove();    }}








0 0