Submission #538695


Source Code Expand

#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <cmath>
#include <cassert>
#include <cstdio>

using namespace std; using namespace placeholders;

using LL = long long;
using ULL = unsigned long long;
using VI = vector< int >;
using VVI = vector< vector< int > >;
using VS = vector< string >;
using SS = stringstream;
using PII = pair< int, int >;
using VPII = vector< pair< int, int > >;
template < typename T = int > using VT = vector< T >;
template < typename T = int > using VVT = vector< vector< T > >;
template < typename T = int > using LIM = numeric_limits< T >;

template < typename T > inline istream& operator>>( istream &s, vector< T > &v ){ for ( T &t : v ) { s >> t; } return s; }
template < typename T > inline ostream& operator<<( ostream &s, const vector< T > &v ){ for ( int i = 0; i < int( v.size() ); ++i ){ s << ( " " + !i ) << v[i]; } return s; }
template < typename T > inline T fromString( const string &s ) { T res; istringstream iss( s ); iss >> res; return res; };
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };

#define REP2( i, n ) REP3( i, 0, n )
#define REP3( i, m, n ) for ( int i = ( int )( m ); i < ( int )( n ); ++i )
#define GET_REP( a, b, c, F, ... ) F
#define REP( ... ) GET_REP( __VA_ARGS__, REP3, REP2 )( __VA_ARGS__ )
#define FOR( e, c ) for ( auto &e : c )
#define ALL( c ) begin( c ), end( c )
#define AALL( a, t ) ( t* )a, ( t* )a + sizeof( a ) / sizeof( t )
#define DRANGE( c, p ) ( c ).begin(), ( c ).begin() + ( p ), ( c ).end()

#define SZ( v ) ( (int)( v ).size() )
#define PB push_back
#define EM emplace
#define EB emplace_back
#define BI back_inserter

#define EXIST( c, e ) ( ( c ).find( e ) != ( c ).end() )

#define MP make_pair
#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

void dfs( const VI &P, VI &depth, VI &sizes, const int u, const int d = 0 )
{
	if ( sizes[u] != -1 )
	{
		return;
	}

	if ( depth[u] != -1 )
	{
		sizes[u] = d - depth[u];
	}

	depth[u] = d;

	dfs( P, depth, sizes, P[u], d + 1 );

	return;
}

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	int N, A;
	cin >> N >> A;
	--A;

	string K;
	cin >> K;

	VI P( N );
	cin >> P;
	transform( ALL( P ), begin( P ), bind( minus< int >(), _1, 1 ) );

	if ( SZ( K ) <= 6 )
	{
		for ( int k = fromString< int >( K ); k; --k )
		{
			A = P[A];
		}

		cout << A + 1 << endl;
		return 0;
	}

	VI depth( N, -1 ), sizes( N, -1 );
	dfs( P, depth, sizes, A );

	for ( ; sizes[A] == -1; A = P[A] );
	const int dist = depth[A];

	int m = 0;
	FOR( c, K )
	{
		m *= 10;
		m += c - '0';
		m %= sizes[A];
	}

	m = ( ( m - dist ) % sizes[A] + sizes[A] ) % sizes[A];

	for ( ; m; --m )
	{
		A = P[A];
	}

	cout << A + 1 << endl;

	return 0;
}

Submission Info

Submission Time
Task D - へんてこ辞書
User torus711
Language C++11 (GCC 4.9.2)
Score 100
Code Size 3209 Byte
Status AC
Exec Time 44 ms
Memory 2172 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 50 / 50 50 / 50
Status
AC × 2
AC × 12
AC × 25
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_03.txt
Subtask1 subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt, subtask0_4.txt, subtask0_5.txt, subtask0_6.txt, subtask0_7.txt, subtask0_8.txt, subtask0_9.txt, subtask0_sample_01.txt, subtask0_sample_03.txt
All subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt, subtask0_4.txt, subtask0_5.txt, subtask0_6.txt, subtask0_7.txt, subtask0_8.txt, subtask0_9.txt, subtask0_sample_01.txt, subtask0_sample_03.txt, subtask1_0.txt, subtask1_1.txt, subtask1_10.txt, subtask1_11.txt, subtask1_2.txt, subtask1_3.txt, subtask1_4.txt, subtask1_5.txt, subtask1_6.txt, subtask1_7.txt, subtask1_8.txt, subtask1_9.txt, subtask1_sample_02.txt
Case Name Status Exec Time Memory
subtask0_0.txt AC 40 ms 1956 KB
subtask0_1.txt AC 35 ms 1696 KB
subtask0_2.txt AC 37 ms 1760 KB
subtask0_3.txt AC 36 ms 1568 KB
subtask0_4.txt AC 41 ms 1940 KB
subtask0_5.txt AC 37 ms 1708 KB
subtask0_6.txt AC 37 ms 1564 KB
subtask0_7.txt AC 37 ms 1856 KB
subtask0_8.txt AC 34 ms 1496 KB
subtask0_9.txt AC 39 ms 1824 KB
subtask0_sample_01.txt AC 27 ms 804 KB
subtask0_sample_03.txt AC 26 ms 928 KB
subtask1_0.txt AC 38 ms 1692 KB
subtask1_1.txt AC 40 ms 1824 KB
subtask1_10.txt AC 44 ms 2172 KB
subtask1_11.txt AC 27 ms 800 KB
subtask1_2.txt AC 37 ms 1704 KB
subtask1_3.txt AC 34 ms 1560 KB
subtask1_4.txt AC 35 ms 1628 KB
subtask1_5.txt AC 38 ms 1960 KB
subtask1_6.txt AC 37 ms 1832 KB
subtask1_7.txt AC 34 ms 1576 KB
subtask1_8.txt AC 41 ms 2132 KB
subtask1_9.txt AC 43 ms 2012 KB
subtask1_sample_02.txt AC 27 ms 916 KB