Topological thinking station + rating (Los Valley P1983)

Grading station

Title Description

A unidirectional railway line, there are sequentially numbered 1,2, ..., n n-th station. Each level has a railway station, a minimum of level 1. Several times on a conventional train with this line, each pass will meet the following requirements: If the station stop tour trips x, then the originating station, terminal or greater between all levels of x must docking station . (Note: the starting station and the terminal needs to known a priori to be regarded as a natural stop site)

For example, in Table 5 are views of the operation of the train. Wherein the front train 4 times meet the requirements, and the fifth time since the stop of the train station 3 (stage 2) has not docked via station No. 6 (level 2 also) does not satisfy the requirements.

Existing train operation m times (all satisfy requirements), the n estimated test station into at least several different levels.

Input Format

The first line contains two positive integers n, m separated by a space.

I + 1-row (1≤i≤m), a first positive integer s_i (2 ≤ s_i ≤ n) represents the i-th train stops s_i times out; s_i Then there is a positive integer, indicating that all stops the numbers in ascending order. Each separated by a space between the two numbers. Input to ensure that all of the trips are to meet the requirements.

Output Format

A positive integer that is the least number of levels n railway stations divided.


This question is difficult to think of a topology, which began to think that it can not maintain the tree line at the interval with a maximum value, and then with the left and right to
compare stop the station level, and then modify finally see a mistake;

Positive solutions should be the trips for each pass, the recording its docking site; then enumerate all non-stop station di [1] -di [s] in each non-docking sites forAll the trips docking siteWere built side. Finally, then run again topology, find the maximum path;

Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100010;
const int M=2000100;
const LL mod=2e9;
int n,m,cnt,head[N],di[N],in[N],dp[N],ans;
bool vis[N],vit[1100][1100];
struct Node{
	int to,nex;
}edge[M];
void add(int p,int q){
	edge[cnt].nex=head[p];
	edge[cnt].to=q;
	head[p]=cnt++;
}
void bfs(){
	queue<int>qu;
	for(int i=1;i<=n;i++) if(!in[i]) qu.push(i),dp[i]=1;
	while(!qu.empty()){
		int u=qu.front();
		qu.pop();
		for(int i=head[u];~i;i=edge[i].nex){
			int v=edge[i].to;
			dp[v]=max(dp[v],dp[u]+1);
			ans=max(dp[v],ans);	
			in[v]--;
			if(!in[v]) qu.push(v);
		}
	}
}
int main(){
	memset(head,-1,sizeof(head));
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		memset(vis,false,sizeof(vis));
		int s;
		scanf("%d",&s);
		for(int j=1;j<=s;j++) scanf("%d",&di[j]),vis[di[j]]=true;
		for(int j=di[1];j<=di[s];j++){
			if(vis[j]) continue;
			for(int k=1;k<=s;k++){
				if(vit[j][di[k]]) continue;
				vit[j][di[k]]=true;
				add(j,di[k]),in[di[k]]++;
			}
		}
	}
	bfs();
	cout<<ans<<endl;
	return 0;
}
Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105145552