URAL 1176 Hyperchannels(欧拉回路路径)

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

inputoutput
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 = 1005;
const int M = 24005;
int n,m,cnt=0;
int tot=0,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!=-1;i=edg[i].next){
        int v=edg[i].to;
        if(!vis[u][v]){
            vis[u][v]=1;
            dfs(v);
        }
    }
    st.push(u);
}
int main() {
    int u,v,nn=0,sum=0;
    met(head,-1);
    scanf("%d%d",&n,&s);
    for(int i=1;i<=n;i++){
        for(int j=1;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 0;
}

 

转载于:https://www.cnblogs.com/jianrenfang/p/6007028.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值