【codevs 1506】傳話_===搜尋===

來源:互聯網
上載者: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

這明明是個BFS。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxn=5000+5;int first[maxn],nxt[maxn],tot=0;int a[maxn];bool used[maxn];int n,m;struct edge{    int f,t;}l[maxn];void build(int f,int t){    l[++tot]=(edge){f,t};    nxt[tot]=first[f];    first[f]=tot;    return;}bool bfs(int s){    queue<int>q;    memset(used,0,sizeof(used));    while(!q.empty()) q.pop();    q.push(s);    used[s]=1;    while(!q.empty())    {        int f=q.front();        q.pop();        for(int i=first[f];i;i=nxt[i])        {            int w=l[i].t;            if(w==s) return 1;            else if(!used[w])            q.push(w),used[w]=1;        }    }    return 0;}int main(){    int f,t;    scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++)    {        scanf("%d%d",&f,&t);        build(f,t);    }    for(int i=1;i<=n;i++)    {        if(bfs(i)) puts("T\n");        else puts("F\n");    }    return 0;}

既然DFS的做法不會,,那就學學。

從某部落格裡學來的DFS

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=1000+10;bool s[maxn][maxn],vis[maxn][maxn],flag;int n,m;void dfs(int x,int y){    if(flag==1) return;    if(s[x][y])    {        flag=1;        return;    }    for(int i=1;i<=n;i++)    {        if(s[x][i]&&!vis[x][i])        {            vis[x][i]=true;            dfs(i,y);        }    }}int main(){    int a,b;    scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++)    {        scanf("%d%d",&a,&b);        s[a][b]=1;    }    for(int i=1;i<=n;i++)    {        flag=0;        memset(vis,0,sizeof(vis));        dfs(i,i);        if(flag) puts("T\n");        else puts("F\n");    }    return 0;}

然後剛知道這是傳遞閉包的思想。
正宗的傳遞閉包如下:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=1000+1;int n,m;bool a[maxn][maxn];void floyd(){    for(int k=1;k<=n;k++)    for(int i=1;i<=n;i++)    for(int j=1;j<=n;j++)    {        if(a[i][k]&&a[k][j])        a[i][j]=1;    }}int main(){    int x,y;    scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++)    {        scanf("%d%d",&x,&y);        a[x][y]=1;    }    floyd();    for(int i=1;i<=n;i++)    {        if(a[i][i]) puts("T\n");        else puts("F\n");    }    return 0;}

原理是floyd。平均複雜度比DFS要慢一些,思想差不多。
不過傳遞閉包是什麼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.