Gym101667C Game Map(dfs)

Gym-101667C Game Map

Description

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 (ܿ଴, ܿଵ,⋯,ܿ௠ିଵሻ be a list of cities that he will conquer in order.
All cities of the list are distinct, i.e., ܿ௜ ് ܿ௝ if ്݆݅, ܿ௜ and ܿ௜ାଵ are neighbors to each other, and the number of neighbors of ܿ௜ାଵ is greater than the number of neighbors of ܿ௜ for ݅ ൌ 0, 1, … , ݉ െ 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 ݊ 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.

Input

Your program is to read from standard input. The input starts with a line containing two integers, ݊ and ݉ (1 ൑ ݊ ൑ 100,000, n െ 1 ൑ ݉ ൑ 300,000), where ݊ is the number of cities on the game map and ݉ is the number of roads. All cities are numbered from 0 to ݊െ1. In the following ݉ lines, each line contains two integers ݅ and ݆ ሺ0 ൑ ݅ ് ݆ ൑ ݊ െ 1ሻ which represent a road connecting two cities ݅ and ݆.

Output

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.
The following shows sample input and output for two test cases

Sample Input 1

6 9
0 1
0 4
1 2
1 3
1 4
1 5
2 5
3 4
4 5

Output for the Sample Input 1

4

Sample Input 2

12 11
1 2
2 3
3 4
4 5
5 0
6 3
7 4
8 5
9 4
10 5
11 5

Output for the Sample Input 2

5

题解

题意

对于每个点,得到其邻居的数目。给出序列要求:邻居的数目必须得增长。求这样序列的最大长度

思路

dfs+剪枝,对一个点的所有邻居进行遍历,当邻居节点的邻居数目大于当前点的邻居数目时,进行dfs。然而如果对每个点都进行dfs的话,会TLE,有一步的剪枝。由于,这样一种情况的存在,当之后访问到之前访问过的节点时,可以直接维护最值。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

const int MAXN = 2e5+10;
vector<int> gra[MAXN];
int vis[MAXN];
int N,M;

int dfs(int u){	
	if(vis[u]==0){
	//vis[u]=1;
	vis[u]=1;
	for(int i=0;i<gra[u].size();i++){
		int to = gra[u][i];
		if(gra[to].size()>gra[u].size()){
			vis[u] = max(vis[u],dfs(to)+1);
		}
	}
	}
	return vis[u];
}

void init(){
	memset(vis,0,sizeof(vis));
	memset(gra,0,sizeof(gra));
}

int main() {
	
	scanf("%d %d",&N,&M);
		init();
		int a,b;
		for(int i=0;i<M;i++){
			scanf("%d %d",&a,&b);
			gra[a].push_back(b);
			gra[b].push_back(a);
		}
		int mmax = 0;
		for(int i=0;i<N;i++){
			if(vis[i]==0)	mmax = max(mmax,dfs(i));
		}
		printf("%d\n",mmax);
	   
    return 0;
}

posted @ 2018-09-19 20:22  caomp  阅读(478)  评论(0)    收藏  举报