// https://github.com/potato167/po167_library #include #include using namespace std; using ll=long long; const ll ILL=2167167167167167167; const int INF=2100000000; #define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++) #define all(p) p.begin(),p.end() template using pq_ = priority_queue, greater>; template int LB(vector &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();} template int UB(vector &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();} template bool chmin(T &a,T b){if(b bool chmax(T &a,T b){if(a void So(vector &v) {sort(v.begin(),v.end());} template void Sore(vector &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});} bool yneos(bool a,bool upp=false){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;} template void vec_out(vector &p,int ty=0){ if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'< T vec_min(vector &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;} template T vec_max(vector &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;} template T vec_sum(vector &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;} int pop_count(long long a){int res=0;while(a){res+=(int)(a&1),a>>=1;}return res;} template T square(T a){return a * a;} namespace po167{ struct SCC{ std::vector> G; std::vector> G_rev; int size; SCC(int n):G(n),G_rev(n),size(n){} void clear(){ for(auto &x:G) x.clear(); for(auto &x:G_rev) x.clear(); } void reserve(int ind,size_t n_){ assert(ind &order,std::vector &seen,int ind,int &now){ if(seen[ind]!=0) return; seen[ind]=-1; for(auto x:G[ind]){ dfs1(order,seen,x,now); } order[now]=ind; now++; } void dfs2(std::vector &seen,int ind,int rank){ if(seen[ind]!=-1) return; seen[ind]=rank; for(auto x:G_rev[ind]){ dfs2(seen,x,rank); } } std::vector> tp_sort(){ std::vector seen(size,0),order(size); int now=0; for(int i=0;i=0;i--){ if(seen[order[i]]==-1){ dfs2(seen,order[i],now); now++; } } std::vector> ans(now); for(int i=0;i for_two_sat(){ std::vector seen(size,0),order(size); int now=0; for(int i=0;i=0;i--){ if(seen[order[i]]==-1){ dfs2(seen,order[i],now); now++; } } return seen; } }; struct two_SAT{ po167::SCC graph; std::vector ans; bool exists=false; int size; two_SAT(int n):graph(2*n),ans(n),size(n){} void add_clause(int i,bool f,int j,bool g){ graph.add_edge(i*2+(!f),j*2+(g)); graph.add_edge(j*2+(!g),i*2+(f)); } void clear(){ graph.clear(); exists=false; } void reserve(int ind,size_t n_){ assert(ind> &p){ int ind; for(int i=0;i<(int)(p.size())-1;i++){ ind=graph.add_ver(); graph.add_ver(); ind/=2; add_clause(ind,1,p[i].first,p[i].second^1); add_clause(ind,0,p[i+1].first,p[i+1].second^1); if(i){ add_clause(ind-1,0,ind,1); } ans.push_back(false); size++; } } bool build(){ auto rank=graph.for_two_sat(); exists=true; for(int i=0;i> t; rep(i, 0, t) solve(); } void solve(){ int N, M; cin >> N >> M; map, int> m; rep(i, 0, M) { int a, b, c; cin >> a >> b >> c; a--, b--; m[{a, b + N}] += (1 << c); m[{a + N, b}] += (1 << (1 - c)); } po167::SCC S(N * 2); for (auto e : m) { S.add_edge(e.first.first, e.first.second); } auto tp = S.tp_sort(); vector dpL(N * 2, INF), dpR(N * 2, -INF); dpL[0] = 0, dpR[0] = 0, dpL[N] = 0, dpR[N] = 0; vector pot(N * 2); vector seen(N * 2); vector rank(N * 2); rep(i, 0, tp.size()) for (auto x : tp[i]) rank[x] = i; for (auto g : tp) { // vec_out(g); int ok = 1; vector order = {g[0]}; seen[g[0]] = 1; rep(rp, 0, order.size()) { int a = order[rp]; for (auto x : S.G[a]) if (rank[x] == rank[a]){ int w = m[{a, x}]; // cout << "# " << a << " " << x << " " << w << endl; if (w == 3) ok = 0; if (seen[x] == 0) { seen[x] = 1; pot[x] = pot[a] + (w == 1 ? -1 : 1); order.push_back(x); } if (pot[x] != pot[a] + (w == 1 ? -1 : 1)) { ok = 0; } } } if (ok == 0) { for (auto x : g) dpL[x] = -INF, dpR[x] = INF; } else { int L = INF, R = -INF; for (auto x : g) { chmin(L, dpL[x] - pot[x]); chmax(R, dpR[x] - pot[x]); } for (auto x : g) { dpL[x] = L + pot[x]; dpR[x] = R + pot[x]; } } for (auto x : g) for (auto y : S.G[x]) if (rank[x] != rank[y]) { if (m[{x, y}] & 1) { chmin(dpL[y], dpL[x] - 1); chmax(dpR[y], dpR[x] - 1); } if (m[{x, y}] & 2) { chmin(dpL[y], dpL[x] + 1); chmax(dpR[y], dpR[x] + 1); } } } rep(i, 0, N) { int ans = max(dpR[i], dpR[i + N]); chmax(ans, -min(dpL[i], dpL[i + N])); if (ans > N * 2) { cout << "-1\n"; } else { cout << ans << "\n"; } } } /* t 回目の操作だとして、 * (c + t) & 1 ならば 1 * そうでないなら -1 として、 * 総和の絶対値の最大化になる * 偶数閉路であって、これが 0 にならないものが存在するならば最大値がドーン * まずはそれの検知が必要だけど簡単なのか? * 頂点を倍化する * そのあとポテンシャルをつけることで、全てのアレをあれしたいが * それができないから総和が非負な偶数閉路が存在するわけで * DFS をすればとりあえず検知はできる * そうしたらとりあえず * DAG でええか * */