結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー sugim48sugim48
提出日時 2015-07-31 23:56:04
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,920 bytes
コンパイル時間 1,007 ms
コンパイル使用メモリ 96,152 KB
実行使用メモリ 7,812 KB
最終ジャッジ日時 2023-09-24 23:45:59
合計ジャッジ時間 12,645 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 516 ms
4,376 KB
testcase_15 AC 302 ms
4,380 KB
testcase_16 AC 290 ms
4,376 KB
testcase_17 AC 642 ms
4,376 KB
testcase_18 AC 34 ms
4,380 KB
testcase_19 AC 32 ms
4,376 KB
testcase_20 AC 51 ms
4,380 KB
testcase_21 AC 98 ms
4,380 KB
testcase_22 AC 44 ms
4,380 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 104 ms
4,376 KB
testcase_26 AC 62 ms
4,376 KB
testcase_27 AC 998 ms
4,380 KB
testcase_28 AC 933 ms
4,376 KB
testcase_29 AC 349 ms
4,376 KB
testcase_30 TLE -
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 && z <= 100; _i = a[_i]) z++;
			if (z > 100) {
				ok = false;
				continue;
			}
			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 <= 223092870 + 1; n++) {
			if (n == 223092870 + 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