結果

問題 No.1612 I hate Construct a Palindrome
ユーザー 👑 jupirojupiro
提出日時 2021-07-21 22:40:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 4,866 bytes
コンパイル時間 2,647 ms
コンパイル使用メモリ 225,824 KB
実行使用メモリ 15,892 KB
最終ジャッジ日時 2023-09-24 18:55:41
合計ジャッジ時間 6,744 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 86 ms
13,264 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 83 ms
13,400 KB
testcase_06 AC 69 ms
12,096 KB
testcase_07 AC 71 ms
12,084 KB
testcase_08 AC 70 ms
11,896 KB
testcase_09 AC 72 ms
11,648 KB
testcase_10 AC 71 ms
11,716 KB
testcase_11 AC 71 ms
11,796 KB
testcase_12 AC 77 ms
12,036 KB
testcase_13 AC 78 ms
12,256 KB
testcase_14 AC 80 ms
12,136 KB
testcase_15 AC 135 ms
15,892 KB
testcase_16 AC 127 ms
15,588 KB
testcase_17 AC 131 ms
15,576 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using std::cin;
using std::cout;
using std::endl;
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;


bool palindrome(const std::string &s)
{
  int l = 0;
  int r = (int)s.size() - 1;
  while (l < r)
  {
    if(s[l] != s[r])
      return false;
    l += 1, r -= 1;
  }
  return true;
}
void solve()
{
  int n, m; cin >> n >> m;
  std::vector<std::vector<std::pair<int, int>>> g(n);
  std::vector<std::vector<int>> edge(26);
  std::vector<std::pair<int,int>> vp(m);
  std::vector<char> c(m);
  for (int i = 0; i < m; ++i)
  {
    int u, v; cin >> u >> v;
    u -= 1, v -= 1;
    vp[i] = {u, v};
    cin >> c[i];
    g[u].emplace_back(v, i);
    g[v].emplace_back(u, i);
    edge[c[i] - 'a'].emplace_back(i);
  }
  std::vector<std::pair<int, int>> e(2, { -1, -1 });
  std::vector<int> eid(2, -1);
  for (int i = 0; i < 26; ++i)
  {
    if(edge[i].empty())
      continue;
    if(e[0].first == -1)
    {
      eid[0] = edge[i][0];
      e[0] = vp[edge[i][0]];
    }
    else
    {
      eid[1] = edge[i][0];
      e[1] = vp[edge[i][0]];
    }
  }
  if(e[1].first == -1)
  {
    cout << -1 << "\n";
    return;
  }
  {
    std::vector<int> d(n, inf);
    std::queue<int> q;
    q.emplace(0);
    d[0] = 0;
    while(not q.empty())
    {
      const int cur = q.front();
      q.pop();
      for (const auto &[nxt, id] : g[cur])
      {
        if(chmin(d[nxt], d[cur] + 1))
        {
          q.emplace(nxt);
        }
      }
    }
    for (int i = 0; i < 2; i++)
      if(d[e[i].first] > d[e[i].second])
        std::swap(e[i].first, e[i].second);
    if(d[e[0].first] > d[e[1].first])
    {
      std::swap(eid[0], eid[1]);
      std::swap(e[0], e[1]);
    }
  }
  std::vector<int> res;
  std::string s;
  {
    const int st = 0;
    std::vector<int> d(n, inf), b(n, -1), bid(n, -1);
    std::queue<int> q;
    q.emplace(st);
    d[st] = 0;
    while(not q.empty())
    {
      const int cur = q.front();
      q.pop();
      for (const auto &[nxt, id] : g[cur])
      {
        if(chmin(d[nxt], d[cur] + 1))
        {
          b[nxt] = cur;
          bid[nxt] = id;
          q.emplace(nxt);
        }
      }
    }
    std::string t;
    std::vector<int> ans;
    int to = e[0].first;
    while (to != st)
    {
      t += c[bid[to]];
      ans.emplace_back(bid[to]);
      to = b[to];
    }
    std::reverse(t.begin(), t.end());
    std::reverse(ans.begin(), ans.end());
    s += t;
    for (const auto &e : ans)
      res.emplace_back(e);
  }
  s += c[eid[0]];
  res.emplace_back(eid[0]);
  {
    const int st = e[0].second;
    std::vector<int> d(n, inf), b(n, -1), bid(n, -1);
    std::queue<int> q;
    q.emplace(st);
    d[st] = 0;
    while(not q.empty())
    {
      const int cur = q.front();
      q.pop();
      for (const auto &[nxt, id] : g[cur])
      {
        if(chmin(d[nxt], d[cur] + 1))
        {
          b[nxt] = cur;
          bid[nxt] = id;
          q.emplace(nxt);
        }
      }
    }
    if(d[e[1].first] > d[e[1].second])
      std::swap(e[1].first, e[1].second);
    std::string t;
    std::vector<int> ans;
    int to = e[1].first;
    while (to != st)
    {
      t += c[bid[to]];
      ans.emplace_back(bid[to]);
      to = b[to];
    }
    std::reverse(t.begin(), t.end());
    std::reverse(ans.begin(), ans.end());
    s += t;
    for (const auto &e : ans)
      res.emplace_back(e);
  }
  s += c[eid[1]];
  res.emplace_back(eid[1]);
  {
    const int st = e[1].second;
    std::vector<int> d(n, inf), b(n, -1), bid(n, -1);
    std::queue<int> q;
    q.emplace(st);
    d[st] = 0;
    while(not q.empty())
    {
      const int cur = q.front();
      q.pop();
      for (const auto &[nxt, id] : g[cur])
      {
        if(chmin(d[nxt], d[cur] + 1))
        {
          b[nxt] = cur;
          bid[nxt] = id;
          q.emplace(nxt);
        }
      }
    }
    std::string t;
    std::vector<int> ans;
    int to = n - 1;
    while (to != st)
    {
      t += c[bid[to]];
      ans.emplace_back(bid[to]);
      to = b[to];
    }
    std::reverse(t.begin(), t.end());
    std::reverse(ans.begin(), ans.end());
    s += t;
    for (const auto &e : ans)
      res.emplace_back(e);
  }
  if (palindrome(s))
  {
    s += c[g[n - 1][0].second];
    s += c[g[n - 1][0].second];
    res.emplace_back(g[n - 1][0].second);
    res.emplace_back(g[n - 1][0].second);
  }
  cout << res.size() << "\n";
  for (const auto & e : res)
    cout << e + 1 << "\n";
}
int main()
{
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int kkt = 1; 
  // cin >> kkt;
  while(kkt--)
    solve();
  return 0;
  
}
0