結果

問題 No.3482 Quod Erat Demonstrandum
コンテスト
ユーザー ococonomy1
提出日時 2026-03-27 21:26:34
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 7,517 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,356 ms
コンパイル使用メモリ 223,676 KB
実行使用メモリ 19,972 KB
最終ジャッジ日時 2026-03-27 21:26:51
合計ジャッジ時間 7,845 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 35 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pli = pair<ll,int>;
#define MOD 998244353
//#define MOD 1000000007
#define el '\n'
#define El '\n'
#define YESNO(x) ((x) ? "Yes" : "No")
#define YES YESNO(true)
#define NO YESNO(false)
#define EXIT_ANS(x) {cout << (x) << '\n'; return;}
#define PA() {EXIT_ANS(ans);}
template <typename T> void inline SORT(vector<T> &v){sort(v.begin(),v.end()); return;}
template <typename T> void inline REV(vector<T> &v){reverse(v.begin(),v.end()); return;}
template <typename T> void inline VEC_UNIQ(vector<T> &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;}
template <typename T> T inline MAX(vector<T> &v){return *max_element(v.begin(),v.end());}
template <typename T> T inline MIN(vector<T> &v){return *min_element(v.begin(),v.end());}
template <typename T> T inline SUM(vector<T> &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;}
template <typename T> void inline DEC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]--; return;}
template <typename T> void inline INC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]++; return;}
void inline TEST(void){cerr << "TEST" << endl; return;}
template <typename T> bool inline chmin(T &x,T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}
template <typename T> bool inline chmax(T &x,T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template <typename T = long long> vector<T> inline get_vec(int n){
    vector<T> ans(n);
    for(int i = 0; i < n; i++)cin >> ans[i];
    return ans;
}
template <typename T> void inline print_vec(vector<T> &vec,bool kaigyou = false){
    int n = (int)vec.size();
    for(int i = 0; i < n; i++){
        cout << vec[i];
        if(kaigyou || i == n - 1)cout << '\n';
        else cout << ' ';
    }
    if(!n)cout << '\n';
    return;
}
template <typename T> void inline debug_vec(vector<T> &vec,bool kaigyou = false){
    int n = (int)vec.size();
    for(int i = 0; i < n; i++){
        cerr << vec[i];
        if(kaigyou || i == n - 1)cerr << '\n';
        else cerr << ' ';
    }
    if(!n)cerr << '\n';
    return;
}
vector<vector<int>> inline get_graph(int n,int m = -1,bool direct = false){
    if(m == -1)m = n - 1;
    vector<vector<int>> g(n);
    while(m--){
        int u,v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        if(!direct)g[v].push_back(u);
    }
    return g;
}
template <typename T> vector<vector<pair<T,int>>> inline get_weighted_graph(int n,int m = -1,bool direct = false){
    if(m == -1)m = n - 1;
    vector<vector<pair<T,int>>> g(n);
    while(m--){
        int u,v;
        cin >> u >> v;
        u--; v--;
        ll w; cin >> w;
        g[u].push_back(pair(w,v));
        if(!direct)g[v].push_back(pair(w,u));
    }
    return g;
}

// 重み付きUnionFind
//{ポテンシャルの型、ノードに乗せる型(普通の UF にもある方)}
//ポテンシャルの演算が可換でない時未 verify。というか普通に壊れてる。
template <typename T,typename U>
class ococo_potential_unionfind {
private:
    //単位元
    T e = 0;
    //ポテンシャルのマージ
    T func(T l,T r){
        return (l + r);
    }
    //ポテンシャルの逆元
    T pot_g(T x){
        return -x;
    }

    //ノードに乗せる値のマージ
    U func_val(U l,U r){
        return (l + r);
    }
    //主にここより上をいじる

    vector<int> root;
    vector<int> sz;
    vector<T> potential;
    vector<U> val;
    int com_cnt;
public:

    ococo_potential_unionfind(int n = 0){
        root.resize(n);
        sz.resize(n,1);
        potential.resize(n,e);
        val.resize(n);
        for(int i = 0; i < n; i++)root[i] = i;
        com_cnt = n;
    }

    ococo_potential_unionfind(vector<U> v){
        int n = (int)v.size();
        root.resize(n);
        sz.resize(n,1);
        potential.resize(n,e);
        val = v;
        for(int i = 0; i < n; i++)root[i] = i;
        com_cnt = n;
    }

    int ne(int a){
        if(root[a] == a)return a;
        int par = root[a];
        int r = ne(par);
        potential[a] = func(potential[a],potential[par]);
        return root[a] = r;
    }
    //a -> b に行くとき、コスト W がかかる
    //既に連結ならば false を返す
    bool einsert(int a,int b,T w){
        int a1 = ne(a),a2 = ne(b);
        if(a1 == a2)return false;
        com_cnt--;
        val[a] = val[b] = func_val(val[a],val[b]);
        if(sz[a1] < sz[a2]){
            root[a1] = a2;
            sz[a2] = max(sz[a1] + 1,sz[a2]);
            potential[a1] = func(potential[a1],pot_g(potential[a]));
            potential[a1] = func(potential[a1],potential[b]);
            potential[a1] = func(w,potential[a1]);
        }
        else{
            root[a2] = a1;
            sz[a1] = max(sz[a2] + 1,sz[a1]);
            potential[a2] = func(potential[a2],pot_g(potential[b]));
            potential[a2] = func(potential[a2],potential[a]);
            potential[a2] = func(pot_g(w),potential[a2]);
        }
        return true;
    }

    bool is_connect(int a,int b){
        return (ne(a) == ne(b));
    }
    //a -> b にかかるコスト。連結でない場合は未定義なので is_connect であらかじめ確かめるべき
    T get_potential_dif(int a,int b){
        ne(a); ne(b);
        return func(pot_g(potential[a]),potential[b]);
    }

    U get_val(int x){
        return val[ne(x)];
    }
    // 何個の島に分かれているか出力する関数 O(1)
    int component_count(void) {
        return com_cnt;
    }
};
vector<int> ococo_bfs(vector<vector<int>> const& g,int s){
    queue<int> que;
    que.push(s);
    int n = (int)g.size();
    vector<int> dist(n,-1); dist[s] = 0;
    while(!que.empty()){
        int fr = que.front(); que.pop();
        for(int i = 0; i < (int)g[fr].size(); i++){
            int to = g[fr][i];
            if(dist[to] != -1)continue;
            que.push(to);
            dist[to] = dist[fr] + 1;
        }
    }
    return dist;
}

#define MULTI_TEST_CASE true
void solve(void){
    //問題を見たらまず「この問題設定から言えること」をいっぱい言う
    //よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く
    //複数の解法のアイデアを思いついた時は全部メモしておく
    //g++ -D_GLIBCXX_DEBUG -Wall -O2 b.cpp -o o
    ll ans = 0;
    int n;
    cin >> n;
    int m; cin >> m;
    vector<vector<int>> g(n);
    ococo_potential_unionfind<int,int> uf(n);
    while(m--){
        int u,v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
        int c; cin >> c;
        c--;
        uf.einsert(u,v,c);
    }
    vector<int> dist = ococo_bfs(g,0);
    if(dist[n - 1] == -1)EXIT_ANS("Unknown");
    //cerr << uf.get_potential_dif(0,n - 1) << el;
    if(uf.get_potential_dif(0,n - 1) % 2 == 0)cout << "Same" << el;
    else cout << "Different" << el;
    cout << dist[n - 1] << el;


    return;
}

void calc(void){
    return;
}

signed main(void){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if(MULTI_TEST_CASE)cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
0