Dizzy Cows(拓扑)
鏈接:https://ac.nowcoder.com/acm/contest/1077/D
來源:牛客網(wǎng)
時間限制: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:
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
輸入
復制
輸出
復制
/*
題意是:給無向邊標定方向,使圖不構成環(huán)。
不構成環(huán),很容易想到拓撲排序
對應代碼2:
一開始我是加入了所以邊進行拓撲排序(不算無向邊的度),
當隊列中出來的這個點有無向邊與它相連時,輸出:當前點–>通過無向邊與之相連的點,
但是我不知道如何 標記 那已經(jīng)輸出的那條無向邊(即方向已經(jīng)確定的邊)已經(jīng)確定方向了,
,不標記的化會再輸出它的反向邊。
(后來傻不拉幾的才反映過來實質(zhì)是一個反向邊標記問題:
快速找到該條邊的反向邊標號,并標記不可用)。
如何快速找到一條邊的反向邊?(每條邊已經(jīng)標號并且方向邊標號相鄰):
nowID ^1 為反向邊的編號,
對應代碼1:
不標記反向邊的話,要換一種思路:
只需要對有向邊上的點做拓撲排序,
記錄每個點的出場順序(拓撲序),
讓拓撲序小的指向大的就ok。
*/
代碼一:
代碼二:
//標記 無向邊中已經(jīng)確定方向的 反向邊
#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開始,如果前面已經(jīng)標號邊數(shù)為奇數(shù),下一個idx也是奇數(shù)!!為了后面找后面邊的反邊,此時前面標號邊數(shù)一定要構成一個偶數(shù),即讓下一條邊標號為偶數(shù)。前面邊數(shù)不夠,只能虛擬一條邊。*/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(拓扑)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 产生数(floyd+高精度计算)
- 下一篇: Music Notes(前缀和+二分)