日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【牛客 - 330F】Applese 的QQ群(拓扑排序,二分)

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【牛客 - 330F】Applese 的QQ群(拓扑排序,二分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:
?

Applese 有一個QQ群。在這個群中,大家互相請教問題。如 b 向 a 請教過問題,就把 a 叫做是 b 的"老板"。這樣一個群中就會有很多老板。


同時規定:如果 a 是 b 的老板,b 是 c 的老板,那么 a 也是 c 的老板。

為了不破壞群里面和諧交流的氛圍,Applese 定了一個群規:不允許出現 a 既是 b 的老板, b 又是 a 的老板。

你需要幫助 Applese 判斷大家是否遵守了群規。

輸入描述:

第一行兩個整數 n, m,表示群里的人數以及請教問題的數量。 接下來 m 行,每行兩個整數 a, b,表示 a 是 b 的"老板",即 b 向 a 請教了一個問題。 注:無論是否違反了群規,a 都會成為 b 的老板。

輸出描述:

對于每次提問,輸出一行"Yes"表示大家都遵守了群規,反之輸出"No"。

示例1

輸入

復制

4 4 1 2 2 3 3 1 1 4

輸出

復制

Yes Yes No No

備注:

1≤n≤1051≤n≤105 1≤m≤2?1051≤m≤2?105 1≤a,b≤n

?

解題報告:

? 本來想搞一個種類并查集、、后來交上去wa了就沒在管,,其實是個二分+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 ;}

?

總結

以上是生活随笔為你收集整理的【牛客 - 330F】Applese 的QQ群(拓扑排序,二分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。