upc 9312 Game Map

Game Map

时间限制: 1 Sec  内存限制: 128 MB
提交: 138  解决: 60
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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

题意

有一个无向图,定义一个点u由一条边与点v相连,称u、v为邻居。求一个最长的点序列,要求度数从小到大,并且相邻的点互为邻居。输出序列长度。

分析

一开始BFS了一下,算了一下复杂度感觉就有点超时,抱着侥幸的心理,交了一发果然T了。DFS不能跑每一个点,对于之前访问过的点,不需要再跑了用vis[i]记录,i点之后最多有几个点,这样直接维护最值就可以了。

///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
ll n,m,ans;
vector<ll>mp[MAX];
ll vis[MAX];
ll dfs(ll x)
{
    if(!vis[x])
    {
        vis[x]=1;
        for(ll i=0;i<mp[x].size();i++)
        {
            ll u=mp[x][i];
            if(mp[u].size()>mp[x].size())
                vis[x]=max(vis[x],dfs(u)+1); ///直接维护最值
        }
    }
    return vis[x];
}
int main()
{

    scanf("%lld%lld",&n,&m);
    ans=0;

    for(int i=1;i<=m;i++)
    {
        ll x,y;
        scanf("%lld%lld",&x,&y);
        mp[x].push_back(y);
        mp[y].push_back(x);
    }

    for(int i=0;i<n;i++)
    {
        if(!vis[i])
            ans=max(ans,dfs(i));
    }

    printf("%lld
",ans);

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Kissheart/p/9743284.html