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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Dizzy Cows(拓扑)

發布時間:2024/9/3 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dizzy Cows(拓扑) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/1077/D
來源:牛客網

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
Special Judge, 64bit IO Format: %lld
題目描述
The cows have taken to racing each other around the farm but they get very dizzy when running in circles, and everyone knows that dizzy cows don’t produce any milk. Farmer John wants to convert all of the two-way cow paths in the farm to one-way paths in order to eliminate any ‘cycles’ and prevent the cows from getting dizzy. A ‘cycle’ enables a cow to traverse one or more cow paths and arrive back at her starting point, thus completing a loop or circle.
The farm comprises N pastures (1 <= N <= 100,000) conveniently numbered 1…N. M1 (1 <= M1 <= 100,000) one-way cow paths and M2 two-way cow paths (1 <= M2 <= 100,000) connect the pastures. No path directly connects a pasture to itself, although multiple paths might connect two different pastures. A cow may or may not be able to travel between any two given pastures by following a sequence of cow paths.
Your job is to assign a direction to the two-way cow paths such that the entire farm (ultimately with only one-way paths) has no cycles. That is, there should be no sequence of one-way cow paths which leads back to its starting position. The existing one-way cow paths do not form a cycle and should be left as they are.
One-way cow paths run from pasture Ai (1 <= Ai <= N) to pasture Bi (1 <= Bi <= N). Two-way cow paths connect pastures Xi (1 <= Xi <= N) and Yi (1 <= Yi <= N).
Consider this example:

1-->2| /|| / ||/ |3<--4

The cow paths between pastures 1 and 3, 2 and 3, and 2 and 4 are two-way paths. One-way paths connect 1 to 2 and also 4 to 3. One valid way to convert the two-way paths into one-way paths in such a way that there are no cycles would be to direct them from 1 to 3, from 2 to 3, and from 3 to 4:

1-->2| /|| / |vL v3<--4

輸入描述:

  • Line 1: Three space separated integers: N, M1, and M2
  • Lines 2…1+M1: Line i+1 describes a one-way cow path using two space separated integers: Ai and Bi
  • Lines 2+M1…1+M1+M2: Line i+M1+1 describes a two-way cow path using two space separated integers: Xi and Yi
    輸出描述:
  • Lines 1…M2: Line i should contain two space-separated integers: either Xi and Yi or Yi and Xi, depending on the direction assigned to the i-th two-way path. The two-way paths must appear in the same order in the output as they do in the input. If there is no solution, output “-1” on a single line.
    示例1
    輸入
    復制
4 2 3 1 2 4 3 1 3 4 2 3 2

輸出
復制

1 3 4 2 2 3

/*
題意是:給無向邊標定方向,使圖不構成環。
不構成環,很容易想到拓撲排序

對應代碼2:
一開始我是加入了所以邊進行拓撲排序(不算無向邊的度),
當隊列中出來的這個點有無向邊與它相連時,輸出:當前點–>通過無向邊與之相連的點,
但是我不知道如何 標記 那已經輸出的那條無向邊(即方向已經確定的邊)已經確定方向了,
,不標記的化會再輸出它的反向邊。
(后來傻不拉幾的才反映過來實質是一個反向邊標記問題
快速找到該條邊的反向邊標號,并標記不可用)。
如何快速找到一條邊的反向邊?(每條邊已經標號并且方向邊標號相鄰):

nowID ^1 為反向邊的編號,

對應代碼1:
不標記反向邊的話,要換一種思路:
只需要對有向邊上的點做拓撲排序,
記錄每個點的出場順序(拓撲序),
讓拓撲序小的指向大的就ok。

*/
代碼一:

#include <bits/stdc++.h> using namespace std; const int N = 1e5+5; struct Edge {int to;int val;int nxt; } edge[N<<1]; int head[N],idx,top[N]; int in[N]; int n,m1,m2; void add_edge(int from,int to,int val) {edge[idx].to = to;edge[idx].val = val;edge[idx].nxt = head[from];head[from] = idx++;in[to]++; }void topSort() {queue<int>q;int tp = 0;for(int i = 1; i <= n ; i++){if(!in[i]){q.push(i);top[i] = ++tp;}}while(!q.empty()){int k = q.front();q.pop();for(int i = head[k]; ~i; i = edge[i].nxt){int t = edge[i].to;if(!(--in[t])){q.push(t);top[t] = ++tp;}}} } int main() {cin>>n>>m1>>m2;int x,y;memset(head,-1,sizeof(head));for(int i = 0; i < m1; i++){cin>>x>>y;add_edge(x,y,1);}topSort();for(int i = 0; i < m2; i++){cin>>x>>y;if(top[x] < top[y])cout<<x<<" "<<y<<endl;elsecout<<y<<" "<<x<<endl;}return 0; }

代碼二:

//標記 無向邊中已經確定方向的 反向邊

#include <bits/stdc++.h> using namespace std; const int N = 1e5+5; int n,m1,m2,idx; struct Edge {int from;int to;int val;int nxt; } edge[N<<1]; int head[N],in[N]; //head[i]存i擴展出來的邊中最大的那個編號(即最后一條擴展邊的編號) inline void add_edge(int from,int to,int val) {edge[idx].from = from;edge[idx].to = to;edge[idx].val = val;edge[idx].nxt = head[from];head[from] = idx++; } void topSort() {queue<int>q;for(int i = 1; i <= n; i++){if(!(in[i])) q.push(i);}while(!q.empty()){int k = q.front();q.pop();for(int i = head[k]; ~i; i = edge[i].nxt){if(edge[i].val == 1){int t = edge[i].to;if(!(--in[t])) q.push(t);}else if (edge[i].val == 2){//cout<<edge[i].from<<" "<<edge[i].to<<endl;edge[i^1].val = -1;}}} } int main() {memset(head,-1,sizeof(head));cin>>n>>m1>>m2;int x,y;for(int i = 0; i < m1; i++){cin>>x>>y;add_edge(x,y,1);in[y]++;}/*邊標號從0開始,如果前面已經標號邊數為奇數,下一個idx也是奇數!!為了后面找后面邊的反邊,此時前面標號邊數一定要構成一個偶數,即讓下一條邊標號為偶數。前面邊數不夠,只能虛擬一條邊。*/if(idx&1) idx++;for(int i = 0; i < m2; i++){cin>>x>>y;add_edge(x,y,2);add_edge(y,x,2);}topSort();for(int i = 0; i < idx; i++){if(edge[i].val == 2)cout<<edge[i].from<<" "<<edge[i].to<<endl;}return 0; }

總結

以上是生活随笔為你收集整理的Dizzy Cows(拓扑)的全部內容,希望文章能夠幫你解決所遇到的問題。

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