結果
問題 | No.517 壊れたアクセサリー |
ユーザー |
![]() |
提出日時 | 2019-01-29 23:12:22 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 3,130 bytes |
コンパイル時間 | 995 ms |
コンパイル使用メモリ | 98,728 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-10-12 00:14:28 |
合計ジャッジ時間 | 1,874 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <string>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <limits.h>#include <math.h>#include <functional>#include <bitset>#include <iomanip>#include <cassert>#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))#define debug(x) cerr << #x << ": " << x << '\n'#define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n'#define debugArrayP(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i].first<< " " << x[i].second << '\n'using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> Pii;typedef vector<int> vint;typedef vector<ll> vll;const ll INF = LLONG_MAX;const ll MOD = 1e9+7;struct UnionFind {vector<int> data;UnionFind(int size) : data(size, -1) { }bool unionSet(int x, int y) {x = root(x); y = root(y);if (x != y) {if (data[y] < data[x]) swap(x, y);data[x] += data[y]; data[y] = x;}return x != y;}bool findSet(int x, int y) {return root(x) == root(y);}int root(int x) {return data[x] < 0 ? x : data[x] = root(data[x]);}int size(int x) {return -data[root(x)];}};typedef ll Weight;struct Edge {int src, dst;Weight weight;Edge(int src, int dst, Weight weight) :src(src), dst(dst), weight(weight) { }};bool operator < (const Edge &e, const Edge &f) {return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!e.src != f.src ? e.src < f.src : e.dst < f.dst;}typedef vector<Edge> Edges;typedef vector<Edges> Graph;typedef vector<Weight> Array;typedef vector<Array> Matrix;bool visit(const Graph &g, int v, vector<int> &order, vector<int> &color) {color[v] = 1;for(auto e: g[v]) {if (color[e.dst] == 2) continue;if (color[e.dst] == 1) return false;if (!visit(g, e.dst, order, color)) return false;}order.push_back(v); color[v] = 2;return true;}bool topologicalSort(const Graph &g, vector<int> &order) {int n = g.size();vector<int> color(n);repeat(u, n) if (!color[u] && !visit(g, u, order, color)) return false;reverse(order.begin(),order.end());return true;}bool used[26];int main(){cin.tie(0);ios::sync_with_stdio(false);Graph G(26);UnionFind uf(26);string s;int N;cin>>N;int length=0;repeat(i,N){cin>>s;length += s.length();repeat(c,s.length()-1){used[s[c]-'A']=true;G[s[c]-'A'].push_back({s[c]-'A',s[c+1]-'A',1});uf.unionSet(s[c]-'A',s[c+1]-'A');}used[s.back()-'A']=true;}int M;cin>>M;repeat(i,M){cin>>s;repeat(c,s.length()-1){G[s[c]-'A'].push_back({s[c]-'A',s[c+1]-'A',1});uf.unionSet(s[c]-'A',s[c+1]-'A');}}vint order;topologicalSort(G,order);string ans;repeat(i,26)if(used[order[i]]&&uf.size(order[i])==length){ans += order[i]+'A';}if(ans.length()==0){cout<<-1<<endl;}else{cout << ans << endl;}return 0;}