zoukankan      html  css  js  c++  java
  • [HIHO1041]国庆出游(DFS, bitset)

    题目链接:http://hihocoder.com/problemset/problem/1041

    学会了用C++的bitset哈,可喜可贺。以后遇到超过64位想用位来表示状态就不愁了哈。

    这题用bitset存从节点1出发开始dfs,某点按照dfs顺序能够到达的所有点的集合。后序更新这个bitset,用或运算把下属的可达节点都更新上来。

    先一遍dfs把bitset更新出来,再一遍dfs判断题中所给顺序是否合法。第二遍dfs的时候就按照给定的顺序扫描,根据某一个分支是不是有目标节点来判断退出。

      1     /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%lld", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onenum(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef pair<LL, LL> pll;
     65 typedef map<string, int> msi;
     66 typedef vector<int> vi;
     67 typedef vector<LL> vl;
     68 typedef vector<vl> vvl;
     69 typedef vector<bool> vb;
     70 
     71 const int maxn = 110;
     72 bitset<maxn> son[maxn];
     73 int n, m, cur;
     74 int pos[maxn];
     75 bool vis[maxn];
     76 vi edge[maxn];
     77 bool flag;
     78 
     79 void dfs1(int u, int p) {
     80     son[u][u] = 1; vis[u] = 1;
     81     Rep(i, edge[u].size()) {
     82         int v = edge[u][i];
     83         if(!vis[v] && p != v) {
     84             dfs1(v, u);
     85             son[u] |= son[v];
     86         }
     87     }
     88 }
     89 
     90 void dfs2(int u, int p) {
     91     if(flag) return;
     92     if(u == pos[cur]) cur++;
     93     if(cur > m) {
     94         flag = 1;
     95         return;
     96     }
     97     vis[u] = 1;
     98     while(cur <= m) {
     99         int tmp = cur;
    100         Rep(i, edge[u].size()) {
    101             int v = edge[u][i];
    102             if(p != v && son[v][pos[cur]] && !vis[v]) {
    103                 dfs2(v, u);
    104             }
    105         }
    106         if(tmp == cur) break;
    107     }
    108 }
    109 
    110 int main() {
    111     // FRead();
    112     int T, u, v;
    113     Rint(T);
    114     W(T) {
    115         Rep(i, n+5) {
    116             edge[i].cl();
    117             son[i].reset();
    118         }
    119         Cls(vis); flag = 0; cur = 1;
    120         Rint(n);
    121         Rep(i, n-1) {
    122             Rint(u); Rint(v);
    123             edge[u].pb(v);
    124             edge[v].pb(u);
    125         }
    126         Rint(m);
    127         For(i, 1, m+1) Rint(pos[i]);
    128         dfs1(1, -1);
    129         Cls(vis);
    130         dfs2(1, -1);
    131         printf("%s
    ", flag ? "YES" : "NO");
    132     }
    133     RT 0;
    134 }
  • 相关阅读:
    text-overflow:ellipsis; 使用
    js jquery jquery.wordexport.js 实现导出word
    CI框架程序--本地调试之后部署新浪SAE
    跳转页面的几种方式[归纳整理中......]
    使用jquery插件报错:TypeError:$.browser is undefined的解决方法
    apache Internal Server Error 的几个问题
    【总结整理】用户的需求分析:问对问题才能找准用户需求----摘自《人人都是产品经理》
    【总结整理】如何成为小白用户----摘自《人人都是产品经理》
    【总结整理双十一促销门道---摘自《人人都是产品经理》
    【总结整理】2018淘宝双11评价
  • 原文地址:https://www.cnblogs.com/kirai/p/5593410.html
Copyright © 2011-2022 走看看