我是靠谱客的博主 知性星星,最近开发中收集的这篇文章主要介绍山东省第六届ACM省赛 Circle of Friends(tarjan缩点+dfs),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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
*/

最后

以上就是知性星星为你收集整理的山东省第六届ACM省赛 Circle of Friends(tarjan缩点+dfs)的全部内容,希望文章能够帮你解决山东省第六届ACM省赛 Circle of Friends(tarjan缩点+dfs)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部