山东省第六届ACM省赛 Circle of Friends(tarjan缩点+dfs)

13 篇文章 0 订阅
13 篇文章 0 订阅

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.

Sample Input

3 
4 4 
0 1
1 2 
2 1 
2 3  

3 3 
0 1 
1 2 
2 1 

3 1 
0 1

Sample Output

2 
1 
-1

题目描述

在同一个强连通分量中的朋友请求帮忙不需要什么花费,否则需要请一顿饭.现在0节点位置的朋友要请教n-1节点位置的朋友,问最少需要请多少吨饭.

解题思路

强连通分量缩点之后再建图,然后dfs求0~n-1的最短距离.

代码实现

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
const int maxn = 1e5+7;
#define INF 0x3f3f3f3f
struct node
{
    int u;
    int v;
    int next;
} edge[maxn];
int head[maxn],dis[maxn],pre[maxn];
int dfn[maxn],sccno[maxn],low[maxn],sk[maxn];
bool vis[maxn];
int tot,cnt,indexx,scc_num,ans,n;
vector<int>vc[maxn];

void init()
{
    memset(head,-1,sizeof(head));
    memset(pre,-1,sizeof(pre));
    memset(dis,INF,sizeof(dis));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(sccno,-1,sizeof(sccno));
    tot=0,cnt=0,indexx=0,scc_num=0,ans=INF;
}

void addedge(int u,int v)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void tarjan(int u)
{
    dfn[u]=low[u]=++cnt;
    sk[++indexx]=u;
    vis[u]=1;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u])
    {
        ++scc_num;
        int t;
        do
        {
            t=sk[indexx];
            sccno[t]=scc_num;
            vis[sk[indexx]]=0;
        }
        while(u!=sk[indexx--]);
    }
}

void dfs(int point,int step)
{
    if(point==sccno[n-1])
        ans=min(ans,step);
    for(int i=0;i<(int)vc[point].size();i++)
        dfs(vc[point][i],step+1);
}

void solve(int n)
{
    for(int i=0; i<n; i++)
        if(!dfn[i])  tarjan(i);
    int a,b;
    for(int i=0;i<=scc_num;i++) vc[i].clear();
    for(int i=0;i<n;i++)
    {
        for(int j=head[i];j!=-1;j=edge[j].next)
        {
            a=sccno[edge[j].u],b=sccno[edge[j].v];
            if(a!=b)
                vc[a].push_back(b);
        }
    }
    dfs(sccno[0],0);
    if(ans==INF) cout<<"-1"<<endl;
    else cout<<ans<<endl;
}

int main()
{
    IO;
    int T,m;
    int u,v;
    cin>>T;
    while(T--)
    {
        init();
        cin>>n>>m;
        for(int i=0; i<m; i++)
        {
            cin>>u>>v;
            addedge(u,v);
        }
        solve(n);
    }
    return 0;
}
/*
2
8 11
0 1
1 2
2 3
3 2
3 4
4 3
4 6
6 4
6 7
1 5
5 6
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值