zoukankan      html  css  js  c++  java
  • 【CS Round #44 (Div. 2 only) C】Check DFS

    【链接】点击打开链接


    【题意】


    给你一个n节点,m条边的无向联通图.
    给你一个节点访问的顺序.(1..n的排列)
    你可以改变每个点优先访问的出度.(但必须按照dfs的规则);
    问你能不能按照所给的访问顺序访问所有的点。

    【题解】


    模拟题。
    按照要求,看看当前到达的点的出度里面有没有下一个点.
    没有的话,看看这个点是不是没有其他可以到达的点了。(访问过的点就不能再访问了)
    如果是这样的话,就返回上一层的点继续搜,否则的话直接输出无解.
    (因为如果有其他的点可以到达的话,显然必须得先走那些点,这样就和所需的顺序不同了)
    找到一个目标的点之后,就进入那层递归。
    然后如果这个点x还有其他的出度的话,还得对这个点进行出度的搜索。
    这样就能模拟dfs的过程了。

    【错的次数】


    3

    【反思】


    一开始用了另外一种方法写,好像不是很靠谱

    【代码】

    /*
    
    */
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <queue>
    #include <iomanip>
    #include <set>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb emplace_back
    #define fi first
    #define se second
    #define ld long double
    #define ms(x,y) memset(x,y,sizeof x)
    #define ri(x) scanf("%d",&x)
    #define rl(x) scanf("%lld",&x)
    #define rs(x) scanf("%s",x)
    #define rf(x) scnaf("%lf",&x)
    #define oi(x) printf("%d",x)
    #define ol(x) printf("%lld",x)
    #define oc putchar(' ')
    #define os(x) printf(x)
    #define all(x) x.begin(),x.end()
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0)
    #define sz(x) ((int) x.size())
    #define ld long double
    
    typedef pair<int, int> pii;
    typedef pair<LL, LL> pll;
    
    //mt19937 myrand(time(0));
    //int get_rand(int n){return myrand()%n + 1;}
    const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
    const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
    const double pi = acos(-1.0);
    const int N = 1e5;
    
    int n, m, p[N + 10], cnt = 2;
    vector <int> g[N + 10];
    bool bo[N + 10];
    
    void dfs(int x) {
    	if (cnt > n) {
    		puts("1");
    		exit(0);
    	}
    	bo[x] = true;
    	int len = sz(g[x]), c = 0, fi = 0;
    	rep1(i, 0, len - 1) {
    		int y = g[x][i];
    		if (!bo[y]) {
    			c++;
    			if (y == p[cnt]) fi = 1;
    		}
    	}
    	if (fi) {
    		dfs(p[cnt++]);
    		if (c > 1) dfs(x);
    	}
    	else {
    		if (c > 0) {
    			puts("0");
    			exit(0);
    		}
    	}
    }
    
    int main() {
    	//Open();
    	//Close();
    	ri(n), ri(m);
    	rep1(i, 1, n) ri(p[i]);
    	rep1(i, 1, m) {
    		int x, y;
    		ri(x), ri(y);
    		g[x].pb(y), g[y].pb(x);
    	}
    	dfs(1);
    	puts("0");
    	return 0;
    }
    


  • 相关阅读:
    k8s 节点的 NodeAffinity 使用
    template 与 host , item trigger的关系
    mysql 性能优化思路
    nginx 配sorry page
    修改tomcat JVM 大小 jdk--目录修改
    (转)MySQL慢查询分析优化 + MySQL调优
    注册表操作 Microsoft.Win32.Registry与RegistryKey类
    C#(99):WCF之.NET Remoting通讯
    CallContext线程数据缓存-调用上下文
    C#(99):JSON与对象的序列化与反序列化
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626066.html
Copyright © 2011-2022 走看看