1176. 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 ( A ≤ N) 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 20 0 1 00 0 1 01 1 0 10 0 1 0 | 2 44 11 22 11 44 2 |
Problem Author: Pavel Atnashev Problem Source: Third USU personal programming contest, Ekaterinburg, Russia, February 16, 2002
***************************************************************************************
深搜求欧拉回路并记录路径(注:vector void reverse(),根据实际重新计算vector容量,好用得加<algorithm>预处理!!!!!!!!!!!!!!!!!!!!)
***************************************************************************************
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namespace std;10 vector adj[10001];11 vector path;12 int n,m,i,j,k;13 void dfs(int s)//深搜求欧拉回路14 {15 while(adj[s].size())16 {17 int p=adj[s].back();18 adj[s].pop_back();19 dfs(p);20 21 }22 path.push_back(s);//压入路径23 }24 int main()25 {26 cin>>n>>m;27 for(i=1;i<=n;i++)28 for(j=1;j<=n;j++)29 {30 cin>>k;31 if(i!=j&&k==0)32 adj[i].push_back(j);33 }34 dfs(m);35 reverse(path.begin(),path.end());36 for(i=0;i