zoukankan      html  css  js  c++  java
  • 蓝桥杯 历届试题 网络寻路

      历届试题 网络寻路  
    时间限制:1.0s   内存限制:256.0MB
          
    问题描述

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

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

    如下图所示的网络。

    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)。

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

    输出格式
    输出一个整数,表示满足要求的路径条数。
    样例输入1
    3 3
    1 2
    2 3
    1 3
    样例输出1
    6
    样例输入2
    4 4
    1 2
    2 3
    3 1
    1 4
    样例输出2
    10
     
     
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 #define size 10000
     8 struct Edge{
     9     int to,next,val;
    10 };
    11 Edge e[size*10+5];
    12 int h[size+5];
    13 int sum;
    14 void dfs(int b,int a,int f){
    15     if(f==3){
    16         sum++;
    17         return;
    18     }
    19     int i;
    20     for(i=h[a];i+1;i=e[i].next){
    21         if(e[i].to!=b){
    22            dfs(a,e[i].to,f+1);
    23         }
    24     }
    25 }
    26 int main(){
    27     //freopen("D:\input.txt","r",stdin);
    28     int n,m;
    29     scanf("%d%d",&n,&m);
    30     int i,num=0,a,b;
    31     memset(h,-1,sizeof(h));
    32     for(i=0;i<m;i++){
    33         scanf("%d %d",&a,&b);
    34 
    35         e[num].to=b;
    36 
    37         e[num].next=h[a];
    38         h[a]=num++;
    39 
    40         e[num].to=a;
    41 
    42         e[num].next=h[b];
    43         h[b]=num++;
    44     }
    45     sum=0;
    46     for(i=1;i<=n;i++){
    47         dfs(i,i,0);
    48     }
    49     cout<<sum<<endl;
    50     return 0;
    51 }
  • 相关阅读:
    an optimal solution to the problem
    sql_action
    rgb转灰度 RGB To Gray php Adobe RGB (1998) [gamma=2.20]
    MTUTCP/IP协议栈linux kernelTCP丢包重传UDP高性能AI ip数据报 tcp数据报
    Metaheuristic
    a computercentered view of information systems to a databasecentered view
    算法 图像识别 搜索
    var wi = 0; wi < arr.length; wi++
    模拟信号 数字信号
    locations in main memory to be referenced by descriptive names rather than by numeric addresses
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4403626.html
Copyright © 2011-2022 走看看