CodeForces 402 E Strictly Positive Matrix

Strictly Positive Matrix

题解:

如果原来的 a[i][j] = 0, 现要 a[i][j] = 1, 那么等于 sum{a[i][k] + a[k][j]} > 1。

如果把a[i][j]视作 i -> j 是否能达到。

那么对于上述的那个方程来说,相当于 i先走到k, k再走到j。 单向边。

 

所以化简之后,就是询问一幅图是不是只有一个强连通缩点。

 

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 2e3 + 10;
const int M = N * N;
int head[N], nt[M], to[M], tot;
void add(int u, int v){
    to[tot] = v;
    nt[tot] = head[u];
    head[u] = tot++;
}
int belong[N], dfn[N], low[N], now_time, scc_cnt;
stack<int> s;
void dfs(int u){
    dfn[u] = low[u] = ++now_time;
    s.push(u);
    for(int i = head[u]; ~i; i = nt[i]){
        if(!dfn[to[i]]) dfs(to[i]);
        if(!belong[to[i]]) low[u] = min(low[u], low[to[i]]);
    }
    if(dfn[u] == low[u]){
        ++scc_cnt;
        int now;
        while(1){
            now = s.top(); s.pop();
            belong[now] = scc_cnt;
            if(now == u) break;
        }
    }
}
void scc(int n){
    now_time = scc_cnt = 0;
    for(int i = 1; i <= n; ++i)
        if(!belong[i]) dfs(i);
}
int main(){
    memset(head, -1, sizeof(head));
    int n, t;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            scanf("%d", &t);
            if(t) add(i, j);
        }
    }
    scc(n);
//    cout << "?" << endl;
    if(scc_cnt ^ 1) puts("NO");
    else puts("YES");
    return 0;
}
View Code

 

posted @ 2019-05-15 09:57  Schenker  阅读(110)  评论(0编辑  收藏  举报