山东省第六届ACM省赛题——Circle of Friends(强连通分量+dfs)
来源:互联网 发布:红蜘蛛端口号怎么修改 编辑:程序博客网 时间:2024/05/24 08:34
题目描述
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 1
1 2
2 1
2 3
3 3
0 1
1 2
2 1
3 1
0 1
示例输出
2
1
-1
题意就是缩点+建图找最短路,坑点嘛。。很多
#include <iostream>#include <algorithm>#include <cstring>#include <cmath>#include <cstdio>#include <set>#include <vector>#include <iomanip>#include <stack>//#include <map>#include <queue>#define MAXN 100010#define mod 9973#define INF 0x3f3f3f3fusing namespace std;vector<int> g[MAXN];struct Node{ int x,y,next;} edge[MAXN*100];int dfn[MAXN],low[MAXN],vis[MAXN],s[MAXN],belong[MAXN],head[MAXN];int n,tot=0,ssum=0,ans=0,times=0,t=0,ans2;void init(){ memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(vis,0,sizeof(vis)); memset(low,0,sizeof(low)); memset(belong,0,sizeof(belong)); tot=0,ssum=0,ans=0,times=0,t=0;}void add(int x,int y){ edge[tot].x=x; edge[tot].y=y; edge[tot].next=head[x]; head[x]=tot++;}void tarjan(int x){ int y,i; times++; t++; dfn[x]=low[x]=times; vis[x]=1; s[t]=x; for (i=head[x]; i!=-1; i=edge[i].next) { y=edge[i].y; if (vis[y]==0) { tarjan(y); low[x]=min(low[x],low[y]); } if (vis[y]==1) low[x]=min(low[x],dfn[y]); } if (dfn[x]==low[x]) { ssum++; do { y=s[t--]; belong[y]=ssum; vis[y]=2; } while (y!=x); }}bool flag;int aans;void dfs(int x,int st){ if(x==belong[n-1]) { flag=true; aans=aans<st?aans:st; } for(int i=0; i<g[x].size(); ++i) { int y=g[x][i]; dfs(y,st+1); }}int main(){ ios::sync_with_stdio(false); int t,m,i,j,u,v; cin>>t; while(t--) { init(); cin>>n>>m; for( i=0; i<m; i++) { cin>>u>>v; add(u,v); } for(i=0; i<n; ++i) if(!vis[i]) tarjan(i); int x,y; for(i=0; i<=MAXN; ++i) g[i].clear(); for( i=0; i<n; i++) for (j=head[i]; j!=-1; j=edge[j].next) if(belong[edge[j].x]!=belong[edge[j].y]) { x=belong[edge[j].x],y=belong[edge[j].y]; //cout<<"x="<<x<<" "<<"y="<<y<<endl; g[x].push_back(y); } flag=false; aans=INF; dfs(belong[0],0); if(flag) cout<<aans<<endl; else cout<<"-1"<<endl; }}
- 山东省第六届ACM省赛题——Circle of Friends(强连通分量+dfs)
- SDUT-3262-Circle of Friends(强连通分量)
- sdut 3262 Circle of Friends (SCC缩点) 有向图 强连通分量模板
- 山东省第六届ACM省赛题——BIGZHUGOD and His Friends II(塞瓦定理,二分求解方程)
- 山东第六届省赛Circle of Friends(强联通缩点最短路)
- 第6届山东省浪潮杯 Circle of Friends SDUT3262
- 第6届山东省浪潮杯 Circle of Friends SDUT3262
- 山东省第五届ACM省赛题——Circle(递推求概率)
- poj2186——双dfs求强连通分量
- 第五届山东省ACM Circle(高斯消元)
- SDUT 3262 Circle of Friends【强连通Tarjan+缩点染色+最短路SPFA】
- 山东省第四届ACM大学生程序设计竞赛 Thrall’s Dream(单源强连通分量)
- [ACM] poj 1236 Network of Schools (有向强连通分量)
- poj1236——Network of Schools(加最少边组成强连通分量)
- poj2553——The Bottom of a Graph(强连通分量)
- POJ 2553——The Bottom of a Graph(强连通分量)
- poj1236——Network of Schools(强连通分量+缩点)
- hdu 5222(Tarjan求强连通分量+dfs)
- 《图解HTTP》学习笔记(四)-返回结果的HTTP状态码
- unity3d 动作会产生抖动的问题
- Ajax异步交互解决数据乱码问题
- READING NOTE: Chained Predictions Using Convolutional Neural Networks
- 检查是否为BST
- 山东省第六届ACM省赛题——Circle of Friends(强连通分量+dfs)
- 异步请求后台json传回前台406
- 【论文】New baseline correction algorithm for text-line recognition with bidirectional recurrent neural
- 接口与抽象类的理解日志
- VC++ 通过INI配置文件,实现多国语言切换界面
- 集线器与网桥的原理和作用
- 九度 OJ 1065:输出梯形
- spark快速大数据分析之数据读取与保存
- unix重启系统调用解析