結果

問題 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  
実行時間 452 ms / 2,000 ms
コード長 2,614 bytes
コンパイル時間 2,218 ms
コンパイル使用メモリ 197,304 KB
実行使用メモリ 43,268 KB
最終ジャッジ日時 2024-07-17 19:37:23
合計ジャッジ時間 8,786 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 5 ms
5,376 KB
testcase_02 AC 418 ms
26,540 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 428 ms
26,548 KB
testcase_06 AC 217 ms
24,736 KB
testcase_07 AC 226 ms
24,740 KB
testcase_08 AC 205 ms
24,360 KB
testcase_09 AC 208 ms
28,476 KB
testcase_10 AC 210 ms
28,476 KB
testcase_11 AC 209 ms
28,460 KB
testcase_12 AC 233 ms
29,708 KB
testcase_13 AC 229 ms
29,828 KB
testcase_14 AC 230 ms
29,828 KB
testcase_15 AC 452 ms
43,268 KB
testcase_16 AC 452 ms
43,256 KB
testcase_17 AC 444 ms
43,184 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:34:9: warning: 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: warning: 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: warning: 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: warning: 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: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   94 |         auto[a,k,d] = G[1][0];
      |             ^
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/allocator.h:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/string:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/locale_classes.h:40,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/ios_base.h:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ios:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/istream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/sstream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/complex:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ccomplex:39,
                 from /home

ソースコード

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