codevs 1506 傳話 Tarjan 解題報告_codevs

來源:互聯網
上載者:User
題目描述 Description

一個朋友網路,如果a認識b,那麼如果a第一次收到某個訊息,那麼會把這個訊息傳給b,以及所有a認識的人。

如果a認識b,b不一定認識a。

所有人從1到n編號,給出所有“認識”關係,問如果i發布一條新訊息,那麼會不會經過若干次傳話後,這個訊息傳回給了i,1<=i<=n。 輸入描述 Input Description

第一行是n和m,表示人數和認識關係數。

接下來的m行,每行兩個數a和b,表示a認識b。1<=a, b<=n。認識關係可能會重複給出,但一行的兩個數不會相同。 輸出描述 Output Description

一共n行,每行一個字元T或F。第i行如果是T,表示i發出一條新訊息會傳回給i;如果是F,表示i發出一條新訊息不會傳回給i。 範例輸入 Sample Input

4 6

1 2

2 3

4 1

3 1

1 3

2 3 範例輸出 Sample Output

T

T

T

F 資料範圍及提示 Data Size & Hint

n<=1000

1<=a, b<=n 思路

其實就是判一個環。Tarjan直接來就好了。 代碼

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<vector>using namespace std;const int N=1000+5;int n,m,cnt,l,dfn[N],low[N],front[N],s[N];bool v[N],ans[N];int cc[N],top=1;struct node{    int next,to;}e[N*N];void add(int x,int y,int d){    e[d].to=y;    e[d].next=front[x];    front[x]=d;}void push(int x){    s[top++]=x;    v[x]=true;}void pop(int ll){    top--;    v[s[top]]=false;    cc[ll]=s[top];}void tarjer(int k)//目前根節點 {    dfn[k]=low[k]=++cnt;    push(k);    for (int i=front[k];i;i=e[i].next)//枚舉與他相連的每一條邊     {        int t=e[i].to;//指向的點         if (!dfn[t]) {tarjer(t);low[k]=min(low[k],low[t]);}        else {if (v[t]) low[k]=min(low[k],dfn[t]);}    }        if (low[k]==dfn[k])    {        l=0;        while(dfn[s[top-1]]!=dfn[k])        {l++;pop(l);}        pop(++l);    }    if (l>1)    {        for (int i=1;i<=l;i++)        ans[cc[i]]=true;    }        }int main(){    scanf("%d%d",&n,&m);    for (int i=1;i<=m;i++)    {        int x,y;        scanf("%d%d",&x,&y);        add(x,y,i);    }    for (int i=1;i<=n;i++)    if (!dfn[i]) tarjer(i);    for (int i=1;i<=n;i++)    if (ans[i]) printf("T\n");    else printf("F\n");    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.