Circle of Friends
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
Hint
Source
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d\n", a) #define plld(a) printf("%lld\n", a) #define pc(a) printf("%c\n", a) #define ps(a) printf("%s\n", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 200100, INF = 0x7fffffff; int n, m, s, t; vector<int> G[maxn]; int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt; stack<int> S; int nex[maxn << 1], head[maxn], cnt; void dfs(int u) { pre[u] = low[u] = ++dfs_clock; S.push(u); for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(!pre[v]) { dfs(v); low[u] = min(low[u], low[v]); } else if(!sccno[v]) low[u] = min(low[u], pre[v]); } if(low[u] == pre[u]) { scc_cnt++; for(;;) { int x = S.top(); S.pop(); sccno[x] = scc_cnt; if(x == u) break; } } } struct node { int u, v; }Node[maxn << 1]; void add(int u, int v) { Node[cnt].u = u; Node[cnt].v = v; nex[cnt] = head[u]; head[u] = cnt++; } int mx; bool flag; void dfs2(int u, int d) { // cout << u << endl; d += 1; if(u == sccno[n - 1]) { flag = 1; mx = min(mx, d); return; } for(int i = head[u]; i != -1; i = nex[i]) { int v = Node[i].v; // cout << v << endl; dfs2(v, d); } } int main() { int T; rd(T); while(T--) { mem(sccno, 0); mem(pre, 0); mem(head, -1); cnt = 0; flag = 0; mx = INF; dfs_clock = scc_cnt = 0; rd(n), rd(m); for(int i = 0; i < n; i++) G[i].clear(); for(int i = 0; i < m; i++) { int u, v; rd(u), rd(v); G[u].push_back(v); } for(int i = 0; i < n; i++) if(!pre[i]) dfs(i); for(int i = 0; i < n; i++) for(int j = 0; j < G[i].size(); j++) { int v = G[i][j]; if(sccno[i] != sccno[v]) add(sccno[i], sccno[v]); } s = sccno[0]; // cout << s << endl; dfs2(s, -1); if(flag) pd(mx); else printf("-1\n"); } return 0; }