码迷,mamicode.com
首页 > 其他好文 > 详细

【Educational Codeforces Round 36 D】 Almost Acyclic Graph

时间:2018-01-14 10:58:42      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:cli   name   pre   题意   def   memset   删掉   链接   ever   

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


找到任意一个环。
然后枚举删掉其中的某一条边即可。
(因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边
(其它环,如果都包括这条边,那么就可以,否则,某个环不包括那也没办法,自然就无解了。
这样枚举的边的数目最多是500(环最多N条边)
然后复杂度就是500*10万了。
足够过了

【代码】

#include <bits/stdc++.h>
using namespace std;

int n, m;
vector<int> G[550];
int cor[550];
vector<int> path;

int dfs(int v){
    cor[v] = 1;
    for(int r : G[v]){
        if(!cor[r] and dfs(r) ){
            path.push_back( r );
            return 1;
        }else if( cor[r] == 1){
            path.push_back( r );
            return 1;
        }
    }
    cor[v] = 2;
    return 0;
}

int dfs2(int v, int a, int b){
    cor[v] = 1;
    for(int r : G[v]){
        if(v == a and r == b) continue;
        if(!cor[r] and dfs2(r, a, b) ){
            return 1;
        }else if( cor[r] == 1){
            return 1;
        }
    }
    cor[v] = 2;
    return 0;
}

int main(){
    scanf("%d %d", &n,&m);
    for(int i = 0; i < m; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        G[x].push_back(y);
    }
    int ciclo = 0;
    for(int i = 1; i <= n; i++){
        if(!cor[i]) ciclo = ciclo or dfs(i);
    }
    if(!ciclo){
        printf("YES\n");
        return 0;
    }
    reverse(path.begin(), path.end());
    path.push_back( path[0] );
    /*
    for(int p: path){
        printf(">> %d\n", p);
    }
    */
    for(int i = 1; i < path.size(); i++){
        memset(cor, 0, sizeof cor);
        ciclo = 0;
        for(int j = 1; j <= n; j++){
            if(!cor[j]) ciclo = (ciclo or dfs2(j, path[i-1], path[i]));
        }
        if(!ciclo){
            printf("YES\n", i);
            return 0;
        }
    }

    printf("NO\n");
    return 0;

}

【Educational Codeforces Round 36 D】 Almost Acyclic Graph

标签:cli   name   pre   题意   def   memset   删掉   链接   ever   

原文地址:https://www.cnblogs.com/AWCXV/p/8282387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!