結果

問題 No.1078 I love Matrix Construction
ユーザー 👑 kmjpkmjp
提出日時 2020-06-12 23:29:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 2,402 bytes
コンパイル時間 2,520 ms
コンパイル使用メモリ 212,932 KB
実行使用メモリ 76,644 KB
最終ジャッジ日時 2023-09-06 11:26:27
合計ジャッジ時間 7,928 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
27,976 KB
testcase_01 AC 10 ms
28,084 KB
testcase_02 AC 32 ms
35,008 KB
testcase_03 AC 96 ms
46,108 KB
testcase_04 AC 147 ms
53,336 KB
testcase_05 AC 116 ms
48,932 KB
testcase_06 AC 33 ms
34,444 KB
testcase_07 AC 19 ms
30,188 KB
testcase_08 AC 124 ms
48,948 KB
testcase_09 AC 13 ms
28,836 KB
testcase_10 AC 327 ms
76,644 KB
testcase_11 AC 141 ms
54,436 KB
testcase_12 AC 257 ms
68,072 KB
testcase_13 AC 295 ms
73,008 KB
testcase_14 AC 201 ms
58,636 KB
testcase_15 AC 282 ms
70,772 KB
testcase_16 AC 17 ms
29,656 KB
testcase_17 AC 10 ms
27,948 KB
testcase_18 AC 27 ms
32,840 KB
testcase_19 AC 60 ms
39,388 KB
testcase_20 AC 59 ms
39,140 KB
testcase_21 AC 11 ms
28,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

class SCC {
public:
	static const int MV = 500*500*2;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<NV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0,i; SC.clear(); SC.resize(NV); NUM.clear();
		assert(NV);
		FOR(i,NV) vis[i]=0; FOR(i,NV) if(!vis[i]) dfs(i); FOR(i,NV) vis[i]=0;
		for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};

class TwoSat {
	int NV;
	SCC sc;
public:
	vector<int> val;
	void init(int NV) { this->NV=NV*2; sc.init(NV*2); val.resize(NV);}
	void add_edge(int x,int y) { // k+0:normal k+NV:inverse
		sc.add_edge((x+NV/2)%NV,y);
		sc.add_edge((y+NV/2)%NV,x);
	}
	bool sat() { // empty:false 
		sc.scc();
		for(int i=0;i<NV/2;i++) if(sc.GR[i]==sc.GR[i+NV/2]) return false;
		for(int i=0;i<NV/2;i++) val[i]=sc.GR[i]>sc.GR[i+NV/2];
		return true;
	}
};

TwoSat ts;
int N;
int S[505],T[505],U[505];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	ts.init(N*N);
	FOR(i,N) cin>>S[i];
	FOR(i,N) cin>>T[i];
	FOR(i,N) {
		cin>>U[i];
		S[i]--;
		T[i]--;
		FOR(j,N) ts.add_edge(S[i]*N+j+(U[i]&1)*N*N,j*N+T[i]+(U[i]/2)*N*N);
	}
	
	if(ts.sat()) {
		FOR(y,N) {
			FOR(x,N) cout<<ts.val[y*N+x]<<" ";
			cout<<endl;
		}
	}
	else {
		cout<<-1<<endl;
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0