zoukankan      html  css  js  c++  java
  • 【ICPC 2017 Daejeon】UPC-9312 Game Map(dfs)

    题目描述
    The ICPC-World is the most popular RPG game for ACM-ICPC contestants, whose objective is to conquer the world. A map of the game consists of several cities. There is at most one road between a pair of cities. Every road is bidirectional. If there is a road connecting two cities, they are called neighbors. Each city has one or more neighbors and all cities are connected by one or more roads. A player of the game can start at any city of the world. After conquering a city that the player stays, the player can proceed to any neighbor city which is the city the player to conquer at the next stage.
    Chansu, a mania of the game, enjoys the game in a variety of ways. He always determines a list of cities which he wants to conquer before he starts to play the game. In this time, he wants to choose as many cities as possible under the following conditions: Let (c0, c1, …, cm-1)be a list of cities that he will conquer in order. All cities of the list are distinct, i.e., ci ≠ cj if i ≠ j, ci and ci+1 are neighbors to each other, and the number of neighbors of ci+1 is greater than the number of neighbors of ci for i = 0, 1, …, m-2.
    For example, let’s consider a map of the game shown in the figure below. There are six cities on the map. The city 0 has two neighbors and the city 1 has five neighbors. The longest list of cities satisfying the above conditions is (2,5,4,1) with 4 cities.在这里插入图片描述

    In order to help Chansu, given a map of the game with n cities, write a program to find the maximum number of cities that he can conquer, that is, the length of the longest list of cities satisfying the above conditions.

    输入
    Your program is to read from standard input. The input starts with a line containing two integers, n and m (1 ≤ n ≤ 100,000, n-1 ≤ m ≤ 300,000), where n is the number of cities on the game map and m is the number of roads. All cities are numbered from 0 to n-1. In the following m lines, each line contains two integers i and j (0 ≤ i ≠ j ≤ n-1) which represent a road connecting two cities i and j.

    输出
    Your program is to write to standard output. Print exactly one line. The line should contain the maximum number of cities which Chansu can conquer.

    样例输入
    6 9
    0 1
    0 4
    1 2
    1 3
    1 4
    1 5
    2 5
    3 4
    4 5

    样例输出
    4

    题意: 给出一个有N个节点M条边的图,任意选择一个起点,可以从这个起点开始向有边相连相邻扩张占领节点,只允许向度数比当前结点大的节点扩张,问最多可以扩张多少个结点。

    题解: 因为不知道从哪里开始是能延展出最长最多节点的起点。因此所有节点都搜一遍,直接向比其度数大的节点扩张,然后之后遍历的节点如果度数较小,遇到了度数较大的已经遍历深搜过的节点,就直接接上上次得到的结果,这样一段一段的结果接起来,取一个最大值,即深搜得到的能扩展出最多节点的数量

    #include<bits/stdc++.h>
    #define LL long long
    #define M(a,b) memset(a,b,sizeof a)
    #define pb(x) push_back(x)
    using namespace std;
    const int maxn=1e5+7;
    struct edge
    {
        int to,nxt;
        edge() {}
        edge(int a,int b)
        {
            to=a;
            nxt=b;
        }
    } mp[maxn*3];
    int head[maxn],cnt;
    int n,m,du[maxn],sum[maxn];
    void addedge(int to,int from)
    {
        du[to]++,du[from]++;
        mp[cnt]=edge(to,head[from]);
        head[from]=cnt++;
        mp[cnt]=edge(from,head[to]);
        head[to]=cnt++;
    }
    int dfs(int x)
    {
        if(sum[x])return sum[x];
        for(int i=head[x]; i!=-1; i=mp[i].nxt)
        {
            if(du[mp[i].to]<=du[x])continue;
            sum[x]=max(sum[x],dfs(mp[i].to)+1);
        }
        return sum[x];
    }
    int main()
    {
        int from,to,ans=0;
        cnt=0;
        M(head,-1);
        M(du,0);
        M(sum,0);
        scanf("%d%d",&n,&m);
        for(int i=1; i<=m; i++)
            scanf("%d%d",&from,&to),addedge(from,to);
        for(int i=0; i<n; i++) ans=max(ans,dfs(i)+1);
        printf("%d
    ",ans);
    }
    
    
  • 相关阅读:
    将项目发布到多台服务器并解决高并发
    Nginx 反向代理和负载均衡
    Nginx的基本理论
    图片上传功能(EasyUI前台框架+SSM框架)
    错误:Eclipse老是出现 updating error reports database
    JPA
    Java中 @override 报错
    SpringBoot常用注解
    SpringBoot学习:整合shiro(rememberMe记住我后自动登录session失效解决办法)
    SpringBoot学习:整合shiro(rememberMe记住我功能)
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135691.html
Copyright © 2011-2022 走看看