結果

問題 No.1612 I hate Construct a Palindrome
ユーザー rianoriano
提出日時 2021-07-21 22:37:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 453 ms / 2,000 ms
コード長 2,614 bytes
コンパイル時間 2,078 ms
コンパイル使用メモリ 192,996 KB
実行使用メモリ 43,484 KB
最終ジャッジ日時 2023-09-24 18:51:43
合計ジャッジ時間 9,695 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 430 ms
26,392 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 429 ms
26,892 KB
testcase_06 AC 221 ms
24,720 KB
testcase_07 AC 225 ms
24,620 KB
testcase_08 AC 205 ms
24,044 KB
testcase_09 AC 214 ms
28,140 KB
testcase_10 AC 214 ms
28,112 KB
testcase_11 AC 214 ms
28,212 KB
testcase_12 AC 233 ms
29,572 KB
testcase_13 AC 236 ms
29,696 KB
testcase_14 AC 239 ms
29,612 KB
testcase_15 AC 453 ms
42,896 KB
testcase_16 AC 449 ms
43,196 KB
testcase_17 AC 449 ms
43,484 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 3 ms
4,384 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:34:9: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   34 |     auto[aa,kk,dd] = G[pt][0]; a1 = kk; d1 = dd;
      |         ^
main.cpp:37:13: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   37 |         auto[a,k,d] = G[pt][i];
      |             ^
main.cpp:55:17: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   55 |             auto[x,k,d] = p;
      |                 ^
main.cpp:78:17: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   78 |             auto[x,k,d] = p;
      |                 ^
main.cpp:94:13: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   94 |         auto[a,k,d] = G[1][0];
      |             ^
次のファイルから読み込み:  /usr/local/gcc7/include/c++/12.2.0/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/allocator.h:46,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/string:41,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/locale_classes.h:40,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/ios_base.h:41,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/ios:42,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/istream:38,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/sstream:38,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/complex:45,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/ccomplex:39,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:54,
         次から読み込み:  main.cpp:1:
��

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<tuple<int,int,char>>>;

const ll mod = 1000000007;

int main() {
    ll N,M;
    cin >> N >> M;
    Graph G(N+1);
    vector<tuple<ll,ll,char>> path;
    vector<set<char>> cnt(N+1);
    rep(i,M){
        ll a,b; char c; cin >> a >> b >> c;
        G[a].push_back(make_tuple(b,i+1,c));
        G[b].push_back(make_tuple(a,i+1,c));
        path.push_back(make_tuple(a,b,c));
        cnt[a].insert(c); cnt[b].insert(c);
    }
    ll pt = 0;
    rep(i,N){
        if(cnt[i+1].size()>1){
            pt = i+1; break;
        }
    }
    if(pt==0){
        cout << -1 << endl; return 0;
    }
    char d1,d2; int a1,a2;
    auto[aa,kk,dd] = G[pt][0]; a1 = kk; d1 = dd;
    int ss = aa; int tt;
    rep(i,G[pt].size()){
        auto[a,k,d] = G[pt][i];
        if(d!=d1){
            a2 = k; tt = a; d2 = d; break;
        }
    }
    //cout << a1 << " " << a2 << endl;
    //cout << ss  << " " << tt << endl;
    string S; vector<ll> ans;
    //BFS (普通の幅優先探索)
    queue<int> go; ll dist[N+1],prev[N+1],used[N+1]; // ll par[N+1];
    char cc[N+1];
    rep(i,N+1){
        dist[i] = 2000000000; prev[i] = 0; used[i] = 0;
    }
    dist[tt] = 0; go.push(tt);
    while(!go.empty()){
        int s = go.front(); go.pop();
        for(auto p:G[s]){
            auto[x,k,d] = p;
            if(dist[x]<=dist[s]+1) continue;
            dist[x] = dist[s] + 1;
            prev[x] = s; used[x] = k; cc[x] = d;
            go.push(x);
        }
    }
    int n = N;
    while(prev[n]>0){
        ans.push_back(used[n]); S += cc[n];
        n = prev[n];
    }

    S += d2; S += d1;
    ans.push_back(a2); ans.push_back(a1);

    rep(i,N+1){
        dist[i] = 2000000000; prev[i] = 0; used[i] = 0;
    }
    dist[1] = 0; go.push(1);
    while(!go.empty()){
        int s = go.front(); go.pop();
        for(auto p:G[s]){
            auto[x,k,d] = p;
            if(dist[x]<=dist[s]+1) continue;
            dist[x] = dist[s] + 1;
            prev[x] = s; used[x] = k; cc[x] = d;
            go.push(x);
        }
    }
    n = ss;
    while(prev[n]>0){
        ans.push_back(used[n]); S += cc[n];
        n = prev[n];
    }
    
    string T = S;
    reverse(S.begin(),S.end());
    if(S==T){
        auto[a,k,d] = G[1][0];
        ans.push_back(k); ans.push_back(k);
    }
    reverse(ans.begin(),ans.end());
    cout << ans.size() << endl;
    for(ll yy:ans){
        cout << yy << endl;
    }
    //cout << ans << endl;
}
0