Game Map
Time limit: 1 Sec memory limit: MB
Submitted by: 138 resolution: 60
Submitted State [Discussion Version] [Propositional person:Admin] The title describes the Icpc-world is the most popular RPG game for ACM-ICPC contestants, whose objective are to conquer the world. A Map of the game consists of several cities. There is at the most one road between a pair of cities. Every road is bidirectional. If there is a road connecting the cities, they is called neighbors. Each city have one or more neighbors and all cities is connected by one or more roads. A Player of the game can start at any city of the world. After conquering a city and the player stays, the player can proceed to any neighbor city which are the city of 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 would conquer in order. All cities of the list is distinct, i.e., ci≠cj if i≠j, CI and ci+1 is neighbors to each other, and the number of NE Ighbors 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 is six cities on the map. The city 0 has both neighbors and the city 1 have 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 maps of the game with N Cities, write a program to find the maximum number of cities that He can conquer, that's, the length of the longest list of cities satisfying the above conditions.
Enter your program was to read from standard input. The input starts with a line containing-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 are the number of roads. All cities is numbered from 0 to n-1. In the following m lines, each line contains II integers i and J (0≤i≠j≤n-1) which represent a road connecting Cities I and J.
Output your program was to write to standard output. Print exactly one line. The line should contain the maximum number of cities which Chansu can conquer.
Sample input
6 90 10 41 21 31 41 52 53 44 5
Sample output
4
Test instructions
There is an no-map, which defines a point where u is connected by an edge to a point V, which is called U, V for the neighbor. The longest point sequence, which requires a degree from small to large, and adjacent points to each other as neighbors. The output sequence length.
Analysis
First BFS a bit, forget about the complexity of the feeling is a little time-out, holding a fluke psychology, handed a very good t. DFS can not run every point, for the points previously visited, do not need to run again with Vis[i] records, I point up to a few points, so that the direct maintenance of the maximum value on it.
///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>#defineINF 0x3f3f3f3f#defineFast_io Ios::sync_with_stdio (False)Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-6;Const intmax=1e5+Ten;Const intmod=1e9+7; typedefLong Longll;using namespacestd;#defineGCD (A, B) __gcd (A, B)Inline ll LCM (ll A,ll b) {returnA/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;}returnR;} inline ll Inv1 (ll b) {returnQpow (b,mod-2);} inline ll EXGCD (ll A,ll b,ll&x,ll &y) {if(!B) {x=1; y=0;returnA;} ll R=EXGCD (b,a%b,y,x); y-= (A/b) *x;returnR;} inline ll read () {ll x=0, f=1;CharC=getchar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0';returnx*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);///Direct maintenance of the maximum value } } returnvis[x];}intMain () {scanf ("%lld%lld",&n,&m); Ans=0; for(intI=1; i<=m;i++) {ll x, y; scanf ("%lld%lld",&x,&y); Mp[x].push_back (y); Mp[y].push_back (x); } for(intI=0; i<n;i++) { if(!Vis[i]) ans=Max (Ans,dfs (i)); } printf ("%lld\n", ans); return 0;}
View Code
UPC 9312 Game Map