CodeForces 615B Longtail Hedgehog(dp)

http://codeforces.com/contest/615/problem/B

n个点,m条无向边,在连成的链中找一条递增的链,使得末尾节点的度数乘以深度最大,因为是无向边,又要求递增的链,所以尽量使小的数做起点,并且将所有的边按照节点大小进行排序,从最小的点开始遍历,记录每一个点最大的深度,然后找到最大的乘积

注意:最后的乘积用long long

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

#define N 200010
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))

const double EPS = 1e-10;
typedef long long LL;

vector <vector <int> > G;

int n, m, vis[N], dep[N];
LL maxs, dp[N], L[N];

void Init ()
{
    G.clear();
    G.resize (n+1);
    maxs = 0;
    met (L, 0);

    for (int i=1; i<=n; i++)
        dp[i] = 1;
}

void Solve ()
{
    met (vis, 0);
    for (int i=1; i<=n; i++)
    {
        int len = G[i].size();
        for (int j=0; j<len; j++)
        {
            int v = G[i][j];
            dp[i] = max (dp[i], dp[v]+1);
        }
    }

    for (int i=1; i<=n; i++)
        maxs = max (maxs, dp[i]*L[i]);
}

int main ()
{
    while (scanf ("%d %d", &n, &m) != EOF)
    {
        Init();
        while (m--)
        {
            int u, v;
            scanf ("%d %d", &u, &v);

            if (u<v) swap (u, v);
            G[u].push_back (v);
            L[u]++, L[v]++;
        }

        Solve();
        printf ("%I64d\n", maxs);
    }
    return 0;
}


你可能感兴趣的:(CodeForces 615B Longtail Hedgehog(dp))