求树的最大路径和

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4648

Magic Pen 6

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 1857    Accepted Submission(s): 637

Problem Description

In HIT, many people have a magic pen. Lilu0355 has a magic pen, darkgt has a magic pen, discover has a magic pen. Recently, Timer also got a magic pen from seniors.

At the end of this term, teacher gives Timer a job to deliver the list of N students who fail the course to dean‘s office. Most of these students are Timer‘s friends, and Timer doesn‘t want to see them fail the course. So, Timer decides to use his magic pen
to scratch out consecutive names as much as possible. However, teacher has already calculated the sum of all students‘ scores module M. Then in order not to let the teacher find anything strange, Timer should keep the sum of the rest of students‘ scores module
M the same.

Plans can never keep pace with changes, Timer is too busy to do this job. Therefore, he turns to you. He needs you to program to "save" these students as much as possible.

Input

There are multiple test cases.

The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a1,a2,...an (-100000000 <= a1,a2,...an <= 100000000) denoting
the score of each student.(Strange score? Yes, in great HIT, everything is possible)

Output

For each test case, output the largest number of students you can scratch out.

Sample Input

2 3
1 6
3 3
2 3 6
2 5
1 3

Sample Output

1
2
0

Hint

The magic pen can be used only once to scratch out consecutive students.

Source

2013 Multi-University Training Contest 5

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  4822 4821 4820 4819 4818

简单题,len从大到小,(sum[j+len]-sum[j])%M==0 len即为所求。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAX=100010;
int sum[MAX];
int main()
{
    int N,M;
    while(cin>>N>>M)
    {
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=N;i++)
        {
            int x;
            scanf("%d",&x);
            sum[i]=sum[i-1]+x;
        }
        bool flag=true;
        for(int i=N;i>=0&&flag;i--)
        {
            for(int j=0;j+i<=N;j++)
            {
                if((sum[j+i]-sum[j])%M==0)
                {
                    cout<<i<<endl;
                    flag=false;
                    break;
                }
            }
        }
    }
    return 0;
}

求树的最大路径和

时间: 2024-11-10 01:21:44

求树的最大路径和的相关文章

求一棵树的最小路径覆盖

相关题目:http://codeforces.com/problemset/problem/618/D 有向图的最小路径覆盖(所有点)可以用二分图来解,n-最大匹配. 无向图的最小路径覆盖(所有点)似乎是比较困难的问题 那么对于特殊的无向图 - '树'来说,求它的最小路径覆盖有什么好用的方法呢? 首先我们可以考虑用dp求解,维护每棵子树留下向上可延伸路径的情况下能获得的最小路径覆盖是多少f[i][1],以及每棵子树不留下向上可延伸路径的情况下能获得的最小路径覆盖f[i][0].转移在这里不多赘述

Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)

题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上有一个点的度数是n-1,那么必然需要走一条生成树上的边,此时答案为x+y*(n-2). 否则可以不走生成树上的边,则答案为y*(n-1). 再考虑x<y的情况,那么应该尽量走生成树上的边,由于树上没有环,于是我们每一次需要走树的一条路,然后需要从非生成树上的边跳到树的另一个点上去, 显然跳的越少越好,于

51nod 配对(求树的重心)

传送门:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1737 给出一棵n个点的树,将这n个点两两配对,求所有可行的方案中配对两点间的距离的总和最大为多少. Input 一个数n(1<=n<=100,000,n保证为偶数) 接下来n-1行每行三个数x,y,z表示有一条长度为z的边连接x和y(0<=z<=1,000,000,000) Output 一个数表示答案 Input示例 6 1 2 1 1 3 1

VIJOS1107 求树的最长链

vijos1107环游大同80天 学习了一下求树的最长链的方法 最简单的思路就是两次dfs 两次dfs分别有什么用呢? 第一次dfs,求出某个任意的点能到达的最远的点 第二次dfs,从所搜到的最远的点倒搜回去. 为什么需要两次呢? 其实很容易想通第一遍dfs的起始点或许并不是最长链的起点 从最远的点倒搜到的最长的链就是所求的解 (因为最长链一定经过这个最远的点啊... 这里注意题目表述: 假设任意的两个风景点都有且仅有一条路径(无回路)相连.显然,任意一个风景点都可以作为游览路线的起点或者终点.

poj2631 求树的直径裸题

题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明: 1.设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则   dis(u

poj:1985:Cow Marathon(求树的直径)

Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has com

lightoj-1094 Farthest Nodes in a Tree(求树的直径)

1094 - Farthest Nodes in a Tree PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBGiven a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirect

hdoj 4612 Warm up【双连通分量求桥&amp;&amp;缩点建新图求树的直径】

Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5093    Accepted Submission(s): 1131 Problem Description N planets are connected by M bidirectional channels that allow instant transport

【poj1655】【poj3107】【求树的重心】

先来说一下怎样来求树的直径: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明: 1 设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则 dis(u,T) >dis(u,s) 且 dis(u,T)>dis(u,t) 则最长路不是s-t了,与假设矛盾 2 设u不为s-t路径上的点 首先明确,假如u走到了s-t路径上的一点,