【Codeforces Beta Round #88 C】Cycle

Link:http://codeforces.com/problemset/problem/117/C

Description

问你一张图里面有没有一个三元环,有的话就输出。

Solution

O(N2)写个图的遍历就行;
主程序里的for循环,是防止多个连通块.

NumberOf WA

0

Reviw


Code

#include <bits/stdc++.h>
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 push_back
#define fi first
#define se second
#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+1)
#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)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

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 = 5000;

int n,v[N+10];
char s[N+10][N+10];

bool dfs(int x,int fa){
    v[x] = 1;
    rep1(i,1,n)
        if (s[x][i]-'0'){
            if (i!=fa && (s[i][fa]-'0')){
                oi(x),oc,oi(i),oc,oi(fa),puts("");
                return true;
            }
            if (!v[i])
                if (dfs(i,x)) return true;
        }
    return false;
}

int main(){
    //Open();
    //Close();
    ri(n);
    rep1(i,1,n) rs(s[i]);

    rep1(i,1,n)
        if (!v[i])
            if (dfs(i,i)) return 0;
    puts("-1");
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(103)  评论(0编辑  收藏  举报