POJ 3895 Cycles of Lanes

本文介绍了一种基于深度优先搜索算法求解无向图中最大简单环长度的方法,并提供了完整的Java实现代码。

 

Cycles of Lanes

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1374 Accepted: 502

 

Description

Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of the N crossroads of the park (labeled from 1 to N). There is no pair of crossroads connected by more than one lane and it is possible to pass from each crossroad to each other crossroad by a path composed of one or more lanes. A cycle of lanes is simple when passes through each of its crossroads exactly once. 
The administration of the University would like to put on the lanes pictures of the winners of Regional Collegiate Programming Contest in such way that the pictures of winners from the same university to be on the lanes of a same simple cycle. That is why the administration would like to assign the longest simple cycles of lanes to most successful universities. The problem is to find the longest cycles? Fortunately, it happens that each lane of the park is participating in no more than one simple cycle (see the Figure). 

Input

On the first line of the input file the number T of the test cases will be given. Each test case starts with a line with the positive integers N and M, separated by interval (4 <= N <= 4444). Each of the next M lines of the test case contains the labels of one of the pairs of crossroads connected by a lane.

Output

For each of the test cases, on a single line of the output, print the length of a maximal simple cycle.

Sample Input

1 
7 8 
3 4 
1 4 
1 3 
7 1 
2 7 
7 5 
5 6 
6 2

Sample Output

4

 

把《算法》的无向图部分看完了,于是找到简单的无向图的题练练手。

 

 

题意是让你计算出几幅无向图的最大环的长度。

输入:

1(用例的个数)

7 8(顶点的数量 边的数量)

3 4(顶点3到顶点4之间存在边)

1 4(顶点1到顶点4之间存在边)

……

输出:

4(最大环的长度)

 

思路是:先把图存下来,然后使用深度优先搜索计算最大环的长度。这里使用了面向对象的方法,把无向图和环分别封装成一个类,无向图的对象需要调用方法完成无向图的初始化,而环的对象在构造时便使用深度优先搜索计算好了最大环的长度,而且用实例域记录了下来。所以输出时只要调用环的对象的getter方法即可。

 

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by 小粤 on 2015/9/4.
 */

public class Main
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        int numberOfTestCases = scanner.nextInt();
        for (int i = 0; i < numberOfTestCases; i++)
        {
            Graph graph = new Graph(scanner.nextInt());
            int edge = scanner.nextInt();
            for (int j = 0; j < edge; j++)
            {
                graph.addEdge(scanner.nextInt() - 1, scanner.nextInt() - 1); // 此处把输入纠正(1->0, 3->2, 14->13...)
            }

            Cycle cycle = new Cycle(graph);
            System.out.println(cycle.getMaxLengthOfCycle());
        }
    }
}

class Graph
{
    private final int V; // 顶点数目
    private int E; // 边数目
    private List[] adj; // 邻接表

    /**
     * 构造器
     * 初始化一个定点数为v边数为0的图
     *
     * @param v 顶点数目
     */
    public Graph(int v)
    {
        V = v;
        E = 0;
        adj = new List[v];
        for (int i = 0; i < adj.length; i++)
        {
            adj[i] = new ArrayList<Integer>();
        }
    }

    /**
     * 返回顶点数目
     *
     * @return 顶点数目
     */
    public int V()
    {
        return V;
    }

    /**
     * 返回边的数目
     *
     * @return 边的数目
     */
    public int E()
    {
        return E;
    }

    /**
     * 添加新的边
     *
     * @param v 边的一个顶点
     * @param w 边的另一个顶点
     */
    public void addEdge(int v, int w)
    {
        E++;
        adj[v].add(w);
        adj[w].add(v);
    }

    /**
     * 获取顶点v的邻接表
     *
     * @param v 要获取邻接表的顶点
     * @return 邻接表
     */
    public Iterable<Integer> adj(int v)
    {
        return adj[v];
    }
}

class Cycle
{
    private boolean[] marked; // 标记顶点是否已经访问过

