zoukankan      html  css  js  c++  java
  • Luogu P1983 车站分级

    Luogu P1983 车站分级

    首先,这题告诉我们没有把握一定不要在线做;
    其次,数据比较大的时候是要做离散化的;
    最后,最重要的是——代码别打太快,不然一定会出事!!!

    #include<bits/stdc++.h>
    #define N 1010
    
    using namespace std;
    
    int n,m,s,cnt,tot,ans;
    int level[N],stage1[N],stage2[N],in[N],head[N];
    bool vis[N],mmap[N][N];
    
    struct node {
    	int nxt,to;
    }e[N*N];
    
    int fscan()
    {
    	int x=0,f=1;
    	char ch;
    	while((ch=getchar())<'0'||ch>'9') {
    		if(ch=='-')f=-1;
    	}
    	while(ch>='0'&&ch<='9') {
    		x=x*10+ch-48;
    		ch=getchar();
    	}
    	return x*f;
    }
    
    void addEdge(int u,int v) {
    	mmap[u][v]=1;
    	e[++cnt]=(node){head[u],v};
    	head[u]=cnt;
    	in[v]++;
    	return;
    }
    
    void Read() {
    	n=fscan();
    	m=fscan();
    	for(int i=1;i<=n;i++) {
    		level[i]=1;
    	}
    	for(int i=1;i<=m;i++) {
    		s=fscan();
    		tot=0;
    		for(int j=1;j<=s;j++) {
    			stage1[j]=fscan();
    			if(j==1) {
    				continue;
    			}
    			for(int k=stage1[j-1]+1;k<=stage1[j]-1;k++) {
    				stage2[++tot]=k;
    			}
    		}
    		for(int j=1;j<=s;j++) {
    			for(int k=1;k<=tot;k++) {
    				if(!mmap[stage1[j]][stage2[k]]) {
    					addEdge(stage1[j],stage2[k]);
    				}
    			}
    		}
    	}
    	return;
    }
    
    void Topo() {
    	queue <int> q;
    	for(int i=1;i<=n;i++) {
    		if(!in[i]) {
    			q.push(i);
    			//printf("%d
    ",i);
    		}
    	}
    	//printf("/n");
    	while(!q.empty()) {
    		int f=q.front();
    		q.pop();
    		for(int i=head[f];i;i=e[i].nxt) {
    			int t=e[i].to;
    			in[t]--;
    			level[t]=level[f]+1;
    			if(!in[t]) {
    				//printf("%d
    ",t);
    				q.push(t);
    			}
    		}
    	}
    	return;
    }
    
    void Print() {
    	for(int i=1;i<=n;i++) {
    		ans=max(ans,level[i]);
    	}
    	printf("%d",ans);
    	return;
    }
    
    int main()
    {
    	Read();
    	Topo();
    	Print();
    	return 0;
    }
    
  • 相关阅读:
    并发运行的思维模型
    进程和线程的区别
    拿来主义
    同步组件合作和团队合作 让世界变得更美好
    strace a++;b++;a+b;
    System 88: GDB Overview
    numpy多维数组维度及添加轴的理解
    Numpy入门
    python列表list 和numpy.array区别
    数组的生成方法
  • 原文地址:https://www.cnblogs.com/luoshui-tianyi/p/11604291.html
Copyright © 2011-2022 走看看