1. 程式人生 > >UPC 9312 vector 當做二維陣列

UPC 9312 vector 當做二維陣列

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

要求輸出城市的個數,並且所要輸出的城市的要求是其連線的城市的個數是增加的,也就是遞增的,看看資料,一般的二維陣列肯定是會爆掉的,那就應用vector來實現

#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<iostream>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
#define rep(i,a,b) for(int i=a;i<=b;i++)
int n, m;
vector<int> e[N];
struct node
{
    int id;
    int val;
    bool operator < (const node &a) const
    {
        return val<a.val;
    }
}v[N];
int dp[N],vis[N];
int du[N];
int main()
{

    int n, m;
   scanf("%d %d", &n, &m);
   rep(i,0,n-1)
   v[i].id=i,dp[i]=1;
   rep(i,1,m)
   {
       int x,y;
       scanf("%d%d",&x,&y);
       e[x].push_back(y);
       e[y].push_back(x);
      du[x]++;du[y]++;
      v[x].val++;v[y].val++;
   }
   sort(v,v+n);
   for(int i=0;i<n;i++)
   {
       int u=v[i].id;
       for(int j=0;j<e[u].size();j++)
       {
           int v=e[u][j];
           if(!vis[v]&&du[u]<du[v])
            dp[v]=max(dp[v],dp[u]+1);
       }
       vis[u]=1;
   }
   printf("%d\n",*max_element(dp,dp+n));

    return 0;
}