結果
| 問題 |
No.470 Inverse S+T Problem
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-20 01:09:02 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,029 bytes |
| コンパイル時間 | 2,351 ms |
| コンパイル使用メモリ | 187,664 KB |
| 実行使用メモリ | 6,912 KB |
| 最終ジャッジ日時 | 2024-12-22 13:12:09 |
| 合計ジャッジ時間 | 3,936 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 2 |
| other | AC * 18 WA * 9 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
class StronglyConnectedComponents {
private:
const vector<vector<int>> &graph;
vector<vector<int>> reversedGraph;
vector<vector<int>> compressedGraph;
vector<int> componentID;
vector<int> postOrder;
vector<bool> visited;
stack<int> adjacent;
public:
StronglyConnectedComponents(const vector<vector<int>> &graph) :
graph(graph),
reversedGraph(graph.size()),
componentID(graph.size(), -1),
visited(graph.size()) {
for (int i = 0; i < graph.size(); i++) {
for (int j : graph[i]) {
reversedGraph[j].push_back(i);
}
}
for (int i = 0; i < graph.size(); i++) {
dfs(i);
}
reverse(postOrder.begin(), postOrder.end());
for (int i : postOrder) {
if (componentID[i] == -1) {
dfs2(i);
while (!adjacent.empty()) {
visited[adjacent.top()] = true;
adjacent.pop();
}
compressedGraph.emplace_back(vector<int>());
}
}
}
vector<vector<int>> &getCompressedGraph() {
return compressedGraph;
}
int operator[](int u) {
return componentID[u];
}
private:
void dfs(int u) {
if (visited[u]) {
return;
}
visited[u] = true;
for (int v : graph[u]) {
dfs(v);
}
postOrder.push_back(u);
}
void dfs2(int u) {
componentID[u] = compressedGraph.size();
for (int v : reversedGraph[u]) {
if (componentID[v] == -1) {
dfs2(v);
} else if (componentID[v] != componentID[u] && visited[componentID[v]]) {
compressedGraph[componentID[v]].push_back(componentID[u]);
visited[componentID[v]] = false;
adjacent.push(componentID[v]);
}
}
}
};
struct SAT2 {
vector<vector<int>> g;
vector<bool> var;
bool satisfiable;
SAT2(int n) : g(n * 2), var(n) {}
// i or j
void add(int i, int ii, int j, int jj) {
g[i * 2 + 1 - ii].push_back(j * 2 + jj);
g[j * 2 + 1 - jj].push_back(i * 2 + ii);
}
bool isSatisfiable() {
return satisfiable;
}
void build() {
StronglyConnectedComponents scc(g);
satisfiable = true;
for (int i = 0; i < var.size(); i++) {
if (scc[i * 2] == scc[i * 2 + 1]) {
satisfiable = false;
}
var[i] = scc[i * 2] > scc[i * 2 + 1];
}
}
bool operator[](int k) {
return var[k];
}
};
int main() {
int n;
cin >> n;
if (n >= 3000) {
puts("Impossible");
return 0;
}
vector<string> s(n);
SAT2 sat(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
string a = s[i];
string b = s[j];
if (a.substr(0, 1) == b.substr(0, 1) || a.substr(1, 2) == b.substr(1, 2)) {
sat.add(i, true, j, true);
}
if (a.substr(0, 1) == b.substr(2, 1) || a.substr(1, 2) == b.substr(0, 2)) {
sat.add(i, true, j, false);
}
if (a.substr(0, 2) == b.substr(1, 2) || a.substr(2, 1) == b.substr(0, 1)) {
sat.add(i, false, j, true);
}
if (a.substr(0, 2) == b.substr(0, 2) || a.substr(2, 1) == b.substr(2, 1)) {
sat.add(i, false, j, false);
}
}
}
sat.build();
if (!sat.isSatisfiable()) {
puts("Impossible");
} else {
for (int i = 0; i < n; i++) {
if (sat[i]) {
cout << s[i].substr(0, 2) << " " << s[i].substr(2, 1) << endl;
} else {
cout << s[i].substr(0, 1) << " " << s[i].substr(1, 2) << endl;
}
}
}
}