题目描述:
试题编号: 201709-4
试题名称: 通信网络
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述某国的军队由N个部门组成,为了提高安全性,部门之间建立了M条通路,每条通路只能单向传递信息,即一条从部门a到部门b的通路只能由a向b传递信息。信息可以通过中转的方式进行传递,即如果a能将信息传递到b,b又能将信息传递到c,则a能将信息传递到c。一条信息可能通过多次中转最终到达目的地。>
由于保密工作做得很好,并不是所有部门之间都互相知道彼此的存在。只有当两个部门之间可以直接或间接传递信息时,他们才彼此知道对方的存在。部门之间不会把自己知道哪些部门告诉其他部门。
上图中给了一个4个部门的例子,图中的单向边表示通路。部门1可以将消息发送给所有部门,部门4可以接收所有部门的消息,所以部门1和部门4知道所有其他部门的存在。部门2和部门3之间没有任何方式可以发送消息,所以部门2和部门3互相不知道彼此的存在。
现在请问,有多少个部门知道所有N个部门的存在。或者说,有多少个部门所知道的部门数量(包括自己)正好是N。
输入格式
输入的第一行包含两个整数N, M,分别表示部门的数量和单向通路的数量。所有部门从1到N标号。
接下来M行,每行两个整数a, b,表示部门a到部门b有一条单向通路。
输出格式
输出一行,包含一个整数,表示答案。
样例输入
4 4
1 2
1 3
2 4
3 4
样例输出
2
样例说明
部门1和部门4知道所有其他部门的存在。
评测用例规模与约定
对于30%的评测用例,1 ≤ N ≤ 10,1 ≤ M ≤ 20;
对于60%的评测用例,1 ≤ N ≤ 100,1 ≤ M ≤ 1000;
对于100%的评测用例,1 ≤ N ≤ 1000,1 ≤ M ≤ 10000。
题解:
看到这道题我一开始的做法是直接双向dfs:
比如说我们看样例:
首先正向对每个点dfs一遍:
从1开始,搜到2,4,3
从2开始,搜到4
从3开始,搜到4
从4开始,啥都搜不到
然后反向对每个点dfs一遍:
从1开始,啥都都不到
从2开始,搜到1
从3开始,搜到1
从4开始,搜到2,1,3统计一下
点1可以与其他的4个点有联系 sum++
点2只能与4,1有联系
点3只能与4,1有联系
点4可以与其他四个点有联系 sum++
所以sum=2,ok。
我试了几个样例,好像都没错,但是最后的得分只有10分,时间是>1s,应该是超时了。
10分代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e3+5;
int n,m;
int g1[MAX][MAX];// 正向图
int g2[MAX][MAX];// 反向图
int flag[MAX];// 点的访问标记
void dfs_g1(int a)
{
//printf("正在dfs正向图,此时位于:%d\n",a);
for(int b = 1; b <= n; b++)
{
if(b == a) continue;
if(g1[a][b] && !flag[b])
{
flag[b] = 1;
dfs_g1(b);
//flag[b] = 0;
}
}
}
void dfs_g2(int a)
{
//printf("正在dfs反向图,此时位于:%d\n",a);
for(int b = 1; b <= n; b++)
{
if(b == a) continue;
if(g2[a][b] && !flag[b])
{
flag[b] = 1;
dfs_g2(b);
//flag[b] = 0;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(cin >> n >> m)
{
memset(g1,0,sizeof(g1));
memset(g2,0,sizeof(g1));
int a,b;
for(int i = 1; i <= m; i++)
{
cin >> a >> b;
g1[a][b] = 1;
g2[b][a] = 1;
}
int sum = 0;
for(int a = 1; a <= n; a++)
{
int ans = 0;
for(int i = 1; i <= n; i++)
{
flag[i] = 0;
}
flag[a] = 1;
dfs_g1(a);
/* 统计从点a正向dfs可以遍历到几个点
ans = 0;
for(int i = 1; i <= n; i++)
{
if(flag[i])
{
ans++;
}
}
print("%d\n",ans);*/
dfs_g2(a);
ans = 0;
for(int i = 1; i <= n; i++)// 统计在从点a正向一次dfs的基础上,又从点a反向一次dfs,可以经过多少个点
{
if(flag[i])
{
ans++;
}
}
//printf("反向dfs后,ans = %d\n",ans);
if(ans == n)
{
sum++;
}
}
cout << sum << endl;
}
return 0;
}
我还没有考虑去怎么优化,直接参考了别人的博客。
思路:
对n个点进行一次dfs,用一个二维数组记录下 与起点 有联系的点(也就是dfs可以到的点),比如起点s和终点e有联系(正向dfs可以到达的),那么终点e和起点s就也有联系(反向dfs可以到达的),直接标记connect[s][e] = connect[e][s] = 1就好了,所以就不用做两遍dfs了,最后统计一下每个点相关联的点如果有n个(包括自己),就sum++。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e3+5;
int n,m;
vector <int> g[MAX];
int connect[MAX][MAX];
int vis[MAX];
void dfs(int s,int cur)// s-起点 cur-当前点
{
vis[cur] = 1;
connect[s][cur] = connect[cur][s] = 1;// s点认识cur点,同时cur点也认识s点
for(int i = 0; i < (int)g[cur].size(); i++)// 遍历当前点cur的相邻点
{
if(!vis[g[cur][i]])
{
// 当前点cur没有被访问,可以从当前点cur的相邻点g[cur][i]继续往下访问
dfs(s,g[cur][i]);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(cin >> n >> m)
{
memset(connect,0,sizeof(connect));
int a,b;
for(int i = 1; i <= m; i++)
{
cin >> a >> b;
g[a].push_back(b);// 记录下a相邻的所有点
}
for(int i = 1; i <= n; i++)
{
memset(vis,0,sizeof(vis));
dfs(i,i);// dfs(起点,当前点)
}
int sum = 0;
for(int i = 1; i <= n; i++)
{
int tmp = 0;
for(int j = 1; j <= n; j++)
{
if(connect[i][j])
{
tmp++;
}
}
if(tmp == n)
{
sum++;
}
}
cout << sum << endl;
}
return 0;
}