2013年第四届蓝桥杯CC++ A组国赛 —— 第五题:网络寻路

    xiaoxiao2022-06-27  213

    标题:网络寻路

    X 国的一个网络使用若干条线路连接若干个节点。节点间的通信是双向的。某重要数据包,为了安全起见,必须恰好被转发两次到达目的地。该包可能在任意一个节点产生,我们需要知道该网络中一共有多少种不同的转发路径。

    源地址和目标地址可以相同,但中间节点必须不同。

    如图1所示的网络。 1 -> 2 -> 3 -> 1 是允许的

    1 -> 2 -> 1-> 2 或者 1->2->3->2 都是非法的。

    输入数据的第一行为两个整数N M,分别表示节点个数和连接线路的条数(1<=N<=10000; 0<=M<=100000)。 接下去有M行,每行为两个整数 u 和 v,表示节点u 和 v 联通(1<=u,v<=N , u!=v)。

    输入数据保证任意两点最多只有一条边连接,并且没有自己连自己的边,即不存在重边和自环。

    输出一个整数,表示满足要求的路径条数。

    例如: 用户输入: 3 3 1 2 2 3 1 3 则程序应该输出: 6

    再例如: 用户输入: 4 4 1 2 2 3 3 1 1 4 则程序应该输出: 10

    资源约定: 峰值内存消耗 < 64M CPU消耗 < 1000ms

    请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

    注意: main函数需要返回0 注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。 注意: 所有依赖的函数必须明确地在源文件中 #include , 不能通过工程设置而省略常用头文件。

    提交时,注意选择所期望的编译器类型(千万不要混淆c和cpp)。

    Code

    /* ^....0 ^ .1 ^1^ .. 01 1.^ 1.0 ^ 1 ^ ^0.1 1 ^ ^..^ 0. ^ 0^ .0 1 .^ .1 ^0 .........001^ .1 1. .111100....01^ 00 11^ ^1. .1^ 1.^ ^0 0^ .^ ^0..1 .1 1..^ 1 .0 ^ ^ 00. ^^0.^ ^ 0 ^^110.^ 0 0 ^ ^^^10.01 ^^ 10 1 1 ^^^1110.1 01 10 1.1 ^^^1111110 010 01 ^^ ^^^1111^1.^ ^^^ 10 10^ 0^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ectives: Variables required by the program: Procedural thinking: Functions required by the program: Determination algorithm: Determining data structure: */ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first gentle breeze that passed through my ear is you, The first star I see is also you. The world I see is all your shadow." FIGHTING FOR OUR FUTURE!!! */ #include <vector> #include <cstring> #include <iostream> #define maxn 10007 using namespace std; int N,M; long long ans; bool vis[maxn]; vector <int> G[maxn]; void dfs(int start,int step,int score){ if(step==3){ for(int i=0;i<G[start].size();i++){ int v=G[start][i]; if(!vis[v]||v==score) ans++; } return; } else { for(int i=0;i<G[start].size();i++){ int v=G[start][i]; if(!vis[v]){ vis[v]=true; dfs(v,step+1,score); vis[v]=false; } } } return; } int main(){ cin>>N>>M; memset(vis,0,sizeof(vis)); for(int i=0;i<M;i++){ int u,v; cin>>u>>v; G[u].push_back(v); G[v].push_back(u); } for(int i=1;i<N+1;i++){ vis[i]=true; dfs(i,1,i); vis[i]=false; } cout<<ans<<endl; return 0; } Alex 007 认证博客专家 机器学习 NLP TensorFlow 我是 Alex 007,一个热爱计算机编程和硬件设计的小白。为啥是007呢?因为叫 Alex 的人太多了,再加上每天007的生活,Alex 007就诞生了。如果你喜欢我的文章的话,给个三连吧!

    最新回复(0)