SDUT-3262-Circle of Friends(强连通分量)
来源:互联网 发布:现场平面布置图软件 编辑:程序博客网 时间:2024/05/29 17:55
Circle of Friends
Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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 11 2 2 1 2 3 3 3 0 1 1 2 2 1 3 1 0 1
示例输出
2 1 -1
提示
来源
模板题code:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <stack>using namespace std;#define MAXN 100005vector<int> tree[MAXN];vector<int> new_tree[MAXN];stack <int> stack_;int low[MAXN],DFN[MAXN],belong[MAXN];bool in_stack[MAXN];int top,dps,circle,flag,n;void Tarjan(int u){ int i,p; low[u]=DFN[u]=++dps; stack_.push(u); in_stack[u]=1; for(i=0; i<(int)tree[u].size(); ++i) { if(!DFN[tree[u][i]]) { Tarjan(tree[u][i]); low[u]=min(low[u],low[tree[u][i]]); } else if(in_stack[tree[u][i]] &&low[u]>DFN[tree[u][i]]) low[u]=min(low[u],DFN[tree[u][i]]); } if(low[u]==DFN[u]) { ++circle; do { p=stack_.top(); in_stack[p]=0; stack_.pop(); belong[p]=circle; } while(p!=u); }}int dfs(int u){ DFN[u]=1; if(u==belong[n-1]) { flag=1; return 0; } int ans=0x3f3f3f3f; for(int i=0; i<(int)new_tree[u].size(); ++i) ans=min(ans,dfs(new_tree[u][i])+1); return ans;}int main(){ int T,i,j,a,b,m; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(DFN,0,sizeof(DFN)); memset(in_stack,0,sizeof(in_stack)); for(i=0; i<n; ++i) tree[i].clear(); for(i=0; i<m; ++i) { scanf("%d%d",&a,&b); tree[a].push_back(b); } circle=dps=0; for(i=0; i<n; ++i) if(!DFN[i]) Tarjan(i); for(i=0; i<=circle; ++i) new_tree[i].clear(); for(i=0; i<n; ++i) for(j=0; j<(int)tree[i].size(); ++j) if(belong[i]!=belong[tree[i][j]]) new_tree[belong[i]].push_back(belong[tree[i][j]]); flag=0; int ans=dfs(belong[0]); if(flag)printf("%d\n",ans); else printf("-1\n"); } return 0;}
0 0
- SDUT-3262-Circle of Friends(强连通分量)
- sdut 3262 Circle of Friends (SCC缩点) 有向图 强连通分量模板
- 山东省第六届ACM省赛题——Circle of Friends(强连通分量+dfs)
- SDUT 3262 Circle of Friends【强连通Tarjan+缩点染色+最短路SPFA】
- sdut 3262 Circle of Friends tarjan
- sdut 3262 Circle of Friends (SCC缩点)
- SDUT-2170-The Largest SCC(强连通分量)
- poj2553-The Bottom of a Graph(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- poj 1236 Network of Schools(强连通分量 Tarjan)
- poj 1236 Network of Schools(强连通分量)
- POJ-1236-Network of Schools(强连通分量)
- POJ-1236 Network of Schools (强连通分量[Tarjan])
- POJ 1236 Network of Schools (强连通分量tarjan)
- poj1236 Network of Schools(强连通分量,tarjan)
- Matlab---在多个axes之间切换
- JavaScript学习之路——分页类
- 我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
- String to Integer---8
- OC中的数组、字典和集合解析
- SDUT-3262-Circle of Friends(强连通分量)
- 顺序表学习
- Encoding options for H.264 video--采用H264编码视频的参数设定
- LEETCODE 278
- upupw kangle 504 错误调试 服务器拒绝了链接"
- ios an ssl error has occurred and a secure
- ZJNU - 1707 TOPOVI
- HttpClient读取JSON数据
- AngularJs 自定义filter