zoukankan      html  css  js  c++  java
  • 判环

    无向图

    #include <bits/stdc++.h>
    #include <vector>
    #define PI acos(-1.0)
    #define mem(a,b) memset((a),b,sizeof(a))
    #define TS printf("!!!
    ")
    #define pb push_back
    //std::ios::sync_with_stdio(false);
    using namespace std;
    //priority_queue<int,vector<int>,greater<int>> que;
    const double EPS = 1.0e-9;
    typedef pair<int, int> pairint;
    typedef long long ll;
    typedef unsigned long long ull;
    const int  maxn = 1e5 + 10;
    const int  maxm = 300;
    int visit[maxn];
    int flag=0;
    int dfs(int x, int pre)
    {
            visit[x]=true;
            int len=p[x].size();
            for(int i=0;i<len;i++)
            {
                    int to=p[x][i];
                    if(to=pre)
                            continue;
                    if(visit[to]==1)
                            flag=1,
                 return 0;
    else dfs(to,x); } } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { scanf("%d %d", &from, &to); p[from].pb(to); p[to].pb(from); } dfs(1,1); if(flag) cout<<"yes"<<endl; else cout<<"no"<<endl; }

    有向图

    #include <bits/stdc++.h>
    #include <vector>
    #define PI acos(-1.0)
    #define mem(a,b) memset((a),b,sizeof(a))
    #define TS printf("!!!
    ")
    #define pb push_back
    //std::ios::sync_with_stdio(false);
    using namespace std;
    //priority_queue<int,vector<int>,greater<int>> que;
    const double EPS = 1.0e-9;
    typedef pair<int, int> pairint;
    typedef long long ll;
    typedef unsigned long long ull;
    const int  maxn = 1e5 + 10;
    const int  maxm = 300;
    int visit[maxn];
    int flag=0;
    int dfs(int x, int pre)
    {
            visit[x]=true;
            int len=p[x].size();
            for(int i=0;i<len;i++)
            {
                    int to=p[x][i];
                    if(visit[to]==1)
                            flag=1,
                            return ;
                    else
                            dfs(to,x);
            }
    }
    int main()
    {
            int n, m;
            cin  >> n >> m;
            for (int i = 1; i <= m; i++)
            {
                    scanf("%d %d", &from, &to);
                    p[from].pb(to);
            }
            dfs(1);
            if(flag)
            cout<<"yes"<<endl;
            else
            cout<<"no"<<endl;
    }
  • 相关阅读:
    set.end()和lower_bound使用记录
    自学图论的码队弟弟(dfs)
    打字训练
    (回归2.0)A
    斐波那契串 新疆省赛
    异或的路径 新疆省赛 (按位亦或)
    E. Product Oriented Recurrence (矩阵快速幂新模板)
    C. Beautiful Lyrics (模拟)构造
    O(n!)新疆省赛 d (贪心)
    Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/Aragaki/p/7511204.html
Copyright © 2011-2022 走看看