結果
問題 |
No.470 Inverse S+T Problem
|
ユーザー |
|
提出日時 | 2016-12-20 03:22:58 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 212 ms / 2,000 ms |
コード長 | 2,087 bytes |
コンパイル時間 | 1,131 ms |
コンパイル使用メモリ | 88,856 KB |
実行使用メモリ | 7,424 KB |
最終ジャッジ日時 | 2024-12-22 13:24:35 |
合計ジャッジ時間 | 2,632 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 27 |
ソースコード
#include <iostream> #include <string> #include <vector> #include <stack> #include <algorithm> using namespace std; typedef vector<vector<int> > Graph; #define VAR(x) ((x) << 1) #define NOT(x) ((x) ^ 1) void visit(int v, const Graph &g, vector<int> &ord, vector<int> &num, int k) { if (num[v] >= 0) return; num[v] = k; for (int i = 0; i < g[v].size(); ++i) visit(g[v][i], g, ord, num, k); ord.push_back(v); } typedef pair<int,int> clause; void two_satisfiability(int m, const vector<clause> &cs, vector<int> &num) { int n = m * 2; // m positive vars and m negative vars Graph g(n), h(n); for (int i = 0; i < cs.size(); ++i) { int u = cs[i].first, v = cs[i].second; g[NOT(u)].push_back( v ); g[NOT(v)].push_back( u ); h[v].push_back( NOT(u) ); h[u].push_back( NOT(v) ); } vector<int> ord, dro; num.resize(n); fill(num.begin(),num.end(),-1); for (int i = 0; i < n; ++i) visit(i, g, ord, num, i); reverse(ord.begin(), ord.end()); fill(num.begin(), num.end(), -1); for (int i = 0; i < n; ++i) visit(ord[i], h, dro, num, i); } int main(){ int N,i; cin>>N; if(N>52*52){ cout<<"Impossible"<<endl; return 0; } vector<string>v(N); for(int i=0;i<N;i++)cin>>v[i]; vector<clause>cs; for(int i=0;i<N;i++)for(int j=i+1;j<N;j++){ if(v[i].substr(0,1)==v[j].substr(0,1) || v[i].substr(1,2)==v[j].substr(1,2))cs.emplace_back(NOT(VAR(i)),NOT(VAR(j))); if(v[i].substr(0,1)==v[j].substr(2,1) || v[i].substr(1,2)==v[j].substr(0,2))cs.emplace_back(NOT(VAR(i)),VAR(j)); if(v[i].substr(0,2)==v[j].substr(1,2) || v[i].substr(2,1)==v[j].substr(0,1))cs.emplace_back(VAR(i),NOT(VAR(j))); if(v[i].substr(0,2)==v[j].substr(0,2) || v[i].substr(2,1)==v[j].substr(2,1))cs.emplace_back(VAR(i),VAR(j)); } vector<int>num; two_satisfiability(N,cs,num); for(i=0;i<N;i++)if(num[VAR(i)]==num[NOT(VAR(i))])break; if(i<N)cout<<"Impossible"<<endl; else for(i=0;i<N;i++){ if(num[VAR(i)]>num[NOT(VAR(i))]){ cout<<v[i].substr(0,1)<<' '<<v[i].substr(1,2)<<endl; }else{ cout<<v[i].substr(0,2)<<' '<<v[i].substr(2,1)<<endl; } } }