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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

P2017 [USACO09DEC]晕牛Dizzy Cows

發布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 P2017 [USACO09DEC]晕牛Dizzy Cows 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

圖論日常不會系列。。。

題意:給定有向邊和無向邊,然后給每一條無向邊定向,使得到的圖無環。

我本來想縮一下點的,但是越想越暈。

然后就翻了題解,恍然大悟。。。

其實只需要給只有有向邊的圖跑一次toposort。然后把無向邊的定向看做是在添加有向邊。

顯然不要違反拓撲序來添加有向邊,這個圖就不可能有環!

所以隨便搞一搞就好了。。。

代碼:

#include<cstdio>#include<queue> const int maxn = 100005; struct Edges {int next, to; } e[maxn * 3]; int head[maxn], tot; int indegree[maxn]; int topo[maxn], ttot; int where[maxn]; struct Query {int u, v; } Q[maxn]; int n, m1, m2; int read() {int ans = 0, s = 1;char ch = getchar();while(ch > '9' || ch < '0'){ if(ch == '-') s = -1; ch = getchar(); }while(ch >= '0' && ch <= '9') ans = (ans << 3) + (ans << 1) + ch - '0', ch = getchar();return s * ans; } void link(int u, int v) {e[++tot] = (Edges){head[u], v};head[u] = tot; } void toposort() {std::queue<int> q;for(int i = 1; i <= n; i++) if(indegree[i] == 0) q.push(i);while(!q.empty()){int u = q.front(); q.pop();topo[++ttot] = u;for(int i = head[u]; i; i = e[i].next){int v = e[i].to;indegree[v]--;if(indegree[v] == 0){q.push(v);}}} } int main() {n = read(), m1 = read(), m2 = read();while(m1--){int u = read(), v = read();link(u, v);indegree[v]++;}for(int i = 1; i <= m2; i++){Q[i].u = read(), Q[i].v = read();}toposort();for(int i = 1; i <= ttot; i++) where[topo[i]] = i;for(int i = 1; i <= m2; i++){int temp1 = where[Q[i].u], temp2 = where[Q[i].v];if(temp1 < temp2) printf("%d %d\n", Q[i].u, Q[i].v);else printf("%d %d\n", Q[i].v, Q[i].u);}return 0; }

轉載于:https://www.cnblogs.com/Garen-Wang/p/9794956.html

總結

以上是生活随笔為你收集整理的P2017 [USACO09DEC]晕牛Dizzy Cows的全部內容,希望文章能夠幫你解決所遇到的問題。

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