//#define _GLIBCXX_DEBUG #include using namespace std; #define endl '\n' #define lfs cout<= (ll)(n); i--) using ll = long long; using ld = long double; const ll MOD1 = 1e9+7; const ll MOD9 = 998244353; const ll INF = 1e18; using P = pair; template bool chmin(T &a,T b){if(a>b){a=b;return true;}else return false;} template bool chmax(T &a,T b){if(a void ans(bool x,T1 y,T2 z){if(x)cout< void debug(vector>&v,ll h,ll w){for(ll i=0;i&v,ll h,ll w){for(ll i=0;i void debug(vector&v,ll n){if(n!=0)cout< vector>vec(ll x, ll y, T w){ vector>v(x,vector(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vectordx={1,0,-1,0,1,1,-1,-1}; vectordy={0,1,0,-1,1,-1,1,-1}; template vector make_v(size_t a,T b){return vector(a,b);} template auto make_v(size_t a,Ts... ts){ return vector(a,make_v(ts...)); } template ostream &operator<<(ostream &os, const pair&p){ return os << p.first << " " << p.second; } ll sz=100005; vectort(sz,-1); bool judge=false; void dfs(ll k,vector>&g,ll par,ll group){ t[k]=group; for(auto to:g[k]){ if(to != par&&t[to]==-1){ dfs(to,g,k,group); } else if(to!=par){ judge=true; } } } vectortsort(vector>&g){ ll n = g.size(); vectorret(n),ret2(n); vectorv(n); for(ll i=0;ist; for(ll i=0;i();//閉路がある ll x=st.top(); st.pop(); ret[x]=i; ret2[i]=x; for(ll j=0;j>n>>m; vector>g1(n); vectora(m),b(m),c(m); rep(i,0,m){ cin>>a[i]>>b[i]>>c[i];a[i]--;b[i]--; if(c[i]==1){ g1[a[i]].PB(b[i]); g1[b[i]].PB(a[i]); } } ll num=0; rep(i,0,n){ if(t[i]==-1){ dfs(i,g1,-1,num); num++; //debug(t,n); //cout<>g2(num); //debug(t,n); rep(i,0,m){ if(c[i]==2){ g2[t[a[i]]].PB(t[b[i]]); if(t[a[i]]==t[b[i]])judge=true; } } auto v=tsort(g2); if(v.empty())judge=true; ans1(judge); return 0; }