生活随笔
收集整理的這篇文章主要介紹了
164. 可达性统计【拓扑排序 / bitset】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
無環說明一定有拓撲排序。
我們得到拓撲排序,倒著推。注意壓位。
#include<bits/stdc++.h>
#include<bitset>
using namespace std
;
const int N
=30010;
int h
[N
],e
[N
],ne
[N
],idx
;
int d
[N
],n
,m
;
vector
<int>ve
;
bitset
<N
>cnt
[N
];
void add(int a
,int b
)
{e
[idx
]=b
,ne
[idx
]=h
[a
],h
[a
]=idx
++;
}
void top_sort()
{queue
<int>q
;for(int i
=1;i
<=n
;i
++) if(!d
[i
]) q
.push(i
);while(q
.size()){int u
=q
.front(); q
.pop();ve
.push_back(u
);for(int i
=h
[u
];i
!=-1;i
=ne
[i
]){int j
=e
[i
];if(--d
[j
]==0) q
.push(j
);}}
}
int main(void)
{memset(h
,-1,sizeof h
);cin
>>n
>>m
;for(int i
=0;i
<m
;i
++){int a
,b
; cin
>>a
>>b
;d
[b
]++;add(a
,b
);}top_sort();for(int i
=ve
.size()-1;i
>=0;i
--){int u
=ve
[i
];cnt
[u
][u
]=1;for(int j
=h
[u
];j
!=-1;j
=ne
[j
]){int k
=e
[j
];cnt
[u
]|=cnt
[k
];}}for(int i
=1;i
<=n
;i
++) cout
<<cnt
[i
].count()<<endl
;return 0;
}
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的164. 可达性统计【拓扑排序 / bitset】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。