//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int c,n,m,mk[3000];
vi v[MAX];
void dfs(int x){
mk[x]=1;
c++;
int i;
f(i,0,v[x].size()){
if(!mk[v[x][i]])
dfs(v[x][i]);
}
}
int main(){
int r,k,i,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p;
while(1){
sii(n,m);
if(n==0 && m==0)
break;
f(i,0,m){
siii(x,y,z);
if(z==1)
v[x].pb(y);
else{
v[x].pb(y);
v[y].pb(x);
}
}
z=0;
f(i,1,n+1){
clr(mk,0);
c=0;
dfs(i);
if(c!=n){
z=1;
break;
}
}
f(i,1,n+5)
v[i].clear();
if(z)
pi(0);
else
pi(1);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int c,n,m,mk[3000];
vi v[MAX];
void dfs(int x){
mk[x]=1;
c++;
int i;
f(i,0,v[x].size()){
if(!mk[v[x][i]])
dfs(v[x][i]);
}
}
int main(){
int r,k,i,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p;
while(1){
sii(n,m);
if(n==0 && m==0)
break;
f(i,0,m){
siii(x,y,z);
if(z==1)
v[x].pb(y);
else{
v[x].pb(y);
v[y].pb(x);
}
}
z=0;
f(i,1,n+1){
clr(mk,0);
c=0;
dfs(i);
if(c!=n){
z=1;
break;
}
}
f(i,1,n+5)
v[i].clear();
if(z)
pi(0);
else
pi(1);
}
return 0;
}
No comments:
Post a Comment