Pagini recente » Cod sursa (job #3309010) | Cod sursa (job #2262525) | Cod sursa (job #2431525) | Cod sursa (job #208193) | Cod sursa (job #3310115)
#include <bits/stdc++.h>
#define cin ci
#define cout co
using namespace std;
ifstream cin("andrei.in");
ofstream cout("andrei.out");
int n, m, x, y, notx, noty, cmp, c;
vector<vector<int>> graf, graft;
vector<int> ord, viz, comp;
void dfs(int node)
{
viz[node] = 1;
for(auto next:graf[node])
if(!viz[next])
dfs(next);
ord.push_back(node);
}
void dfst(int node)
{
comp[node] = cmp;
viz[node] = 1;
for(auto next:graft[node])
if(!viz[next])
dfst(next);
}
int main()
{
cin >> n >> m;
graf.assign(2 * n + 1, vector<int>());
graft.assign(2 * n + 1, vector<int>());
viz.resize(2 * n + 1);
comp.resize(2 * n + 1);
while(m--)
{
cin >> x >> y >> c;
notx = n + x;
noty = n + y;
if(c == 0)
{
graf[x].push_back(noty);
graf[y].push_back(notx);
graft[noty].push_back(x);
graft[notx].push_back(y);
}
if(c == 1)
{
graf[notx].push_back(y);
graf[noty].push_back(x);
graft[y].push_back(notx);
graft[x].push_back(noty);
}
if(c == 2)
{
graf[x].push_back(y);
graf[noty].push_back(notx);
graf[notx].push_back(noty);
graf[y].push_back(x);
graft[y].push_back(x);
graft[x].push_back(y);
graft[noty].push_back(notx);
graft[notx].push_back(noty);
}
}
for(int i=1; i<=2*n; i++)
if(!viz[i])
dfs(i);
fill(viz.begin(), viz.end(), 0);
reverse(ord.begin(), ord.end());
for(auto i: ord)
if(!viz[i])
{
cmp++;
dfst(i);
}
for(int i=1; i<=n; i++)
if(comp[i] > comp[n + i])
cout << 1 << " ";
else
cout << 0 << " ";
return 0;
}