[Codeforces 1037D] Valid BFS?

來源:互聯網
上載者:User

標籤:pen   force   main   ++   時間   inline   dig   ==   style   

[題目連結]

         http://codeforces.com/problemset/problem/1037/D

[演算法]

         首先求出每個點的父節點 , 每棵子樹的大小

         然後判斷BFS序是否合法即可

         時間複雜度 : O(N)

[代碼]

        

#include<bits/stdc++.h>using namespace std;const int MAXN = 2e5 + 10;struct edge{        int to , nxt;} e[MAXN << 1];int n , tot;int a[MAXN],fa[MAXN],head[MAXN],size[MAXN];template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }template <typename T> inline void read(T &x){    T f = 1; x = 0;    char c = getchar();    for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f;    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘;    x *= f;}inline void addedge(int u,int v){        tot++;        e[tot] = (edge){v,head[u]};        head[u] = tot;}int main(){                read(n);        if (n == 1) { puts("YES"); return 0; }        for (int i = 1; i < n; i++)        {                int u , v;                read(u); read(v);                addedge(u,v);                addedge(v,u);        }        for (int i = 1; i <= n; i++) read(a[i]);        if (a[1] != 1) { puts("NO"); return 0; }        queue< int > q;        fa[1] = -1;        q.push(1);        while (!q.empty())        {                int u = q.front();                q.pop();                for (int i = head[u]; i; i = e[i].nxt)                {                        int v = e[i].to;                        if (v != fa[u])                        {                                size[u]++;                                fa[v]    = u;                                q.push(v);                            }                }                }        int t = 1 , cnt = 0;        for (int i = 2; i <= n; i++)        {                if (fa[a[i]] == a[t])                 {                        cnt++;                        continue;                }                if (cnt == size[a[t]])                {                        while (t < i && size[a[t + 1]] == 0) t++;                        if (t == i)                        {                                printf("NO\n");                                return 0;                        }                        cnt = 1;                        t++;                        continue;                    }                printf("NO\n");                return 0;                }        if (cnt == size[a[t]]) printf("YES\n");        else printf("NO\n");                return 0;    }

 

[Codeforces 1037D] Valid BFS?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.