【牛客 - 330F】Applese 的QQ群(拓扑排序,二分)
生活随笔
收集整理的這篇文章主要介紹了
【牛客 - 330F】Applese 的QQ群(拓扑排序,二分)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題干:
?
Applese 有一個(gè)QQ群。在這個(gè)群中,大家互相請教問題。如 b 向 a 請教過問題,就把 a 叫做是 b 的"老板"。這樣一個(gè)群中就會有很多老板。
同時(shí)規(guī)定:如果 a 是 b 的老板,b 是 c 的老板,那么 a 也是 c 的老板。
為了不破壞群里面和諧交流的氛圍,Applese 定了一個(gè)群規(guī):不允許出現(xiàn) a 既是 b 的老板, b 又是 a 的老板。
你需要幫助 Applese 判斷大家是否遵守了群規(guī)。
輸入描述:
第一行兩個(gè)整數(shù) n, m,表示群里的人數(shù)以及請教問題的數(shù)量。 接下來 m 行,每行兩個(gè)整數(shù) a, b,表示 a 是 b 的"老板",即 b 向 a 請教了一個(gè)問題。 注:無論是否違反了群規(guī),a 都會成為 b 的老板。輸出描述:
對于每次提問,輸出一行"Yes"表示大家都遵守了群規(guī),反之輸出"No"。示例1
輸入
復(fù)制
4 4 1 2 2 3 3 1 1 4輸出
復(fù)制
Yes Yes No No備注:
1≤n≤1051≤n≤105 1≤m≤2?1051≤m≤2?105 1≤a,b≤n?
解題報(bào)告:
? 本來想搞一個(gè)種類并查集、、后來交上去wa了就沒在管,,其實(shí)是個(gè)二分+check、、、(不難的題)check用topu一下就行了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; struct Edge{int u,v; } e[MAX]; int n,m; int in[MAX]; vector<int> vv[MAX]; bool ok(int x) {for(int i = 1; i<=n; i++) in[i] = 0,vv[i].clear();for(int i = 1; i<=x; i++) {in[e[i].v]++;vv[e[i].u].pb(e[i].v);}queue<int> q;for(int i = 1; i<=n; i++) {if(in[i] == 0) q.push(i);}int cnt = 0;while(q.size()) {int cur = q.front();q.pop();int up = vv[cur].size();cnt++;for(int i = 0; i<up; i++) {int to = vv[cur][i];in[to]--;if(in[to] == 0) q.push(to);}}if(cnt != n) return 0;else return 1; } int main() {cin>>n>>m;for(int x,y,i = 1; i<=m; i++) {scanf("%d%d",&x,&y);e[i] = {x,y};}int l = 1,r = m;int mid = (l+r)>>1;int ans ;while(l<=r) {mid = (l+r)>>1;if(ok(mid)) ans = mid,l = mid+1;else r = mid-1;}for(int i = 1; i<=ans; i++) printf("Yes\n");for(int i = ans+1; i<=m; i++) printf("No\n");return 0 ;}?
總結(jié)
以上是生活随笔為你收集整理的【牛客 - 330F】Applese 的QQ群(拓扑排序,二分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 行车记录仪拍下四川山体塌方瞬间 挡风玻璃
- 下一篇: profiler.exe - profi