Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- #include <stack>
- #include <fstream>
- #include <unordered_map>
- #include <string>
- #include <algorithm>
- #include <sstream>
- #include <cmath>
- #include <queue>
- #include <time.h>
- #include <stdio.h>
- #include <list>
- #include <stdlib.h>
- #include <string.h>
- #include <list>
- #include <functional>
- #include <random>
- using namespace std;
- //#define JUDJE 1488
- #ifndef JUDJE
- ifstream in("input.in");
- ofstream out("output.out");
- #define cin in
- #define cout out
- #endif
- #define ll long long
- #define MOD 1000000007
- #define mp(a, b) make_pair(a, b)
- #define PI 3.1415926535
- #define EPS 0.00000001
- #define pii pair<int, int>
- #define INF 1000000000
- bool matr[2000][2000];
- vector<vector<int> > g;
- bool used[2000];
- vector<int> path;
- int n, nn, x;
- set<pii> notUsedEdges;
- bool toCircle()
- {
- if( matr[ path[0] ] [ path.back() ] )
- {
- path.push_back(path[0]);
- return 0;
- }
- int u = path.front();
- int v = path.back();
- if(u == v)
- return 0;
- for(int i = 1; i < path.size()-1; ++i)
- {
- int curVa = path[i];
- int predVa = path[i-1];
- vector<int> vvv;
- if(matr[u][curVa] && matr[v][predVa])
- {
- vvv.push_back(u);
- vvv.push_back(curVa);
- for(int j = i+1; j < path.size(); ++j)
- vvv.push_back( path[j] );
- vvv.push_back(predVa);
- for(int j = i-2; j >= 0; --j)
- vvv.push_back(path[j]);
- swap(vvv, path);
- return 0;
- }
- }
- for(int i = 1; i <= n; ++i)
- {
- if(!used[i])
- {
- if(matr[u][i] && matr[v][i])
- {
- for(int ii = 1; ii <= n; ++ii)
- if(used[ii] && matr[ii][i])
- notUsedEdges.erase(mp(ii, i));
- for(int ii = 1; ii <= n; ++ii)
- if(!used[ii] && matr[ii][i])
- notUsedEdges.insert(mp(i, ii));
- vector<int> vvv;
- used[i] = 1;
- vvv.push_back(i);
- for(int ii = 0; ii < path.size(); ++ii)
- vvv.push_back(path[ii]);
- vvv.push_back(i);
- swap(vvv, path);
- return 1;
- }
- }
- }
- return 1;
- }
- void addVertexToCircle()
- {
- pii a = *notUsedEdges.begin();
- for(int i = 1; i <= n; ++i)
- if(matr[i][a.second] && !used[i])
- notUsedEdges.insert( mp(a.second, i));
- for(int i = 1; i <= n; ++i)
- if(matr[a.second][i] && used[i])
- notUsedEdges.erase(mp(i, a.second));
- int j = 0;
- while(path[j] != a.first)
- ++j;
- ++j;
- vector<int> vvv;
- for(j; j < path.size() - 1; ++j)
- vvv.push_back(path[j]);
- for(int i = 0; path[i] != a.first; ++i)
- vvv.push_back(path[i]);
- vvv.push_back(a.first);
- vvv.push_back(a.second);
- used[a.second] = 1;
- swap(vvv, path);
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin >> n >> nn >> x;
- g.resize(n+1);
- for(int i = 1; i <= n; ++i)
- {
- for(int j = 0; j < nn; ++j)
- {
- int a;
- cin >> a;
- g[i].push_back(a);
- matr[i][a] = 1;
- }
- }
- bool isFind = 1;
- int curV = x;
- used[x] = 1;
- path.push_back(x);
- while(isFind)
- {
- int i = 0;
- for(; i < nn; ++i)
- {
- if(!used[g[curV][i]])
- {
- used[g[curV][i]] = 1;
- path.push_back( g[curV][i] );
- curV = g[curV][i];
- isFind = 1;
- break;
- }
- }
- if(i == nn)
- isFind = 0;
- }
- for(int i = 0; i < path.size(); ++i)
- for(int j = 1; j <= n; ++j)
- if(matr[path[i]][j] && !used[j])
- notUsedEdges.insert(mp(path[i], j));
- for(int i = path.size(); i < n; ++i)
- if(!toCircle())
- addVertexToCircle();
- if(path.size() == n)
- toCircle();
- vector<int> ans;
- int jj = 0;
- if(path[0] == x)
- {
- for(int i = 0; i < n; ++i)
- cout << path[i] << " ";
- cout << path[0];
- return 0;
- }
- while(path[jj] != x)
- ++jj;
- for(; jj < path.size(); ++jj)
- ans.push_back(path[jj]);
- for(int i = 1; path[i] != x; ++i)
- ans.push_back(path[i]);
- swap(ans,path);
- for(int i = 0; i < n; ++i)
- cout << path[i] << " ";
- cout << path[0];
- }
- /*
- 8 4 1
- 5 6 7 8
- 4 6 7 8
- 4 5 7 8
- 2 3 5 8
- 1 3 4 6
- 1 2 5 7
- 1 2 3 6
- 1 2 3 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement