URAL 1176 Hyperchannels(欧拉回路路径)

时间:2023-03-08 17:55:49

Hyperchannels

Time limit: 1.0 second
Memory limit: 64 MB
The
Galaxy Empire consists of N planets. Hyperchannels exist between most
of the planets. New Emperor urged to extend hyperchannels network in
such a way, that he can move from any planet to any other using no more
than one channel. One can pass through the channel only in one
direction.
The
last channel-establishing ship is located on the base near planet A.
This ship can’t pass through the existing channel, it always establishes
a new one. But presence of two channels connecting the same planets in
one direction makes navigation too difficult, almost impossible. The
problem is to find a route for this ship to establish all necessary
channels with no excessive ones. In the end of this route ship should
return to the base.

Input

First line contains integer N ≤ 1000 and number of the planet A (AN) where the base is situated.
Each of the following N lines contain N numbers, the j-th number of the i-th line equals to 1 if there exists channel from planet i to planet j, and equals to 0 otherwise.
It is known, that Empire can fulfill its need of hyperchannels by establishing no more than 32000 new ones.

Output

Output
should contain the sequence in which channels should be established.
Each line should contain two integers — numbers of source and
destination planet of channel. You may assume, that solution always
exists.

Sample

input output
4 2
0 0 1 0
0 0 1 0
1 1 0 1
0 0 1 0
2 4
4 1
1 2
2 1
1 4
4 2
Problem Author: Pavel Atnashev
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int n,m,cnt=;
int tot=,s,t;
int head[N],dis[N],vis[N][N],pre[N];
int in[N],out[N];
stack<int>st;
struct man {
int to,next;
} edg[N*N];
void add(int u,int v) {
edg[tot].to=v;
edg[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u){
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(!vis[u][v]){
vis[u][v]=;
dfs(v);
}
}
st.push(u);
}
int main() {
int u,v,nn=,sum=;
met(head,-);
scanf("%d%d",&n,&s);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&u);
if(!u&&i!=j)add(i,j);
}
}
dfs(s);
u=st.top();st.pop();
while(!st.empty()){
v=st.top();
st.pop();
printf("%d %d\n",u,v);
u=v;
}
return ;
}