Codevs 1506 傳話

來源:互聯網
上載者:User
 時間限制: 1 s   空間限制: 128000 KB   題目等級 : 白銀 Silver  題目描述  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

 1 #include<iostream> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 int map[1001][1001]; 6 int used[1001]; 7 int n,m; 8 bool bfs(int i){ 9     memset(used,0,sizeof(used));10     queue<int> Q;11     Q.push(i);12     int cnt=0;13     while(!Q.empty()){14     int x=Q.front(); Q.pop();15     if(x==i&&cnt!=0) return 1;16     cnt++;17     for(int k=1;k<=n;k++){18         if(map[x][k]==1&&!used[k])Q.push(k),used[k]=1;19         }20     }21     return 0;22 }23 24 int main(){25     cin>>n>>m;26     sizeof(map,0,sizeof(map));27     for(int i=1;i<=m;i++){28         int x,y;29         cin>>x>>y;30         map[x][y]=1;31     }32     for(int i=1;i<=n;i++){33         bool FL=bfs(i);34         if(!FL) cout<<'F'<<endl;35         else cout<<'T'<<endl;36     }37     return 0;38 }

思路:快搜,每當我再搜到你的時候~~~

聯繫我們

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