码迷,mamicode.com
首页 > 其他好文 > 详细

第6届山东省浪潮杯 Circle of Friends SDUT3262

时间:2016-05-06 12:36:48      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

Circle of Friends

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

强连通缩点+BFS(找最短距离);

题目描述

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

提示

 

来源

 

示例程序

#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;
}



第6届山东省浪潮杯 Circle of Friends SDUT3262

标签:

原文地址:http://blog.csdn.net/lv414333532/article/details/51329786

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!