結果

問題 No.1078 I love Matrix Construction
ユーザー 👑 jupirojupiro
提出日時 2020-06-13 02:59:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,632 bytes
コンパイル時間 1,580 ms
コンパイル使用メモリ 140,948 KB
実行使用メモリ 88,724 KB
最終ジャッジ日時 2023-09-06 11:42:29
合計ジャッジ時間 7,339 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 32 ms
15,600 KB
testcase_03 AC 94 ms
35,424 KB
testcase_04 AC 140 ms
48,568 KB
testcase_05 WA -
testcase_06 AC 30 ms
15,124 KB
testcase_07 AC 12 ms
7,736 KB
testcase_08 AC 109 ms
40,660 KB
testcase_09 WA -
testcase_10 AC 315 ms
88,724 KB
testcase_11 AC 147 ms
50,844 KB
testcase_12 AC 244 ms
73,696 KB
testcase_13 AC 291 ms
82,324 KB
testcase_14 AC 174 ms
57,996 KB
testcase_15 AC 268 ms
78,756 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 22 ms
12,244 KB
testcase_19 AC 53 ms
23,820 KB
testcase_20 AC 56 ms
23,620 KB
testcase_21 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;


template< typename G >
struct StronglyConnectedComponents {
	const G& g;
	vector<vector<int>> gg, rg;
	vector< int > comp, order, used;

	StronglyConnectedComponents(G& g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
		for (int i = 0; i < g.size(); i++) {
			for (auto e : g[i]) {
				gg[i].emplace_back((int)e);
				rg[(int)e].emplace_back(i);
			}
		}
	}

	int operator[](int k) {
		return comp[k];
	}

	void dfs(int idx) {
		if (used[idx]) return;
		used[idx] = true;
		for (int to : gg[idx]) dfs(to);
		order.push_back(idx);
	}

	void rdfs(int idx, int cnt) {
		if (comp[idx] != -1) return;
		comp[idx] = cnt;
		for (int to : rg[idx]) rdfs(to, cnt);
	}

	void build(vector<vector<int>>& t) {
		for (int i = 0; i < gg.size(); i++) dfs(i);
		reverse(begin(order), end(order));
		int ptr = 0;
		for (int i : order) if (comp[i] == -1) rdfs(i, ptr), ptr++;

		t.resize(ptr);
		for (int i = 0; i < g.size(); i++) {
			for (auto& to : g[i]) {
				int x = comp[i], y = comp[to];
				if (x == y) continue;
				t[x].push_back(y);
			}
		}
	}
};
int n;
int cnv(int i, int j, int k) {
	int res = i * n + j;
	if (k) res += n * n;
	return res;
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	scanf("%d", &n);
	vector<vector<int>> g(n * n * 2);
	vector<int> s(n); for (int i = 0; i < n; i++) scanf("%d", &s[i]), s[i]--;
	vector<int> t(n); for (int i = 0; i < n; i++) scanf("%d", &t[i]), t[i]--;
	vector<int> u(n); for (int i = 0; i < n; i++) scanf("%d", &u[i]);

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (u[i] == 0) {
				g[cnv(s[i], j, 0)].emplace_back(cnv(j, t[i], 1));
				g[cnv(j, t[i], 0)].emplace_back(cnv(s[i], j, 1));
			}
			if (u[i] == 1) {
				g[cnv(s[i], j, 1)].emplace_back(cnv(j, t[i], 1));
				g[cnv(j, t[i], 0)].emplace_back(cnv(s[i], j, 0));
			}
			if (u[i] == 2) {
				g[cnv(s[i], j, 0)].emplace_back(cnv(j, t[i], 0));
				g[cnv(j, t[i], 1)].emplace_back(cnv(s[i], j, 1));
			}
			if (u[i] == 3) {
				g[cnv(s[i], j, 1)].emplace_back(cnv(j, t[i], 0));
				g[cnv(j, t[i], 1)].emplace_back(cnv(s[i], j, 0));
			}
		}
	}
	StronglyConnectedComponents<vector<vector<int>>> scc(g);
	vector<vector<int>> tmp; scc.build(tmp);
	for (int i = 0; i < n * n; i++) if (scc[i] == scc[i + n * n]) { puts("-1"); return 0; };
	vector<vector<int>> a(n, vector<int>(n));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int s = i * n + j;
			int t = s + n * n;
			if (scc[s] < scc[t]) {
				a[i][j] = 0;
			}
			else {
				a[i][j] = 1;
			}
		}
	}

	for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) printf("%d%c", a[i][j], " \n"[j + 1 == n]);
	return 0;
}
0