Pagini recente » Cod sursa (job #3292605) | Cod sursa (job #3322514) | Atasamentele paginii Profil oniprep | Cod sursa (job #3335777) | Cod sursa (job #3315257)
// Sursa - Andrei Nicolaescu
#include <bits/stdc++.h>
#define cin ci
#define cout co
using namespace std;
ifstream cin("andrei.in");
ofstream cout("andrei.out");
short n, m, x, y, notx, noty, cmp, c;
vector<vector<short>> graf, graft;
vector<short> ord, viz, comp;
void dfs(short node)
{
viz[node] = 1;
for (auto next : graf[node])
if (!viz[next])
dfs(next);
ord.push_back(node);
}
void dfst(short node)
{
comp[node] = cmp;
viz[node] = 1;
for (auto next : graft[node])
if (!viz[next])
dfst(next);
}
int32_t main()
{
cin >> n >> m;
graf.assign(2 * n + 1, vector<short>());
graft.assign(2 * n + 1, vector<short>());
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 (short 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)
{
cmp++;
dfst(i);
}
for (short i = 1; i <= n; i++)
if (comp[i] > comp[n + i])
cout << 1 << " ";
else
cout << 0 << " ";
return 0;
}