Pagini recente » Cod sursa (job #2334340) | Cod sursa (job #1176205) | Cod sursa (job #2485311) | Cod sursa (job #2821443) | Cod sursa (job #1258202)
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
ifstream F("plimbare.in");
ofstream G("plimbare.out");
const int N = 110;
int n,m,a[N][N];
vector<int> v[N],w[N];
vector<int> stack,ctc,now;
int mk[N];
void df_graph(int x)
{
mk[x] = 1;
for (size_t i=0;i<v[x].size();++i)
{
int y = v[x][i];
if ( mk[y] == 0 )
df_graph(y);
}
stack.push_back( x );
}
void df_trans(int x)
{
mk[x] = 1;
for (size_t i=0;i<w[x].size();++i)
{
int y = w[x][i];
if ( mk[y] == 0 )
df_trans(y);
}
now.push_back( x );
}
void find_ctc()
{
for (int i=1;i<=n;++i)
if ( mk[i] == 0 )
df_graph(i);
memset(mk,0,sizeof(mk));
for (int i=n-1;i>0;--i)
{
now = vector<int>();
int x = stack[i];
if ( mk[i] == 0 )
df_trans(x);
if ( now.size() > ctc.size() )
ctc = now;
}
}
vector<int> cycle,grupe[2];
vector< pair<int,int> > check;
bool good[N];
void find_cycle(int x)
{
mk[x] = 1;
cycle.push_back(x);
for (int i=0;i<n;++i)
{
if ( a[x][ctc[i]] == 1 && mk[ctc[i]] == 0 )
{
find_cycle( ctc[i] );
break;
}
if ( a[x][ctc[i]] == 1 && mk[ctc[i]] == 1 )
{
int now = ctc[i];
cycle.push_back(now);
reverse(cycle.begin(),cycle.end());
while ( cycle.back() != now )
cycle.pop_back();
reverse(cycle.begin(),cycle.end());
break;
}
}
}
void update_cycle(int x)
{
mk[x] = 1;
vector<int> new_cycle = vector<int>();
bool ok = 0;
for (int i=0;i<int(cycle.size())-1;++i)
{
new_cycle.push_back(cycle[i]);
if ( a[cycle[i]][x] == 1 && a[x][cycle[i+1]] == 1 && !ok )
{
new_cycle.push_back(x);
ok = 1;
}
}
new_cycle.push_back(cycle[0]);
cycle = new_cycle;
}
void update_good(int x,int y)
{
for (int g=0;g<2;++g)
for (int i=0;i<int(grupe[g].size());++i)
if ( a[x][grupe[g][i]] == 1 && a[grupe[g][i]][y] == 1 )
good[grupe[g][i]] = 1;
}
void update_cycle(int x,int y)
{
mk[x] = mk[y] = 1;
cycle.push_back(cycle[0]);
vector<int> new_cycle = vector<int>();
bool ok = 0;
for (int i=0;i<int(cycle.size())-1;++i)
{
new_cycle.push_back(cycle[i]);
if ( a[cycle[i]][x] == 1 && a[y][cycle[i+1]] == 1 && !ok )
{
new_cycle.push_back(x);
new_cycle.push_back(y);
update_good(cycle[i],x);
update_good(x,y);
update_good(y,cycle[i+1]);
ok = 1;
}
}
cycle = new_cycle;
}
void build_grupes()
{
for (int i=0;i<n;++i)
{
int x = ctc[i];
if ( mk[x] ) continue;
bool ok = 0;
for (int j=0;j<int(cycle.size())-1;++j)
if ( a[cycle[j]][x] && a[x][cycle[j+1]] )
{
ok = 1;
update_cycle(x);
break;
}
if ( !ok )
grupe[ a[x][cycle[0]] ].push_back(x);
}
}
void solve_grupes()
{
for (int i=0;i<int(grupe[0].size());++i)
for (int j=0;j<int(grupe[1].size());++j)
if ( a[grupe[0][i]][grupe[1][j]] == 1 )
check.push_back( make_pair(grupe[0][i],grupe[1][j]) );
int places = cycle.size();
--places;
for (int i=0;i<int(check.size());++i)
{
int x = check[i].first;
int y = check[i].second;
if ( mk[x] == 0 && mk[y] == 0 && places )
{
update_cycle(x,y);
--places;
}
}
check = vector<pair<int,int> >();
}
int main()
{
F>>n;
m = n*(n-1)/2;
for (int i=1,x,y;i<=m;++i)
{
F>>x>>y;
v[x].push_back(y);
w[y].push_back(x);
a[x][y] = 1;
}
find_ctc();
n = ctc.size();
memset(mk,0,sizeof(mk));
sort(ctc.begin(),ctc.end());
find_cycle(ctc[0]);
build_grupes();
solve_grupes();
for (int i=0;i<ctc.size();++i)
if ( mk[ctc[i]] == 0 )
update_cycle(ctc[i]);
cycle.pop_back();
G<<cycle.size()<<'\n';
for (int i=0;i<int(cycle.size());++i)
G<<cycle[i]<<' ';
G<<'\n';
}