Pagini recente » HLO 2023 - Cls 11-12 - Tema 0 | Cod sursa (job #2208946) | Cod sursa (job #2299864) | Profil RolandPetrean | Cod sursa (job #1168849)
#include <cstring>
#include <algorithm>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
const int MAX_N = 100005;
int vertexes, color[MAX_N * 2];
vector<int> graph[MAX_N * 2], trans[MAX_N * 2];
stack<int> order;
bool vis[MAX_N * 2];
inline int n(int v) {
return 2 * vertexes - v + 1;
}
void dfs_plus(int node) {
vis[node] = true;
for(vector<int> :: iterator it = graph[node].begin(); it != graph[node].end(); ++ it) {
if(!vis[*it]) {
dfs_plus(*it);
}
}
order.push(node);
}
void dfs_minus(int node) {
vis[node] = true;
color[node] = 1;
for(vector<int> :: iterator it = trans[node].begin(); it != trans[node].end(); ++ it) {
if(!vis[*it]) {
dfs_minus(*it);
}
}
}
int main() {
ifstream fin("andrei.in");
ofstream fout("andrei.out");
int edges;
fin >> vertexes >> edges;
for(int i = 1; i <= edges; ++ i) {
int a, b, c;
fin >> a >> b >> c;
if(c == 0) {
graph[n(a)].push_back(b);
trans[b].push_back(n(a));
graph[n(b)].push_back(a);
trans[a].push_back(n(b));
} else if(c == 1) {
graph[a].push_back(n(b));
trans[n(b)].push_back(a);
graph[b].push_back(n(a));
trans[n(a)].push_back(b);
} else {
graph[a].push_back(b);
graph[b].push_back(a);
trans[a].push_back(b);
trans[b].push_back(a);
graph[n(a)].push_back(n(b));
graph[n(b)].push_back(n(a));
trans[n(a)].push_back(n(b));
trans[n(b)].push_back(n(a));
}
}
for(int i = 1; i <= 2 * vertexes; ++ i) {
if(!vis[i]) {
dfs_plus(i);
}
}
memset(vis, 0, sizeof vis);
while(!order.empty()) {
if(!vis[order.top()] && !vis[n(order.top())]) {
dfs_minus(order.top());
}
order.pop();
}
for(int i = 1; i <= vertexes; ++ i) {
fout << color[i] << (i == vertexes ? '\n' : ' ');
}
return 0;
}