1. 程式人生 > >upc 9312 Game Map

upc 9312 Game Map

分析 under closed sta tchar 並且 lcm sed algorithm

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\n",ans);

    return 0;
}
View Code

upc 9312 Game Map