結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー sugim48sugim48
提出日時 2015-08-01 00:09:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,166 ms / 5,000 ms
コード長 2,645 bytes
コンパイル時間 939 ms
コンパイル使用メモリ 104,496 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-24 23:53:54
合計ジャッジ時間 8,274 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 10 ms
4,376 KB
testcase_15 AC 7 ms
4,376 KB
testcase_16 AC 11 ms
4,380 KB
testcase_17 AC 15 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 5 ms
4,380 KB
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 27 ms
4,380 KB
testcase_28 AC 22 ms
4,380 KB
testcase_29 AC 8 ms
4,376 KB
testcase_30 AC 175 ms
4,376 KB
testcase_31 AC 99 ms
4,380 KB
testcase_32 AC 66 ms
4,380 KB
testcase_33 AC 98 ms
4,376 KB
testcase_34 AC 61 ms
4,376 KB
testcase_35 AC 80 ms
4,376 KB
testcase_36 AC 622 ms
4,380 KB
testcase_37 AC 1,402 ms
4,380 KB
testcase_38 AC 2,166 ms
4,380 KB
testcase_39 AC 874 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:118:24: warning: ‘maxy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   for (int n = maxy; n <= 223092800 * 2; n += maxi) {
                      ~~^~~~~~~~~~~~~~~~

ソースコード

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;
		}
		map<int, int> mp;
		for (int k = 0; k < x.size(); k++) {
			if (mp.count(x[k]) && mp[x[k]] != y[k])
				ok = false;
			mp[x[k]] = y[k];
		}
		if (!ok) {
			cout << -1 << endl;
			continue;
		}
		x.clear(); y.clear();
		for (auto it = mp.begin(); it != mp.end(); it++) {
			x.push_back(it->first);
			y.push_back(it->second);
		}
		bool hoge = false;
		/*for (int n = 1; n <= 2230928 + 1; n++) {
			bool ok = true;
			for (int k = 0; k < x.size(); k++)
				if (n % x[k] != y[k])
					ok = false;
			if (ok) {
				cout << n << endl;
				hoge = true;
				break;
			}
		}*/
		if (hoge) continue;
		int maxi = 0, maxy;
		for (int k = 0; k < x.size(); k++)
			if (x[k] > maxi) {
				maxi = x[k];
				maxy = y[k];
			}
		for (int n = maxy; n <= 223092800 * 2; n += maxi) {
			if (!n) continue;
			bool ok = true;
			for (int k = 0; k < x.size(); k++)
				if (n % x[k] != y[k])
					ok = false;
			if (ok) {
				cout << n << endl;
				hoge = true;
				break;
			}
		}
		if (!hoge) cout << -1 << endl;
	}
}
0