結果
| 問題 |
No.470 Inverse S+T Problem
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-20 03:25:48 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,831 bytes |
| コンパイル時間 | 1,004 ms |
| コンパイル使用メモリ | 82,344 KB |
| 実行使用メモリ | 7,040 KB |
| 最終ジャッジ日時 | 2024-12-22 13:24:41 |
| 合計ジャッジ時間 | 2,373 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 22 WA * 5 |
ソースコード
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAX_V 10000
#define NOT(n) (((n)+V)%(V*2))
#define ADD(a,b) add_edge(NOT(a),b),add_edge(NOT(b),a)
int V;
vector<int> G[MAX_V];
vector<int> rG[MAX_V];
vector<int> vs;
bool used[MAX_V];
int cmp[MAX_V];
void add_edge(int from, int to){
G[from].push_back(to); rG[to].push_back(from);
}
void dfs(int v) {
used[v] = true;
for (int i = 0; i < G[v].size(); i++) {
if (!used[G[v][i]]) dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v, int k) {
used[v] = true;
cmp[v] = k;
for (int i = 0; i < rG[v].size(); i++) {
if (!used[rG[v][i]]) rdfs(rG[v][i], k);
}
}
int scc() {
memset(used, 0, sizeof(used)); vs.clear();
for (int v = 0; v < V; v++) {
if (!used[v]) dfs(v);
}
memset(used, 0, sizeof(used));
int k = 0;
for (int i = vs.size() - 1; i >= 0; i--) {
if (!used[vs[i]]) rdfs(vs[i], k++);
}
return k;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int i;
cin>>V;
if(V>52*52){
cout<<0<<endl;
return 0;
}
vector<string>v(V);
for(int i=0;i<V;i++)cin>>v[i];
for(int i=0;i<V;i++)for(int j=i+1;j<V;j++){
if(v[i].substr(0,1)==v[j].substr(0,1) || v[i].substr(1,2)==v[j].substr(1,2))ADD(NOT(i),NOT(j));
if(v[i].substr(0,1)==v[j].substr(2,1) || v[i].substr(1,2)==v[j].substr(0,2))ADD(NOT(i),j);
if(v[i].substr(0,2)==v[j].substr(1,2) || v[i].substr(2,1)==v[j].substr(0,1))ADD(i,NOT(j));
if(v[i].substr(0,2)==v[j].substr(0,2) || v[i].substr(2,1)==v[j].substr(2,1))ADD(i,j);
}
V*=2;
scc();
V/=2;
for(i=0;i<V;i++)if(cmp[i]==cmp[i+V])break;
if(i<V)cout<<"Impossible"<<endl;
else for(i=0;i<V;i++){
if(cmp[i]>cmp[i+V]){
cout<<v[i].substr(0,1)<<' '<<v[i].substr(1,2)<<'\n';
}else{
cout<<v[i].substr(0,2)<<' '<<v[i].substr(2,1)<<'\n';
}
}
}