【Codeforces 1037D】Valid BFS?

【链接】 我是链接,点我呀:)
【题意】

让你判断一个序列是否可能为一个bfs的序列

【题解】

先dfs出来每一层有多少个点,以及每个点是属于哪一层的。 每一层的bfs如果有先后顺序的话,下一层的节点的出队也是有先后顺序的 因此x是当前层只是一个简单的判断条件,还需要更深入的判断 也就是说它是不是应该在这个时候从队列中出来,也就是说它前面是不是应该有节点比它先出现. 我们需要记录每个节点(dep层)的priority值。 这个值表示了这个节点x它的直系儿子们(dep+1层)在所有dep+1层出现的顺序(如果为1最先出现,其次的话依次往后推 细节比较多。 要注意某个节点可能没有后代节点了。

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = (int)2e5;;
    static class Task{
        int n;
        ArrayList g[] = new ArrayList[N+10];
        int fa[] = new int[N+10];
        int dep[]= new int[N+10],dep_size[] = new int[N+10];
        int priority[] = new int[N+10];
        
        void dfs1(int x,int f) {
        	dep_size[dep[x]]++;
        	fa[x] = f;
        	int len = g[x].size();
        	for (int i = 0;i < len;i++) {
        		int y = (int)g[x].get(i);
        		if (y==f) continue;
        		dep[y] = dep[x] + 1;
        		dfs1(y,x);
        	}
        }
        
        int get_priority_up(int x) {
        	if (x==1)
        		return g[x].size();
        	else
        		return g[x].size()-1;
        }
        
        public void solve(InputReader in,PrintWriter out) {
        	for (int i = 1;i <= N;i++) g[i] = new ArrayList<>();
        	
        	n = in.nextInt();
        	for (int i = 1;i <= n-1;i++) {
        		int x,y;
        		x = in.nextInt();y = in.nextInt();
        		g[x].add(y);g[y].add(x);
        	}
        	dep[1] = 1;
        	dfs1(1,-1);
        	int x;
        	x = in.nextInt();
        	if (x!=1) {
        		out.println("No");
        		return;
        	}
        	priority[1] = 1;
        	int cur_dep = 2,cur_priority = 1,cur_priority_count = 0;
        	
        	int record_priority = 1;
        	
        	ArrayList cur_priority_size[] = new ArrayList[N+10];
        	for (int i = 1;i <= n;i++) {
        		cur_priority_size[i] = new ArrayList<>();
        		cur_priority_size[i].add(-1);
        	}
        	cur_priority_size[1].add(g[1].size());
        	for (int i = 2;i <= n;i++) {
        		while((int)cur_priority_size[cur_dep-1].get(cur_priority)==0) {
        			cur_priority++;
        			if (cur_priority>dep_size[cur_dep-1]) {
        				record_priority = 1;
        				cur_dep++;
        				cur_priority = 1;
        			}
        		}
        		x = in.nextInt();
        		if (dep[x]!=cur_dep) {
        			out.println("No");
        			return;
        		}
        		int _father = fa[x];
        		int f_priority = priority[_father];
        		if (f_priority!=cur_priority) {
        			out.println("No");
        			return;
        		}
        		priority[x] = record_priority;
        		cur_priority_size[cur_dep].add(g[x].size()-1);
        		//out.println("priority["+x+"]="+priority[x]);
        		record_priority++;
        		
        		cur_priority_count++;
        		if (cur_priority_count==get_priority_up(_father)) {
        			cur_priority++;
        			cur_priority_count = 0;
        			if (cur_priority>dep_size[cur_dep-1]) {
        				record_priority = 1;
        				cur_dep++;
        				cur_priority = 1;
        			}
        		}
        	}
        	out.println("Yes");
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}	
posted @ 2019-03-02 15:23  AWCXV  阅读(116)  评论(0编辑  收藏  举报