結果

問題 No.517 壊れたアクセサリー
ユーザー hashiryohashiryo
提出日時 2019-01-29 23:12:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,130 bytes
コンパイル時間 855 ms
コンパイル使用メモリ 100,120 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 05:14:35
合計ジャッジ時間 1,601 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0