結果
| 問題 |
No.470 Inverse S+T Problem
|
| コンテスト | |
| ユーザー |
tancahn2380
|
| 提出日時 | 2019-06-11 23:41:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,351 bytes |
| コンパイル時間 | 2,947 ms |
| コンパイル使用メモリ | 214,008 KB |
| 最終ジャッジ日時 | 2025-01-07 04:17:54 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 22 WA * 5 |
ソースコード
# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq) (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw) sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe) transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr) transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy) for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu) FOR(i,0,upupu)
# define INIT std::ios::sync_with_stdio(false);std::cin.tie(0)
struct SCC{
private:
int v;
vector<vector<int>> G; //グラフの隣接リスト表現
vector<vector<int>> rG; //辺の向きを逆にしたグラフ
vector<int> vs; //帰りがけ順の並び
vector<bool> used; //すでに調べたか
public:
vector<int> cmp; //属する競連結成分のトポロジカル順序
SCC(){
}
SCC(int V){
v = V;
G.resize(v);
rG.resize(v);
used.resize(v, false);
cmp.resize(v, 0);
}
void init(int V){
v = V;
G.resize(v);
rG.resize(v);
used.resize(v, false);
cmp.resize(v, 0);
}
void add_edge(int f, int t){
G[f].emplace_back(t);
rG[t].emplace_back(f);
}
void dfs(int cur){
used[cur] = true;
for (int i = 0; i < (int)G[cur].size(); i++) {
if (!used[G[cur][i]])dfs(G[cur][i]);
}
vs.emplace_back(cur);
}
void rdfs(int cur, int k) {
used[cur] = true;
cmp[cur] = k;
for (int i = 0; i < (int)rG[cur].size(); i++) {
if (!used[rG[cur][i]])rdfs(rG[cur][i], k);
}
}
int scc() {
vs.clear();
for (int i = 0; i < v; i++) {
if (!used[i])dfs(i);
}
used.assign(v, 0);
int k = 0;
for (int i = vs.size() - 1; i >= 0; i--) {
if (!used[vs[i]])rdfs(vs[i], k++);
}
return k;
}
};
struct two_sat{
private:
int v;
SCC sc;
public:
vector<int> val;
two_sat(int V){
v = V*2;
val.resize(v/2);
sc.init(v);
}
void add_edge(int f, int t){
sc.add_edge((f + v/2)%v, t);
sc.add_edge((t + v/2)%v, f);
}
bool sat(){
sc.scc();
for(int i = 0;i < v/2;i++){
if(sc.cmp[i] == sc.cmp[i + v/2])return false;
}
for(int i = 0;i < v/2;i++){
val[i] = sc.cmp[i] > sc.cmp[i + v/2];
}
return true;
}
};
int n;
string s[101010];
int main(){
cin >> n;
REP(i, n){
cin >> s[i];
}
if(n >= 52){
cout << "Impossible" << endl;
return 0;
}
two_sat ts(n);
REP(y, n)REP(x, y){
if(s[y].substr(0, 1)==s[x].substr(0, 1) || s[y].substr(1, 2)==s[x].substr(1, 2)) ts.add_edge(y + n, x + n);
if(s[y].substr(0, 1)==s[x].substr(2, 1) || s[y].substr(1, 2)==s[x].substr(0, 2)) ts.add_edge(y + n, x);
if(s[y].substr(2, 1)==s[x].substr(0, 1) || s[y].substr(0, 2)==s[x].substr(1, 2)) ts.add_edge(y, x + n);
if(s[y].substr(2, 1)==s[x].substr(2, 1) || s[y].substr(0, 2)==s[x].substr(0, 2)) ts.add_edge(y, x);
}
if(!ts.sat()){
cout << "Impossible" << endl;
return 0;
}
REP(i, n){
if(ts.val[i] == 1)cout << s[i].substr(0, 1) << " " << s[i].substr(1, 2) << endl;
else cout << s[i].substr(0, 2) << " " << s[i].substr(2, 1) << endl;
}
}
tancahn2380