Submission #7276245


Source Code Expand

Copy
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int N;
int A[1050][1050];

int used[1050];

int child[1050];

int p[1050];

int sign = 0;

queue<int> Q[1050];

void UPDATE(int Z) {
	if (Q[Z].size() == 0) {
		child[Z] = 1030;
	}
	else {
		int next = Q[Z].front();
		Q[Z].pop();
		child[Z] = next;
	}
}

int main(void) {

	cin >> N;

	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N - 1; j++) {
			cin >> A[i][j];
			Q[i].push(A[i][j]);
		}
	}

	for (int i = 1; i <= N; i++) {
		int y = Q[i].front();
		Q[i].pop();
		child[i] = y;
	}

	int ans = 0;

	int cnt = 0;

	while (1) {

		for (int i = 1; i <= N; i++) { used[i] = 0; }

		int sign = 0;
		for (int i = 1; i <= N; i++) {
			if (child[child[i]] == i && used[i]==0 && used[child[i]]==0) { 
				cnt++;
				int Y = child[i];
				UPDATE(Y);
				UPDATE(i);
				used[Y] = 1;
				used[i] = 1;
				sign = 1;
				}
		}

		if (sign == 0) { ans = -1; break; }

		ans++;

		if (cnt == (N*(N - 1)) / 2) { break; }

		

	}


	cout << ans << endl;

	return 0;
}

Submission Info

Submission Time
Task E - League
User LEE_0102
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1158 Byte
Status AC
Exec Time 896 ms
Memory 8832 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 23
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23
Case Name Status Exec Time Memory
a01 AC 2 ms 1024 KB
a02 AC 2 ms 1024 KB
a03 AC 2 ms 1024 KB
b04 AC 2 ms 1024 KB
b05 AC 2 ms 1024 KB
b06 AC 2 ms 1024 KB
b07 AC 2 ms 1024 KB
b08 AC 896 ms 8832 KB
b09 AC 213 ms 8832 KB
b10 AC 228 ms 8832 KB
b11 AC 227 ms 8832 KB
b12 AC 65 ms 6016 KB
b13 AC 56 ms 5760 KB
b14 AC 28 ms 2688 KB
b15 AC 21 ms 2432 KB
b16 AC 13 ms 2048 KB
b17 AC 9 ms 1792 KB
b18 AC 47 ms 3456 KB
b19 AC 38 ms 3200 KB
b20 AC 14 ms 2048 KB
b21 AC 10 ms 1792 KB
b22 AC 5 ms 1536 KB
b23 AC 3 ms 1280 KB