codevs1506傳話(tarjan)_tarjan

來源:互聯網
上載者:User

這個題多種解法,我用tarjan又寫了一遍

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <stack>using namespace std;const int MAXN = 2000;int n,m,to[MAXN<<1],head[MAXN],cd[MAXN],blt[MAXN],nxt[MAXN<<1],tot,scc[MAXN],sz[MAXN],tim=0;bool vis[MAXN],flag = 0;struct Edge{    int from,to;}e[MAXN<<1];stack <int> s;void build(int f,int t){    to[++tot] = t;    nxt[tot] = head[f];    head[f]  = tot;}int low[MAXN],dfn[MAXN],scccnt = 0; void dfs(int u){    low[u] = dfn[u] = ++tim;    s.push(u);    int v;    for(int i = head[u] ; i ; i = nxt[i])    {        v = to[i];        if(!dfn[v])        {            dfs(v);            low[u] = min(low[u],low[v]);        }        else if(!scc[v])        {            low[u] = min(low[u],dfn[v]);        }    }    if(low[u] == dfn[u])    {        int x;        scccnt ++;        while(!s.empty())        {            x = s.top();            s.pop();            scc[x] = scccnt;            sz[scccnt]++;            if(u == x)            break;        }    }}int main(){    scanf("%d%d",&n,&m);    for(int i = 1; i <= m; i ++)    {        scanf("%d%d",&e[i].from ,& e[i].to);        build(e[i].from ,e[i].to);    }    for(int i = 1; i <= n; i ++)    if(!dfn[i])    dfs(i);    int ans = 0,q = 0;    int xx[5000];    for(int i = 1; i <= scccnt ; i ++)    {        if(sz[i]>1)        {            q ++;            xx[q] = i;        }    }    for(int j = 1; j <= q; j ++)    for(int i = 1; i <= n; i ++)    {        if(scc[i] == xx[j])        {            vis[i] = 1;        }    }    for(int i = 1; i <= n; i ++)    {        if(vis[i])        puts("T");        else        puts("F");    }    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.