結果

問題 No.1612 I hate Construct a Palindrome
ユーザー momoyuumomoyuu
提出日時 2024-08-09 18:38:20
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,411 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 140,880 KB
実行使用メモリ 16,768 KB
最終ジャッジ日時 2024-08-09 18:38:30
合計ジャッジ時間 9,055 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 5 ms
6,940 KB
testcase_02 AC 289 ms
14,664 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 331 ms
14,576 KB
testcase_06 AC 91 ms
11,520 KB
testcase_07 WA -
testcase_08 AC 147 ms
12,544 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 2 ms
6,944 KB
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
In lambda function,
    inlined from 'std::vector<int> solve2()' at main.cpp:189:8:
main.cpp:165:9: warning: 'wv' may be used uninitialized [-Wmaybe-uninitialized]
  165 |         if(ni==to){
      |         ^~
main.cpp: In function 'std::vector<int> solve2()':
main.cpp:148:9: note: 'wv' was declared here
  148 |     int wv;
      |         ^~
In lambda function,
    inlined from 'std::vector<int> solve2()' at main.cpp:178:8:
main.cpp:173:16: warning: 'wu' may be used uninitialized [-Wmaybe-uninitialized]
  173 |             dfs(dfs,nj,to);
      |             ~~~^~~~~~~~~~~
main.cpp: In function 'std::vector<int> solve2()':
main.cpp:146:9: note: 'wu' was declared here
  146 |     int wu,ni;
      |         ^~
main.cpp: In function 'std::vector<int> solve1()':
main.cpp:110:30: warning: 'nj' may be used uninitialized [-Wmaybe-uninitialized]
  110 |         ans.push_back(g[0][nj].second.second);
      |                              ^
main.cpp:102:13: note: 'nj' was declared here
  102 |         int nj;
      |             ^~
main.cpp:115:29: warning: 'wu' may be used uninitialized [-Wmaybe-uninitialized]
  115 |         for(int i = 0;i<g[wu].size();i++){
      |                             ^
main.cpp:66:9: note: 'wu' was declared here
   66 |     int wu,ni;
      |         ^~
main.cpp:22:30: warning: 'nj' may be used uninitialized [-Wmaybe-uninitialized]
   22 |         ans.push_back(g[0][nj].second.second);
      |                              ^
main.cpp:14:20: note: 'nj' was declared here
   14 |         int ni = 0,nj;
      |                    ^~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
using ll = long long;

#include<set>
int n,m;
vector<vector<pair<int,pair<char,int>>>> g;
vector<char> C;
vector<int> solve1(){
if(n==2){
        int ni = 0,nj;
        for(int i = 0;i<g[0].size();i++){
            if(g[0][0].second.first==g[0][i].second.first) continue;
            nj = i;
            break;
        }
        vector<int> ans;
        ans.push_back(g[0][0].second.second);
        ans.push_back(g[0][nj].second.second);
        ans.push_back(g[0][nj].second.second);
        return ans;
        // cout<<3<<endl;
        // cout<<g[0][0].second.second<<endl<<g[0][nj].second.second<<endl<<g[0][nj].second.second<<endl;
        // return 0;
    }else if(n==3){
        vector<int> ans;
        for(int i = 0;i<3;i++){
            set<int> a;
            set<char> b;
            vector<int> use;
            for(int j = 0;j<g[i].size();j++){
                if(b.find(g[i][j].second.first)!=b.end()) continue;
                if(a.find(g[i][j].first)!=a.end()) continue;
                b.insert(g[i][j].second.first);
                a.insert(g[i][j].first);
                use.push_back(j);
            }
            if(a.size()<2) continue;
            int ni = use[0];
            int nj = use[1];
            if(i==0){
                if(g[0][ni].first==2) swap(ni,nj);
                ans.push_back(g[0][ni].second.second);
                ans.push_back(g[0][ni].second.second);
                ans.push_back(g[0][nj].second.second);
                break;
            }else if(i==1){
               if(g[i][ni].first==2) swap(ni,nj);
                ans.push_back(g[0][ni].second.second);
                ans.push_back(g[0][nj].second.second);
                break;
            }else{
               if(g[i][ni].first==1) swap(ni,nj);
                ans.push_back(g[0][ni].second.second);
                ans.push_back(g[0][nj].second.second);
                ans.push_back(g[0][nj].second.second);
                break;
            }
        }
        return ans;
    }
    char no = g[n-1][0].second.first;
    int wu,ni;
    ni = -1;
    int wv;
    for(int i = 0;i<n;i++){
        set<char> now;
        for(auto itr:g[i]){
            now.insert(itr.second.first);
        }
        if(now.size()==1) continue;
        wu = i;
        break;
    }
    vector<int> vis(n,0);
    bool fn = false;
    vector<int> que;
    auto dfs = [&](auto dfs,int ni,int to) -> void {
        vis[ni]++;
        if(ni==to){
            fn = true;
            return;
        }
        for(auto&itr:g[ni]){
            int nj = itr.first;
            if(vis[nj]) continue;
            que.push_back(itr.second.second);
            dfs(dfs,nj,to);
            if(fn) return;
            que.pop_back();
        }
    };
    dfs(dfs,0,wu);
    vector<int> ans = que;
    que.clear();
    int k = wu;
    if(wu==0){
        int ni = 0;
        int nj;
        for(int j = 1;j<g[0].size();j++){
            if(g[0][j].second.first!=g[0][0].second.first){
                nj = j;
                break;
            }
        }
        ans.push_back(g[0][ni].second.second);
        ans.push_back(g[0][nj].second.second);
        ans.push_back(g[0][nj].second.second);
        k = g[0][nj].first;
    }else{
        int ni = ans.back();
        for(int i = 0;i<g[wu].size();i++){
            if(g[wu][i].second.first==C[ni]) continue;
            ans.push_back(g[wu][i].second.second);
            k = g[wu][i].first;
            break;
        }
    }
    for(int i = 0;i<n;i++) vis[i] = 0;
    int from = k;
    int to = n - 1;
    fn = false;
    dfs(dfs,from,to);
    for(int i:que) ans.push_back(i);

    string use;
    for(int i = 0;i<ans.size();i++){
        use += C[ans[i]-1];
    }
    string u = use;
    reverse(u.begin(),u.end());
    if(use==u){
        ans.push_back(g[n-1][0].second.second);
        ans.push_back(g[n-1][0].second.second);
    }
    return ans;
    cout<<ans.size()<<endl;
    for(int i = 0;i<ans.size();i++) cout<<ans[i]<<endl;
}

vector<int> solve2(){
    char no = g[n-1][0].second.first;
    int wu,ni;
    ni = -1;
    int wv;
    for(int i = 0;i<n;i++){
        for(auto itr:g[i]){
            if(itr.second.first==no) continue;
            wu = i;
            wv = itr.first;
            ni = itr.second.second;
            break;
        }
        if(ni!=-1) break;
    }

    vector<int> vis(n,0);
    bool fn = false;
    vector<int> que;
    auto dfs = [&](auto dfs,int ni,int to) -> void {
        vis[ni]++;
        if(ni==to){
            fn = true;
            return;
        }
        for(auto&itr:g[ni]){
            int nj = itr.first;
            if(vis[nj]) continue;
            que.push_back(itr.second.second);
            dfs(dfs,nj,to);
            if(fn) return;
            que.pop_back();
        }
    };
    dfs(dfs,0,wu);
    vector<int> ans = que;
    int cnt = ans.size();
    que.clear();
    for(int i = 0;i<n;i++) vis[i] = 0;
    int from = wv;
    ans.push_back(ni);
    int to = n - 1;
    cnt++;
    if(cnt%2==1) to = g[n-1][0].first;
    fn = false;
    dfs(dfs,from,to);
    for(int i:que) ans.push_back(i);
    for(int i = 0;i<cnt;i++){
        ans.push_back(g[n-1][0].second.second);
        if(to==n-1) {
            to = g[n-1][0].first;
        }else{
            to = n-1;
        }
    }
    return ans;
}
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    set<char> s;
    cin>>n>>m;
    g.resize(n);
    C.resize(m);
    for(int i = 0;i<m;i++){
        int u,v;
        char c;
        cin>>u>>v>>c;
        C[i] = c;
        u--;v--;
        s.insert(c);
        g[u].push_back({v,{c,i+1}});
        g[v].push_back({u,{c,i+1}});
    }
    if(s.size()==1){
        cout<<-1<<endl;
        return 0;
    }

    auto ans = solve1();
    {
        string now;
        for(int i:ans) now += C[i];
        string t = now;
        reverse(now.begin(),now.end());
        if(ans.size()<=2*n&&now!=t){
            cout<<ans.size()<<endl;
            for(int i:ans) cout<<i<<endl;
            return 0;
        }
    }
    ans = solve2();
   {
        string now;
        for(int i:ans) now += C[i];
        string t = now;
        reverse(now.begin(),now.end());
        if(ans.size()<=2*n&&now!=t){
            cout<<ans.size()<<endl;
            for(int i:ans) cout<<i<<endl;
            return 0;
        }
    }

    
}
0