第6届山东省浪潮杯 Circle of Friends SDUT3262
2016-03-12 10:46
330 查看
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
首先找出强连通分量,然后把每个强连通分量缩成一个点,得到一个DAG,再利用动态规划求DAG的最长路径问题
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
首先找出强连通分量,然后把每个强连通分量缩成一个点,得到一个DAG,再利用动态规划求DAG的最长路径问题
#include<iostream> #include<stack> #include<cstring> #include<cstdlib> #include<vector> #include<algorithm> #define inf 9999999 using namespace std; struct Edge { int to; int next; }edge[100020];//记录边的情况 stack<int> s; vector<int> g[100020];//存储DAG的边集 int dfn[100020];//记录节点u第一次被访问时的步数 int low[100005];//记录与节点u和u的子树节点中最早的步数 int is_stack[100020];//标记是否在stack中 int head[100020];//链式前向星用来记录的结点 int belong[100020];////各顶点属于哪个强连通分量 int n,m,tp,p,num;//n:点的个数;m:边的条数,tp用来记录深搜时的访问时间,p用来记录栈里面的个数,num有多少个强连通分量 void tarjan(int u) { dfn[u]=low[u]=++tp; s.push(u); is_stack[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(!dfn[v]) { tarjan(v); low[u]=min(low[v],low[u]); } else if(is_stack[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { num++; while(!s.empty()) { int v=s.top(); s.pop(); is_stack[v]=0; belong[v]=num; if(dfn[v]==low[u]) break; } } } int dp(int v) { if(v==belong[n-1]) return 0; int minx=inf; for(int j=0;j<g[v].size();j++) minx=min(minx,dp(g[v][j])+1); return minx; } int main() { int T; cin>>T; while(T--) { while(cin>>n>>m) { memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(head,-1,sizeof(head)); memset(is_stack,0,sizeof(is_stack)); tp=0,p=0,num=0; int k=0;//边的计数器 for(int i=0;i<=m-1;i++) { int a,b; cin>>a>>b;//顶点为1~n edge[i].to=b; edge[i].next=head[a]; head[a]=i; } for(int i=0;i<=n-1;i++) if(!dfn[i]) tarjan(i); for(int i=0;i<=n-1;i++)//将强连通分量所称一个点,得到DAG图 g[i].clear(); for(int i=0;i<=n-1;i++) for(int j=head[i];j!=-1;j=edge[j].next) { if(belong[edge[j].to]!=belong[i]) g[belong[i]].push_back(belong[edge[j].to]); } int x=dp(belong[0]);//利用动态规划求DAG的最长路径 if(x==inf) cout<<"-1"<<endl; else cout<<x<<endl; } } return 0; }
相关文章推荐
- 进程优先级
- QML中定位器和布局的区别
- JPA 各种实体锁模式的区别
- 今日BBC
- iOS开发学习笔记
- 大数相关计算(c语言版)
- Linux_ 文本处理三剑客中的“大宝剑“―Grep家族
- 安卓注册手机短信验证码验证的案例-01
- Objective-c 字典对象
- 三个水杯 (bfs)
- 封装控件的原理[未完待续]
- Codeforces 631D Messenger【KMP】
- Train and Test LeNet on your own dataset
- Codeforces 631D Messenger【KMP】
- 排序算法
- 面向对象的内存调用
- Cookie 与Session 的区别(转载)
- Javascript笔记一
- 获得客户端详细信息以及每个进程的sql语句
- 2016-3-12 linux基础学习——字符串比较、循环