Dizzy Cows(拓扑)
鏈接:https://ac.nowcoder.com/acm/contest/1077/D
來(lái)源:牛客網(wǎng)
時(shí)間限制:C/C++ 1秒,其他語(yǔ)言2秒
空間限制:C/C++ 32768K,其他語(yǔ)言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
輸入
復(fù)制
輸出
復(fù)制
/*
題意是:給無(wú)向邊標(biāo)定方向,使圖不構(gòu)成環(huán)。
不構(gòu)成環(huán),很容易想到拓?fù)渑判?/strong>
對(duì)應(yīng)代碼2:
一開(kāi)始我是加入了所以邊進(jìn)行拓?fù)渑判?#xff08;不算無(wú)向邊的度),
當(dāng)隊(duì)列中出來(lái)的這個(gè)點(diǎn)有無(wú)向邊與它相連時(shí),輸出:當(dāng)前點(diǎn)–>通過(guò)無(wú)向邊與之相連的點(diǎn),
但是我不知道如何 標(biāo)記 那已經(jīng)輸出的那條無(wú)向邊(即方向已經(jīng)確定的邊)已經(jīng)確定方向了,
,不標(biāo)記的化會(huì)再輸出它的反向邊。
(后來(lái)傻不拉幾的才反映過(guò)來(lái)實(shí)質(zhì)是一個(gè)反向邊標(biāo)記問(wèn)題:
快速找到該條邊的反向邊標(biāo)號(hào),并標(biāo)記不可用)。
如何快速找到一條邊的反向邊?(每條邊已經(jīng)標(biāo)號(hào)并且方向邊標(biāo)號(hào)相鄰):
nowID ^1 為反向邊的編號(hào),
對(duì)應(yīng)代碼1:
不標(biāo)記反向邊的話,要換一種思路:
只需要對(duì)有向邊上的點(diǎn)做拓?fù)渑判?#xff0c;
記錄每個(gè)點(diǎn)的出場(chǎng)順序(拓?fù)湫?#xff09;,
讓拓?fù)湫蛐〉闹赶虼蟮木蚾k。
*/
代碼一:
代碼二:
//標(biāo)記 無(wú)向邊中已經(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擴(kuò)展出來(lái)的邊中最大的那個(gè)編號(hào)(即最后一條擴(kuò)展邊的編號(hào)) 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]++;}/*邊標(biāo)號(hào)從0開(kāi)始,如果前面已經(jīng)標(biāo)號(hào)邊數(shù)為奇數(shù),下一個(gè)idx也是奇數(shù)!!為了后面找后面邊的反邊,此時(shí)前面標(biāo)號(hào)邊數(shù)一定要構(gòu)成一個(gè)偶數(shù),即讓下一條邊標(biāo)號(hào)為偶數(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; }總結(jié)
以上是生活随笔為你收集整理的Dizzy Cows(拓扑)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 产生数(floyd+高精度计算)
- 下一篇: Music Notes(前缀和+二分)