結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー sugim48sugim48
提出日時 2015-07-31 23:51:55
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,853 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 94,864 KB
実行使用メモリ 7,532 KB
最終ジャッジ日時 2023-09-24 23:44:55
合計ジャッジ時間 7,221 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;

int main() {
	int N, K; cin >> N >> K;
	vector<int> X(K), Y(K);
	for (int k = 0; k < K; k++) {
		cin >> X[k] >> Y[k];
		X[k]--; Y[k]--;
	}
	vector<int> a(N);
	for (int i = 0; i < N; i++)
		a[i] = i;
	for (int k = K - 1; k >= 0; k--)
		swap(a[X[k]], a[Y[k]]);
	int Q; cin >> Q;
	while (Q--) {
		vector<int> A(N);
		for (int i = 0; i < N; i++) {
			cin >> A[i];
			A[i]--;
		}
		vector<bool> vis(N);
		vector<int> x, y;
		bool ok = true;
		for (int i = 0; i < N; i++) {
			if (vis[i]) continue;
			vis[i] = true;
			int z = 1;
			for (int _i = a[i]; _i != i; _i = a[_i]) {
				z++;
				vis[_i] = true;
			}
			x.push_back(z);
			z = 0;
			for (int _i = i; A[_i] != i; _i = a[_i]) z++;
			y.push_back(z);
			for (int _i = a[i]; _i != i; _i = a[_i]) {
				int j = _i;
				for (int t = 0; t < z; t++)
					j = a[j];
				if (A[j] != _i) ok = false;
			}
		}
		if (!ok) {
			cout << -1 << endl;
			continue;
		}
		for (int n = 1; n <= 22309287 + 1; n++) {
			if (n == 22309287 + 1) {
				cout << -1 << endl;
				break;
			}
			bool ok = true;
			for (int k = 0; k < x.size(); k++)
				if (n % x[k] != y[k])
					ok = false;
			if (ok) {
				cout << n << endl;
				break;
			}
		}
	}
}
0