codeforces 615B. Longtail Hedgehog

题意:输入n,m表示n个点,m条边,求一条递增的序列的点数与末尾点连接的点个数的乘积最大值。

分析:dp跑一下,时间复杂度O(m)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <map>

using namespace std;

long long sum[100005];

long long dp[100005];
int head[100005];
struct node
{
    int v,next;
}G[200005];
int k=1;
void add(int u, int v)
{
    G[k].v = v;
    G[k].next = head[u];
    head[u] = k++;
}

int main()
{
    int n, m;
    int x, y;
    scanf("%d %d", &n, &m);

    for(int i=1; i<=m; i++)
    {
        scanf("%d%d", &x, &y);
        sum[x]++;
        sum[y]++;
        if(x<y)
            add(y,x);
        else
            add(x,y);
    }
    long long ans = 1;
    int v;
    for(int i=1; i<=n; i++)
    {
        dp[i] = 1;
        for(int j=head[i]; j!=0; j=G[j].next)
        {
            v = G[j].v;
            dp[i] = max(dp[v]+1, dp[i]);
        }
        ans=max(ans, sum[i]*dp[i]);
    }
    printf("%lld\n", ans);
    return 0;
}
时间: 2024-08-05 10:04:37

codeforces 615B. Longtail Hedgehog的相关文章

Codeforces Round #338 (Div. 2) B. Longtail Hedgehog 记忆化搜索/树DP

B. Longtail Hedgehog This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no se

codeforces 338(Div 2) Longtail Hedgehog 解题报告

题目链接:http://codeforces.com/problemset/problem/615/B 题目意思:要画一只 hedgehog,由 tail 和 spines 组成.我们要求得 beauty 最大值: tail * spines. 以下摘自 udon 原话,大家细细品味:(不一定是正确无误的哦,可能要误导他人成分...) 1.对于所有点 x,求出 x 的度数 d[x],O(n+m) 2.对于所有点 x,求出以点 x 为结尾的最长链长度 l[x],由于尾巴节点要求递增,符合DAG性质

*Codeforces Round #338 (Div. 2)

A. Bulbs 题意:n个开关m个灯泡,每个开关可以打开多个灯泡(按下其他开关不会导致已经亮的灯泡熄灭),问能否打开所有的灯. 做法:模拟,用tag标记能被打开的灯,遍历一遍所有的灯泡,看是否全部都能被打开. 代码如下: #include<bits/stdc++.h> #define rep(i,n) for(i=1;i<=n;i++) using namespace std; bool tag[110]; int main() { int n,m; cin>>n>&

Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增

D. Animals and Puzzle 题目连接: http://codeforces.com/contest/713/problem/D Description Owl Sonya gave a huge lake puzzle of size n × m to hedgehog Filya as a birthday present. Friends immediately started to assemble the puzzle, but some parts of it turn

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store