Circle of Friends

xiaoxiao2021-02-28  110

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 1 1 2 2 1 2 3 3 3 0 1 1 2 2 1 3 1 0 1

Example Output

2 1 -1

题意就是n个点,m条单向边,u→v,表示u可以请v来帮忙。这里有个设定就是,如果朋友之间出现了环,那么这些朋友请求帮助的时候没有花费,否则花费为1.

思路:很容易想到强连通缩点,然后求个0-n-1的一个步数。

#include<bits/stdc++.h> using namespace std; const int MAXN=1e5+7; int n,m; vector<int>head[MAXN],G[MAXN]; int dfn[MAXN],sccno[MAXN],sum[MAXN],dfs_clock,scc_cnt; stack<int>S; int dfs(int u) { int lowu=dfn[u]=++dfs_clock; S.push(u); for(int i=0,l=head[u].size();i<l;++i) { int v=head[u][i]; if(!dfn[v]) { int lowv=dfs(v); lowu=min(lowu,lowv); } else if(!sccno[v]) { lowu=min(lowu,dfn[v]); } } if(lowu==dfn[u]) { scc_cnt++; while(1) { int x=S.top(); S.pop(); sccno[x]=scc_cnt; if(x==u)break; } } return lowu; } void find_scc() { dfs_clock=scc_cnt=0; fill(dfn,dfn+n,0); fill(sccno,sccno+n,0); for(int i=0;i<n;++i)if(!dfn[i])dfs(i); } bool vis[MAXN]; int step[MAXN]; int s,e; int bfs() { fill(vis+1,vis+1+scc_cnt,0); s=sccno[0]; e=sccno[n-1]; queue<int>q; q.push(s); step[s]=0; vis[s]=1; while(!q.empty()) { int u=q.front(); q.pop(); if(u==e)return step[e]; for(int i=0,l=G[u].size();i<l;++i) { int v=G[u][i]; if(!vis[v]) { vis[v]=1; step[v]=step[u]+1; q.push(v); } } } return -1; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<n;++i)head[i].clear(); int u,v; while(m--) { scanf("%d%d",&u,&v); head[u].push_back(v); } find_scc(); fill(sum+1,sum+1+scc_cnt,0); for(int i=0;i<n;++i)sum[sccno[i]]++; for(int i=1;i<=scc_cnt;++i)G[i].clear(); for(int i=0;i<n;++i) for(int j=0,l=head[i].size();j<l;++j) { u=sccno[i]; v=sccno[head[i][j]]; if(u!=v)G[u].push_back(v); } printf("%d\n",bfs()); } }

转载请注明原文地址: https://www.6miu.com/read-37709.html

最新回复(0)