hdu 4324 Triangle LOVE (targan判連通)

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4324


這題也是比較裸的題,


判斷是否存在超過三個節點的強連通分量,由於題目說不存在兩個節點的強連通分量。直接統計連通分量,如果總數小於初始的頂點數的話,就表示存在超過三個節點的強連通分量。


代碼:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <iostream>

using namespace std;
const int maxn = 2005;
char G[maxn][maxn];

int t, n;
int dfn[maxn];
int low[maxn];
bool instack[maxn];
int Stack[maxn];
int index = 0, cnt = 0, top = 0;
void targan(int v) {
    dfn[v] = low[v] = ++index;
    instack[v] = true;
    Stack[++top] = v;
    for (int i = 0; i < n; i ++) {
        if(G[v][i] == '1') {
            if(!dfn[i]) {
                targan(i);
                low[v] = min(low[v], low[i]);
            }
            else if(instack[i]) {
                low[v] = min(low[v], dfn[i]);
            }
        }
    }
    if(dfn[v] == low[v]) {
        cnt ++;
        int k;
        do {
            k = Stack[top--];
            instack[k] = false;
        } while(k != v);
    }
}
int main() {
    scanf ("%d", &t);
    for (int i = 1; i <= t; i ++) {
        scanf("%d", &n);
        memset(G, 0, sizeof(G));
        for (int j = 0; j < n; j ++) {
            scanf("%s", G[j]);
        }
        printf("Case #%d: ", i);
        memset(dfn, 0, sizeof(dfn));
        index = cnt = top = 0;
        for (int i = 0; i < n; i ++) {
            if(!dfn[i]) {
                targan(i);
            }
        }
        if(cnt == n) {
            printf("No\n");
        }
        else {
            printf("Yes\n");
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章