I - Walk of Three

该博客讨论了一道关于图论的算法题,题目涉及一个公园内的草坪和路径。Vasya只能在与入口草坪相邻的草坪上玩耍,他每天要选择一条之前未走过的、包含三条不同路径的新路线。作者通过编写AC代码,将map容器改为数组以优化性能,最终解决了超时问题。博客主要探讨了如何利用深度优先搜索(DFS)来计算所有可能的路径,并强调了STL中某些操作可能不如数组高效。

The city where Vasya lives has a park with n lawns connected by m

paths. One can walk in both directions along each path. The lawns connected by the path are called neighbors.

The entrance to the park is near the lawn number one which will be called the entrance lawn. Vasya's parents are very concerned about his safety, so they allow him to play only on the lawn that is a neighbor to the entrance lawn. Entrance lawn is usually overcrowded, so Vasya can't play on it.

Vasya finds it boring to simply walk along the path to the neighbor lawn. Instead, he starts at the entrance lawn, and walks along exactly three different paths. After that he plays on the lawn where he ends his walk. Vasya does not break the rules set by the parents, so he always ends his walk on the lawn neighboring to the entrance lawn.

Every day Vasya wants to choose a new walk he hasn't taken before. Help him to determine how many ways are to begin his journey at the entrance lawn, follow exactly three different paths, and find himself on the lawn neighboring to the entrance lawn.

Input

The first line of input contains two integers n

and m — the number of lawns and the number of paths, respectively (1≤n≤100000, 1≤m≤200000

).

The next m

lines contain pairs of lawns connected by paths. Any two lawns are connected by no more than one path. There are no paths connecting a lawn to itself.

Output

Print the number of walks that Vasya can take.
ac代码
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define ll long long
using namespace std;
const int N = 1e5+10;
int n,m,num;
vector<int>g[N];
int mp[101000];
int vis[N];

void dfs(int pos,int k)
{
    if(k==0)
    {
        if(mp[pos])
        {
            num++;
        }
        return;
    }
    int len=g[pos].size();
    for(int i=0; i<len; i++)
    {
        if(vis[g[pos][i]]==0)
        {
            vis[g[pos][i]] = 1;
            dfs(g[pos][i],k-1);
            vis[g[pos][i]] = 0;
        }
    }
    return;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
        if(u==1)
            mp[v] = 1;
        if(v==1)
            mp[u] = 1;
    }
    vis[1] = 1;
    dfs(1,3);
    printf("%d\n",num);
    return 0;
}

昨天训练赛的一道题,,,,,,打了7、8次硬是超时,以为是剪枝还不够,午觉起来。。。。又敲,,把map容器改为了数组存储,ac!!!!难道stl里的函数还没有数组寸的更优秀吗。。

#ifndef DUBINS_H #define DUBINS_H // KEY!!!!!!!!!!! use c in c++, you need to use below code #ifndef __MATLAB__ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #endif /* __MATLAB__ */ typedef enum { LSL = 0, LSR = 1, RSL = 2, RSR = 3, RLR = 4, LRL = 5 } DubinsPathType; typedef struct { /* the initial configuration */ double qi[3]; /* the lengths of the three segments */ double param[3]; /* model forward velocity / model angular velocity */ double rho; /* the path type described */ DubinsPathType type; } DubinsPath; #define EDUBOK (0) /* No error */ #define EDUBCOCONFIGS (1) /* Colocated configurations */ #define EDUBPARAM (2) /* Path parameterisitation error */ #define EDUBBADRHO (3) /* the rho value is invalid */ #define EDUBNOPATH (4) /* no connection between configurations with this word */ /** * Callback function for path sampling * * @note the q parameter is a configuration * @note the t parameter is the distance along the path * @note the user_data parameter is forwarded from the caller * @note return non-zero to denote sampling should be stopped */ typedef int (*DubinsPathSamplingCallback)(double q[3], double t, void* user_data); /** * Generate a path from an initial configuration to * a target configuration, with a specified maximum turning * radii * * A configuration is (x, y, theta), where theta is in radians, with zero * along the line x = 0, and counter-clockwise is positive * * @param path - the resultant path * @param q0 - a configuration specified as an array of x, y, theta * @param q1 - a configuration specified as an array of x, y, theta * @param rho - turning radius of the vehicle (forward velocity divided by maximum angular velocity) * @return - non-zero on error */ int dubins_shortest_path(DubinsPath* path, double q0[3], double q1[3], double rho); /** * Generate a path with a specified word from an initial configuration to * a target configuration, with a specified turning radius * * @param path - the resultant path * @param q0 - a configuration specified as an array of x, y, theta * @param q1 - a configuration specified as an array of x, y, theta * @param rho - turning radius of the vehicle (forward velocity divided by maximum angular velocity) * @param pathType - the specific path type to use * @return - non-zero on error */ int dubins_path(DubinsPath* path, double q0[3], double q1[3], double rho, DubinsPathType pathType); /** * Calculate the length of an initialised path * * @param path - the path to find the length of */ double dubins_path_length(DubinsPath* path); /** * Return the length of a specific segment in an initialized path * * @param path - the path to find the length of * @param i - the segment you to get the length of (0-2) */ double dubins_segment_length(DubinsPath* path, int i); /** * Return the normalized length of a specific segment in an initialized path * * @param path - the path to find the length of * @param i - the segment you to get the length of (0-2) */ double dubins_segment_length_normalized( DubinsPath* path, int i ); /** * Extract an integer that represents which path type was used * * @param path - an initialised path * @return - one of LSL, LSR, RSL, RSR, RLR or LRL */ DubinsPathType dubins_path_type(DubinsPath* path); /** * Calculate the configuration along the path, using the parameter t * * @param path - an initialised path * @param t - a length measure, where 0 <= t < dubins_path_length(path) * @param q - the configuration result * @returns - non-zero if 't' is not in the correct range */ int dubins_path_sample(DubinsPath* path, double t, double q[3]); /** * Walk along the path at a fixed sampling interval, calling the * callback function at each interval * * The sampling process continues until the whole path is sampled, or the callback returns a non-zero value * * @param path - the path to sample * @param stepSize - the distance along the path for subsequent samples * @param cb - the callback function to call for each sample * @param user_data - optional information to pass on to the callback * * @returns - zero on successful completion, or the result of the callback */ int dubins_path_sample_many(DubinsPath* path, double stepSize, DubinsPathSamplingCallback cb, void* user_data); /** * Convenience function to identify the endpoint of a path * * @param path - an initialised path * @param q - the configuration result */ int dubins_path_endpoint(DubinsPath* path, double q[3]); /** * Convenience function to extract a subset of a path * * @param path - an initialised path * @param t - a length measure, where 0 < t < dubins_path_length(path) * @param newpath - the resultant path */ int dubins_extract_subpath(DubinsPath* path, double t, DubinsPath* newpath); #ifndef __MATLAB__ #ifdef __cplusplus } /* extern "C" */ #endif /* __cplusplus */ #endif /* __MATLAB__ */ #endif /* DUBINS_H */ 详细注释上述代码
最新发布
09-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

直接AC好吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值