結果
| 問題 |
No.470 Inverse S+T Problem
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-20 22:52:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 13 ms / 2,000 ms |
| コード長 | 4,644 bytes |
| コンパイル時間 | 1,658 ms |
| コンパイル使用メモリ | 150,384 KB |
| 最終ジャッジ日時 | 2025-01-18 02:59:21 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
struct StronglyConnectedComponents{
using G = vector<vector<int>>;
G graph;
G rev;
int n;
int scc_sz = 0;
G scc_graph;
vector<int> scc_id;
vector<int> vs;
vector<bool> visited;
StronglyConnectedComponents() = default;
StronglyConnectedComponents(int V) : n(V) {
graph.resize(n);
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
}
StronglyConnectedComponents(G& g) : graph(g), n(g.size()) {
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
for(int u = 0; u < n; ++u){
for(int v : graph[u]) rev[v].push_back(u);
}
}
void resize(int V){
n = V;
graph.resize(n);
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
}
void add_edge(int u, int v){
graph[u].emplace_back(v);
rev[v].emplace_back(u);
}
void dfs(int from){
visited[from] = true;
for(int to : graph[from]){
if(!visited[to]) dfs(to);
}
vs.push_back(from);
}
void rdfs(int from, int k){
scc_id[from] = k;
for(int to : rev[from]){
if(scc_id[to] == -1) rdfs(to, k);
}
}
void build(){
for(int v = 0; v < n; ++v){
if(!visited[v]) dfs(v);
}
reverse(vs.begin(), vs.end());
for(int v : vs){
if(scc_id[v] == -1) rdfs(v, scc_sz++);
}
scc_graph.resize(scc_sz);
for(int v = 0; v < n; ++v){
scc_graph[scc_id[v]].push_back(v);
}
return;
}
int size() { return scc_sz; }
vector<int> get_set(int k) { return scc_graph[k]; }
int operator[](int v) { return scc_id[v]; }
};
struct TwoSAT {
StronglyConnectedComponents scc;
int n;
vector<int> sat;
TwoSAT(int V) : n(V) {
scc.resize(n * 2);
sat.resize(n);
}
// [false] [true]
void add_edge(int i, bool f, int j, bool g){
scc.add_edge(i * 2 + (f ? 0 : 1), j * 2 + (g ? 1 : 0));
scc.add_edge(j * 2 + (g ? 0 : 1), i * 2 + (f ? 1 : 0));
}
// sat[v] = true : v = false
// sat[v] = false : v = true
bool satisfiable(){
scc.build();
for(int i = 0; i < n; ++i){
if(scc[i * 2] == scc[i * 2 + 1]) return false;
sat[i] = scc[i * 2] < scc[i * 2 + 1];
}
return true;
}
vector<int> get_sat() { return sat; }
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
if(N > 1000){
cout << "Impossible\n";
return 0;
}
vector<string> s(N);
for(int i = 0; i < N; ++i) cin >> s[i];
TwoSAT ts(N);
for(int i = 0; i < N; ++i){
for(int j = 0; j < i; ++j){
if(s[i][0] == s[j][0] || s[i].substr(1, 2) == s[j].substr(1, 2)){
ts.add_edge(i, false, j, false);
}
if(s[i][0] == s[j][2] || s[i].substr(1, 2) == s[j].substr(0, 2)){
ts.add_edge(i, false, j, true);
}
if(s[i][2] == s[j][0] || s[i].substr(0, 2) == s[j].substr(1, 2)){
ts.add_edge(i, true, j, false);
}
if(s[i][2] == s[j][2] || s[i].substr(0, 2) == s[j].substr(0, 2)){
ts.add_edge(i, true, j, true);
}
}
}
if(!ts.satisfiable()){
cout << "Impossible\n";
return 0;
}
auto ans = ts.get_sat();
for(int i = 0; i < N; ++i){
if(ans[i]) cout << s[i][0] << ' ' << s[i].substr(1, 2) << '\n';
else cout << s[i].substr(0, 2) << ' ' << s[i][2] << '\n';
}
return 0;
}