Codeforces 475B Strongly Connected City 強連通裸題,codeforces475b

來源:互聯網
上載者:User

Codeforces 475B Strongly Connected City 強連通裸題,codeforces475b

題目連結:點擊開啟連結

題意:

就是n*m的矩陣,

每行能走的方向

每列能走的方向

問:圖是否強連通。

只要4個邊界成環即可。

或者無腦tarjan

==

#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <vector>#include <string.h>using namespace std;int haifei;#define N 1000//N為最大點數#define M 500*10//M為最大邊數int n, m;struct Edge{    int from, to, nex;}edge[M<<1];int head[N], edgenum;void add(int u, int v){//邊的起點和終點    Edge E={u, v, head[u]};    edge[edgenum] = E;    head[u] = edgenum++;}int DFN[N], Low[N], Stack[N], top, Time; //Low[u]是點集{u點及以u點為根的子樹} 中(所有反向弧)能指向的(離根最近的祖先v) 的DFN[v]值(即v點時間戳記)int taj;//連通分支標號,從1開始int Belong[N];//Belong[i] 表示i點屬於的連通分支bool Instack[N];vector<int> bcc[N]; //標號從1開始void tarjan(int u ,int fa){    DFN[u] = Low[u] = ++ Time ;    Stack[top ++ ] = u ;    Instack[u] = 1 ;    for (int i = head[u] ; ~i ; i = edge[i].nex ){        int v = edge[i].to ;        if(DFN[v] == -1)        {            tarjan(v , u) ;            Low[u] = min(Low[u] ,Low[v]) ;        }        else if(Instack[v]) Low[u] = min(Low[u] ,DFN[v]) ;    }    if(Low[u] == DFN[u]){        int now;        taj ++ ; bcc[taj].clear();        do{            now = Stack[-- top] ;            Instack[now] = 0 ;            Belong [now] = taj ;            bcc[taj].push_back(now);        }while(now != u) ;    }}void tarjan_init(int all){    memset(DFN, -1, sizeof(DFN));    memset(Instack, 0, sizeof(Instack));    top = Time = taj = 0;    for(int i=1;i<=all;i++)if(DFN[i]==-1 )tarjan(i, i); //注意開始點標!!!}void init(){memset(head, -1, sizeof(head)); edgenum=0;}char h[50],l[50];int Hash(int x, int y){ return (x-1)*m+y; }bool solve(){    init();    for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++) {        if(l[j] == '^' && i-1>=1)            add(Hash(i,j), Hash(i-1,j));        if(l[j]=='v'&&i+1<=n)            add(Hash(i,j), Hash(i+1,j));        if(h[i] == '<' && j-1 >= 1)            add(Hash(i,j), Hash(i, j-1));        if(h[i]=='>'&&j+1 <= m)            add(Hash(i,j), Hash(i, j+1));    }    tarjan_init(n*m);    return taj <= 1;}int main(){     while(cin>>n>>m){        scanf("%s", h+1);        scanf("%s", l+1);        solve() ? puts("YES") : puts("NO");     }     return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.