//#pragma GCC target("avx2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") #include using namespace std; using ll = long long; using pii = pair; using pll = pair; using pli = pair; #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 void inline SORT(vector &v){sort(v.begin(),v.end()); return;} template void inline REV(vector &v){reverse(v.begin(),v.end()); return;} template void inline VEC_UNIQ(vector &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;} template T inline MAX(vector &v){return *max_element(v.begin(),v.end());} template T inline MIN(vector &v){return *min_element(v.begin(),v.end());} template T inline SUM(vector &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;} template void inline DEC(vector &v){for(int i = 0; i < (int)v.size(); i++)v[i]--; return;} template void inline INC(vector &v){for(int i = 0; i < (int)v.size(); i++)v[i]++; return;} void inline TEST(void){cerr << "TEST" << endl; return;} template bool inline chmin(T &x,T y){ if(x > y){ x = y; return true; } return false; } template bool inline chmax(T &x,T y){ if(x < y){ x = y; return true; } return false; } template vector inline get_vec(int n){ vector ans(n); for(int i = 0; i < n; i++)cin >> ans[i]; return ans; } template void inline print_vec(vector &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 void inline debug_vec(vector &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> inline get_graph(int n,int m = -1,bool direct = false){ if(m == -1)m = n - 1; vector> 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 vector>> inline get_weighted_graph(int n,int m = -1,bool direct = false){ if(m == -1)m = n - 1; vector>> 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 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 root; vector sz; vector potential; vector 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 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 ococo_bfs(vector> const& g,int s){ queue que; que.push(s); int n = (int)g.size(); vector 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> g(n); ococo_potential_unionfind 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 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; }