Game Map (容器的使用+贪心)

[提交] [状态] [讨论版] [命题人: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

题意:给定n个点,有m条连线,选择一条路径,要保证路径中第 i 个点所连接的点的个数要大于第 i - 1个点连接的点的个数,

问对所给的图中,最长的路径的长度

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long int
#define maxn 1000010
ll num[maxn];
vector<ll> flag[maxn]; 
typedef struct nodee{
	int member;
	ll countt;
}node;
ll endd[maxn];
node maze[maxn];
bool panduan(node u,node v)
{
	return u.countt<v.countt;
}
int main()
{
	int n,m;
	ll x,y;
	scanf("%d %d",&n,&m);
	memset(num,0,sizeof(num));
	memset(maze,0,sizeof(maze));
	for(int i=0;i<m;i++){
		scanf("%lld %lld",&x,&y);
		maze[x].member=x;
		maze[y].member=y;
		maze[x].countt++;
		maze[y].countt++;
		flag[x].push_back(y);
		flag[y].push_back(x);
		num[x]++;
		num[y]++;
	}
	sort(maze,maze+n,panduan);
	memset(endd,0,sizeof(endd));
	for(int i=0;i<n;i++){
		ll indx=maze[i].member;
		int l=flag[indx].size();
		for(int j=0;j<l;j++){
			if(num[indx]<num[flag[indx][j]])
				endd[flag[indx][j]]=max(endd[flag[indx][j]],endd[indx]+1);
		}
	}
		
	ll maxx=0;
	for(int i=0;i<n;i++)
		maxx=max(maxx,endd[i]);
		
	printf("%lld\n",maxx+1);
	
	return 0;
 } 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值