結果

問題 No.470 Inverse S+T Problem
ユーザー pazzle1230pazzle1230
提出日時 2017-09-15 02:41:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 3,624 bytes
コンパイル時間 1,982 ms
コンパイル使用メモリ 129,944 KB
実行使用メモリ 7,280 KB
最終ジャッジ日時 2023-08-24 01:29:18
合計ジャッジ時間 3,285 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,956 KB
testcase_01 AC 3 ms
6,960 KB
testcase_02 AC 3 ms
6,952 KB
testcase_03 AC 4 ms
7,040 KB
testcase_04 AC 4 ms
6,960 KB
testcase_05 AC 3 ms
6,900 KB
testcase_06 AC 3 ms
6,952 KB
testcase_07 AC 3 ms
7,092 KB
testcase_08 AC 4 ms
7,228 KB
testcase_09 AC 3 ms
6,960 KB
testcase_10 AC 3 ms
6,924 KB
testcase_11 AC 4 ms
6,936 KB
testcase_12 AC 3 ms
7,100 KB
testcase_13 AC 3 ms
6,976 KB
testcase_14 AC 4 ms
7,060 KB
testcase_15 AC 4 ms
6,980 KB
testcase_16 AC 3 ms
6,972 KB
testcase_17 AC 3 ms
7,056 KB
testcase_18 AC 3 ms
7,168 KB
testcase_19 AC 4 ms
7,012 KB
testcase_20 AC 4 ms
6,996 KB
testcase_21 AC 4 ms
7,004 KB
testcase_22 AC 4 ms
7,132 KB
testcase_23 AC 4 ms
6,980 KB
testcase_24 AC 5 ms
6,980 KB
testcase_25 AC 4 ms
7,280 KB
testcase_26 AC 4 ms
7,120 KB
testcase_27 AC 4 ms
7,072 KB
testcase_28 AC 3 ms
6,916 KB
testcase_29 AC 4 ms
7,044 KB
testcase_30 AC 4 ms
6,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define INF_LL (ll)1e18
#define INF (int)1e9
#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
using ll = long long;
using PII = pair<int, int>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

class SCC{
private:
	vector<vector<int> > gg, rg;
	vector<int> order, comp;
	vector<bool> used;
	vector<vector<int> > ng, vs;

	int n, nn;
public:
	SCC(){}
	SCC(int v) : gg(v), rg(v), comp(v, -1), used(v, 0), n(v){}

	void add_edge(int x, int y){
		gg[x].push_back(y);
		rg[y].push_back(x);
	}

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

	void dfs(int v){
		used[v] = true;
		REP(i, gg[v].size()){
			if(!used[gg[v][i]]) dfs(gg[v][i]);
		}
		order.push_back(v);
	}

	void rdfs(int v, int k){
		used[v] = true;
		comp[v] = k;
		REP(i, rg[v].size()){
			if(!used[rg[v][i]]) rdfs(rg[v][i], k);
		}
	}

	int build(){
		REP(i, n){
			if(!used[i]) dfs(i);
		}
		fill(all(used), 0);
		int k = 0;
		for(int i = order.size()-1;i >= 0;i--){
			if(!used[order[i]]) rdfs(order[i], k++);
		}
		nn = k;

		//---------それぞれの強連結成分に含まれる頂点の番号----------
		vs.resize(k, vector<int>());

		REP(i, n)
			vs[comp[i]].push_back(i);
		//-----------------------------------------------------------

		//---------強連結成分をまとめた後のNew Graph!----------------
		ng.resize(k, vector<int>());
		REP(i, n){
			REP(j, gg[i].size()){
				if(comp[i] != comp[gg[i][j]])
					ng[comp[i]].push_back(comp[gg[i][j]]);
			}
		}
		REP(i, nn){
			sort(all(ng[i]));
			ng[i].erase(unique(all(ng[i])), ng[i].end());
		}
		//------------------------------------------------------------
		return k;
	}
	
	int size(){
		return nn;
	}

	vector<vector<int> > graph(){
		return ng;
	}

	vector<int> vertices(int v){
		return vs[v];
	}
};

class TwoSAT{
private:
	SCC scc;
	int n;
	vector<bool> truth;
public:
	TwoSAT(){}
	TwoSAT(int _n) : n(_n), scc(2*_n){}

	void add_state(int a, bool truth1, int b, bool truth2){
		int na = truth1 ? a+n : a, nb = truth2 ? b+n : b;
		a = truth1 ? a : a+n; b = truth2 ? b : b+n;
		scc.add_edge(na, b);
		scc.add_edge(nb, a);
	}

	bool build(){
		scc.build();
		REP(i, n){
			if(scc[i] == scc[i+n]) return false;
			if(scc[i] > scc[i+n]) truth.push_back(true);
			else truth.push_back(false);
		}
		return true;
	}

	vector<bool> result(){
		return truth;
	}

	bool operator[](int k){
		return truth[k];
	}
};

int main(void){
	int N;
	string u[114514];
	map<string, vector<PII> > mp;
	cin >> N;
	if(N > 100){
		cout << "Impossible" << endl;
		return 0;
	}

	TwoSAT tsat(N);
	REP(i, N){
		cin >> u[i];
		mp[u[i].substr(0, 1)].push_back({i, 1});
		mp[u[i].substr(1, 2)].push_back({i, 1});
		mp[u[i].substr(0, 2)].push_back({i, 0});
		mp[u[i].substr(2, 1)].push_back({i, 0});
	}

	for(auto ei : mp){
		auto v = ei.second;
		REP(i, v.size()){
			FOR(j, i+1, v.size()){
				tsat.add_state(v[i].first, 1-v[i].second, v[j].first, 1-v[j].second);
			}
		}
	}
	if(!tsat.build()){
		cout << "Impossible" << endl;
		return 0;
	}

	REP(i, N){
		if(tsat[i]){
			cout << u[i].substr(0, 1) << " " << u[i].substr(1, 2) << endl;
		}else{
			cout << u[i].substr(0, 2) << " " << u[i].substr(2, 1) << endl;
		}
	}
}
0