16940

Worldi·2021년 8월 23일
0

알고리즘

목록 보기
11/59
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#define MAX 100001

using namespace std;

int dist[MAX];
bool check[MAX];
vector<int> a[MAX];
queue<int> sol;
int parent[MAX];
int cnt=0;

bool solflag = true;
void bfs(int start) {
    int real = sol.front();
    sol.pop();
    queue<int> q;
    q.push(real);
    check[real] = true;
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        set<int>s;
        for (int y: a[x]) {
            if ( check[y] == false) {
                check[y]  = true;
                s.insert(y);
                //q.push(y);
            }
        }  
        for (int i = 0 ; i< s.size() ; i++) {
            int temp = sol.front();
            sol.pop();
            if(s.count(temp)==0) {
                cout<<"0";
                return ;
            }
            else {
                q.push(temp);
            }
        }
        
    
    }
    cout<<"1";
    return ;
}

int main(void) {

    cin.tie(NULL);
    ios_base::sync_with_stdio(0);
    cout.tie(NULL);
    int n;
    cin>>n;
    for (int i= 0  ; i< n-1 ; i++) {
        int to, from;
        cin>>to>>from;
        a[to].push_back(from);
        a[from].push_back(to);
    }
    for (int i = 0 ; i< n ; i++) {
        int s;
        cin>>s;
        sol.push(s);
    }
    int temp = sol.front();
    if( temp!=1)  {cout<<"0"; return 0;}
    bfs(0);
    
   
    return 0;
}
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#define MAX 100001

using namespace std;

int dist[MAX];
bool check[MAX];
vector<int> a[MAX];
queue<int> sol;
int order[MAX];
int cnt=0;
vector<int> sol2;
bool solflag = true;
void bfs(int start) {
    int real = sol.front();
    sol.pop();
    queue<int> q;
    q.push(real);
    check[real] = true;
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        set<int>s;
        for (int y: a[x]) {
            if ( check[y] == false) {
                check[y]  = true;
                s.insert(y);
                //q.push(y);
            }
        }  
        for (int i = 0 ; i< s.size() ; i++) {
            int temp = sol.front();
            sol.pop();
            if(s.count(temp)==0) {
                cout<<"0";
                return ;
            }
            else {
                q.push(temp);
            }
        }
        
    
    }
    cout<<"1";
    return ;
}

int main(void) {

    cin.tie(NULL);
    ios_base::sync_with_stdio(0);
    cout.tie(NULL);
    int n;
    cin>>n;
    for (int i= 0  ; i< n-1 ; i++) {
        int to, from;
        cin>>to>>from;
        a[to].push_back(from);
        a[from].push_back(to);
    }
    for (int i = 0 ; i< n ; i++) {
        int s;
        cin>>s;
        sol2.push_back(s);
    }
    for (int i = 0 ; i< n ; i++) {
        order[sol2[i]] = i;
    }
   for (int i=1; i<=n; i++) {
        sort(a[i].begin(), a[i].end(), [&](const int &u, const int &v) {
            return order[u] < order[v];
        });
    }
  vector<int> bfs_order;
    queue<int> q;
    q.push(1);
    check[1] = true;
    while (!q.empty()) {
        int x = q.front(); q.pop();
        bfs_order.push_back(x);
        for (int y : a[x]) {
            if (check[y] == false) {
                check[y] = true;
                q.push(y);
            }
        }
    }
    if (bfs_order == sol2) {
        cout << 1 << '\n';
    } else {
        cout << 0 << '\n';
    }
    return 0;

}
profile
https://worldi.tistory.com/ 로 블로그 이전합니다.

0개의 댓글