Pagini recente » Cod sursa (job #1473856) | Cod sursa (job #777716) | Profil coto2464 | Cod sursa (job #2261514) | Cod sursa (job #3217745)
#include <fstream>
#include <vector>
#define NMAX 100005
using namespace std;
ifstream fin("andrei.in");
ofstream fout("andrei.out");
int n, m;
int a, b, c;
vector<int> l[NMAX*2];
vector<int> lt[NMAX*2];
vector<int> postord;
int cc[NMAX*2]; int nr;
bool f[NMAX*2];
int i;
void dfs(int);
void dfst(int);
int main()
{
fin >>n>>m;
for (i = 1; i <= m; ++i)
{
fin >>a>>b>>c;
if (c == 0)
{
l[a+n].push_back(-b+n);
l[b+n].push_back(-a+n);
lt[-b+n].push_back(a+n);
lt[-a+n].push_back(b+n);
continue;
}
if (c == 1)
{
l[-a+n].push_back(b+n);
l[-b+n].push_back(a+n);
lt[b+n].push_back(-a+n);
lt[a+n].push_back(-b+n);
continue;
}
l[a+n].push_back(b+n);
l[-b+n].push_back(-a+n);
lt[b+n].push_back(a+n);
lt[-a+n].push_back(-b+n);
}
for (i = 0; i <= 2*n; ++i)
if (!f[i])
dfs(i);
for (i = postord.size()-1; i >= 0; --i)
if (f[postord[i]])
{
nr ++;
dfst(postord[i]);
}
for (i = 1; i <= n; ++i)
fout <<(cc[i+n] > cc[-i+n])<<' ';
fout <<'\n';
return 0;
}
void dfst(int vf)
{
f[vf] = 0; cc[vf] = nr;
for (auto vfnou: lt[vf])
if (f[vfnou])
dfst(vfnou);
}
void dfs(int vf)
{
f[vf] = 1;
for (auto vfnou: l[vf])
if (!f[vfnou])
dfs(vfnou);
postord.push_back(vf);
}