P1983 station classification, p1983 station Classification
Description
A one-way railway line numbered 1, 2 ,..., N train stations. Each railway station has a level, with a minimum level of 1. There are several train trips traveling on this line, each of which meets the following requirements: if this train stops at the railway station x, all levels between the origin station and terminal station must be docked if they are greater than or equal to the Railway Station x. (Note: The start station and the terminal station are also known as sites to be docked in advance)
For example, the following table shows the running status of five trains. Among them, the first four trains met the requirements, while the first four trains were docked at the No. 6 railway station (level 2) but not through the bus (level 2) but does not meet the requirements.
The current running status of m trains (all meeting the requirements) is estimated that the n train stations are divided into at least several different levels.
Input/Output Format
Input Format:
The input file is level. in.
The first line contains two positive integers, n and m, separated by a space.
In line I + 1 (1 ≤ I ≤ m), the first is a positive integer si (2 ≤ si
≤ N), indicating that train I has si bus stops; then there are si positive integers, indicating the numbers of all bus stops, arranged in ascending order. Separate each number with a space. Input to ensure that all trains meet the requirements.
Output Format:
The output file is level. out.
The output contains only one row and a positive integer, that is, the minimum number of levels divided by n railway stations.
Input and Output sample
Input example #1:
9 2 4 1 3 5 6 3 3 5 6
Output sample #1:
2
Input example #2:
9 3 4 1 3 5 6 3 3 5 6 3 1 5 9
Output sample #2:
3
Description
For 20% of the data, 1 ≤ n, m ≤ 10;
For 50% of the data, 1 ≤ n, m ≤ 100;
For 100% of data, 1 ≤ n, m ≤ 1000.
I will not talk much about the idea. If you don't understand it, you can refer to the downstairs, that is, building edge + topological sorting.
Let me answer your questions in the Forum.
I. 8.9.10 these three points are inexplicable RE, so please open a map array to record whether there is a path between the two sides, which is equivalent to an access tag
Ii. 2nd, TLE. Check the enumeration during reading. You must enumerate all vertices starting and ending first, and then force edge creation if the conditions are met.
3. Or TLE. Please change your map array to the bool type !!!!!!!!!!!!!!!
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 using namespace std; 8 void read(int & n) 9 {10 char c='+';int x=0;int flag=0;11 while(c<'0'||c>'9')12 {13 c=getchar();14 if(c=='-')15 flag=1;16 }17 while(c>='0'&&c<='9')18 x=x*10+(c-48),c=getchar();19 flag==1?n=-x:n=x;20 }21 const int MAXN=1001;22 struct node23 {24 int u,v,nxt;25 }edge[1000001];26 int head[MAXN];27 int num=1;28 int n,m,p,gg;29 int a[MAXN];30 int vis[MAXN];31 int rudu[MAXN];32 int step[MAXN];33 bool map[MAXN][MAXN];34 inline void add_edge(int x,int y)35 {36 edge[num].u=x;37 edge[num].v=y;38 edge[num].nxt=head[x];39 head[x]=num++;40 }41 inline void init()42 {43 read(n);read(m);44 for(int i=1;i<=n;i++)head[i]=-1;45 for(int i=1;i<=m;i++)46 {47 memset(vis,0,sizeof(vis));48 read(p);49 for(int i=1;i<=p;i++)50 {51 read(a[i]);52 vis[a[i]]=1;53 }54 for(int i=1;i<=p;i++)55 for(int j=a[1];j<=a[p];j++)56 if(vis[j]==0&&map[a[i]][j]==0)57 {58 add_edge(a[i],j);59 map[a[i]][j]=1;60 rudu[j]++; 61 }62 }63 }64 inline void Topsort()65 {66 queue<int>q;67 for(int i=1;i<=n;i++)68 if(rudu[i]==0)69 q.push(i);70 int ans=0;71 while(q.size()!=0)72 {73 int p=q.front();74 q.pop();75 for(int i=head[p];i!=-1;i=edge[i].nxt)76 {77 rudu[edge[i].v]--;78 if(rudu[edge[i].v]==0)79 {80 q.push(edge[i].v);81 step[edge[i].v]=step[edge[i].u]+1;82 ans=max(ans,step[edge[i].v]);83 }84 }85 }86 printf("%d",ans+1);87 }88 int main()89 {90 init();91 Topsort();92 return 0;93 }