    private int[] number; // 每个顶点的序号,根据深度优先搜索的顺序编号
    private int maxLengthOfCycle; // 最大环的长度

    /**
     * 构造器
     * 通过深度优先搜索,初始化相关参数
     *
     * @param graph 要搜索的图
     */
    public Cycle(Graph graph)
    {
        marked = new boolean[graph.V()];
        number = new int[graph.V()];
        for (int i = 0; i < graph.V(); i++)
        {
            if (!marked[i])
            {
                number[i] = 1; // 根据深度优先搜索的顺序编号
                dfs(graph, i, i);
            }
        }
    }

    /**
     * 深度优先搜索
     *
     * @param graph 要搜索的图
     * @param v     要搜索的顶点
     * @param u     要搜索的顶点的上一个顶点
     */
    private void dfs(Graph graph, int v, int u)
    {
        marked[v] = true;
        for (Integer w : graph.adj(v))
        {
            if (!marked[w])
            {
                number[w] = number[v] + 1; // 根据深度优先搜索的顺序编号
                dfs(graph, w, v);
            }
            else if (w != u) // 说明该图有环
            {
                if (maxLengthOfCycle < number[v] - number[w] + 1)
                {
                    maxLengthOfCycle = number[v] - number[w] + 1;
                }
            }
        }
    }

    /**
     * 返回最大环的长度
     *
     * @return 最大环的长度
     */
    public int getMaxLengthOfCycle()
    {
        return maxLengthOfCycle;
    }
}

 

 

本项目构建于RASA开源架构之上,旨在实现一个具备多模态交互能力的智能对话系统。该系统的核心模块涵盖自然语言理解、语音转文本处理以及动态对话流程控制三个主要方面。 在自然语言理解层面,研究重点集中于增强连续对话中的用户目标判定效能,并运用深度神经网络技术提升关键信息提取的精确度。目标判定旨在解析用户话语背后的真实需求,从而生成恰当的反馈;信息提取则专注于从语音输入中析出具有特定意义的要素,例如个体名称、空间位置或时间节点等具体参数。深度神经网络的应用显著优化了这些功能的实现效果,相比经典算法,其能够解析更为复杂的语言结构,展现出更优的识别精度与更强的适应性。通过分层特征学习机制,这类模型可深入捕捉语言数据中隐含的语义关联。 语音转文本处理模块承担将音频信号转化为结构化文本的关键任务。该技术的持续演进大幅提高了人机语音交互的自然度与流畅性,使语音界面日益成为高效便捷的沟通渠道。 动态对话流程控制系统负责维持交互过程的连贯性与逻辑性,包括话轮转换、上下文关联维护以及基于情境的决策生成。该系统需具备处理各类非常规输入的能力,例如用户使用非规范表达或对系统指引产生歧义的情况。 本系统适用于多种实际应用场景,如客户服务支持、个性化事务协助及智能教学辅导等。通过准确识别用户需求并提供对应信息或操作响应,系统能够创造连贯顺畅的交互体验。借助深度学习的自适应特性,系统还可持续优化语言模式理解能力,逐步完善对新兴表达方式与用户偏好的适应机制。 在技术实施方面,RASA框架为系统开发提供了基础支撑。该框架专为构建对话式人工智能应用而设计,支持多语言境并拥有活跃的技术社区。利用其内置工具集,开发者可高效实现复杂的对话逻辑设计与部署流程。 配套资料可能包含补充学习文档、实例分析报告或实践指导手册,有助于使用者深入掌握系统原理与应用方法。技术文档则详细说明了系统的安装步骤、参数配置及操作流程,确保用户能够顺利完成系统集成工作。项目主体代码及说明文件均存放于指定目录中,构成完整的解决方案体系。 总体而言,本项目整合了自然语言理解、语音信号处理与深度学习技术,致力于打造能够进行复杂对话管理、精准需求解析与高效信息提取的智能语音交互平台。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值