結果

問題 No.1078 I love Matrix Construction
ユーザー okok
提出日時 2020-06-13 01:34:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 3,661 bytes
コンパイル時間 1,146 ms
コンパイル使用メモリ 97,224 KB
実行使用メモリ 69,240 KB
最終ジャッジ日時 2023-09-06 11:38:23
合計ジャッジ時間 5,849 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 26 ms
12,536 KB
testcase_03 AC 78 ms
28,640 KB
testcase_04 AC 118 ms
37,540 KB
testcase_05 AC 105 ms
31,980 KB
testcase_06 AC 25 ms
12,572 KB
testcase_07 AC 10 ms
6,660 KB
testcase_08 AC 96 ms
32,088 KB
testcase_09 AC 5 ms
4,420 KB
testcase_10 AC 282 ms
69,240 KB
testcase_11 AC 147 ms
39,492 KB
testcase_12 AC 260 ms
57,368 KB
testcase_13 AC 239 ms
64,808 KB
testcase_14 AC 145 ms
46,596 KB
testcase_15 AC 229 ms
61,648 KB
testcase_16 AC 8 ms
5,636 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 18 ms
10,092 KB
testcase_19 AC 41 ms
19,052 KB
testcase_20 AC 42 ms
18,720 KB
testcase_21 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


void dfs1(vector<vector<pair<int,int>>> &graph, vector<int> &num, vector<int> &used, int node, int &con){
        
        if(used[node]) return ;
        
        used[node] = true;
        
        for(pair<int,int> next : graph[node]){
                dfs1(graph, num, used, next.first, con);
        }
        
        num[con++] = node;
}

void dfs2(vector<vector<pair<int,int>>> &graph, vector<int> &scc, vector<int> &used, int node, int same){
        
        if(used[node]) return ;
        
        used[node] = true;
        
        for(pair<int,int> next : graph[node]){
                dfs2(graph, scc, used, next.first, same);
        }
        
        scc[node] = same;
}

void Strongly_Connected_Components(vector<vector<pair<int,int>>> &graph, vector<int> &scc){
        vector<int>  used(graph.size()), num(graph.size());
        vector<vector<pair<int,int>>> graph2(graph.size());
        int con = 0;
        
        scc.resize(graph.size());
        
        for(int i = 0; i < graph.size(); i++){
                dfs1(graph, num, used, i, con);
        }
        
        for(int i = 0; i < graph.size(); i++){
                for(int j = 0; j < graph[i].size(); j++){
                        graph2[graph[i][j].first].push_back(make_pair(i, graph[i][j].second));
                }
        }
        
        used.clear();
        used.resize(graph.size());
        
        for(int i = (int)graph.size()-1, same = 0; i >= 0; i--){
                if(!used[num[i]]){
                        dfs2(graph2, scc, used, num[i], same);
                        same++;
                }
        }
        
}


bool two_satisfiability(int num, vector<pair<pair<bool,int>,pair<bool,int>>>& closure, vector<bool>& val){
	vector<vector<pair<int,int>>> graph(num*2);
	vector<int> scc;
	
	val.resize(num);
	
	for(int i = 0; i < closure.size(); i++){
		graph[!closure[i].first.first + closure[i].first.second*2].push_back({closure[i].second.first  + closure[i].second.second*2 ,0});
		graph[!closure[i].second.first + closure[i].second.second*2].push_back({closure[i].first.first  + closure[i].first.second*2 ,0});
	}
	
	Strongly_Connected_Components(graph, scc);
	
	for(int i = 0; i < num*2; i += 2){
		if(scc[i] == scc[i+1]){
			return false;
		} else if(scc[i] > scc[i+1]){
			val[i/2] = true;
		} else {
			val[i/2] = false;
		}
	}
	return true;
}

bool check(pair<int,int> a, pair<int,int> b){
	bool res;
	
	res = (a.second - a.first + 1 + b.second - b.first + 1 > max(a.second, b.second) - min(a.first, b.first) + 1);
	
	return res;
}

signed main(){
	cout<<fixed<<setprecision(10);
	
	int N;
	vector<int> S, T, U;
	vector<pair<pair<bool,int>,pair<bool,int>>> closure;
	vector<bool> val;
	cin>>N;
	
	S.resize(N);
	T.resize(N);
	U.resize(N);
	
	for(int i = 0; i < N; i++){
		cin>>S[i];
		S[i]--;
	}
	
	for(int i = 0; i < N; i++){
		cin>>T[i];
		T[i]--;
	}
	
	for(int i = 0; i < N; i++){
		cin>>U[i];
	}
	
	for(int i = 0; i < N; i++){
		for(int j = 0; j < N; j++){
			closure.push_back({{U[i]%2, S[i] * N + j},{U[i]/2, j * N + T[i]}});
		}
	}
	
	if(two_satisfiability(N * N, closure, val)) {
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				if(j) cout<<" "; 
				cout<<val[i * N + j];
			}
			cout<<endl;
		}
	} else {
		cout<<-1<<endl;
	}
	return 0;
}
0