题解
2024-06-17 13:14:57
发布于:广东
15阅读
0回复
0点赞
基础图论,有手就能做(除了Python)
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <vector>
using namespace std;
struct node{
int to;
bool tmp;
};
vector <node> v[100005];
int a[100005];
bool t[100005];
void dfs(int n){
for(int i = 0; i < v[n].size(); i++){
if(a[v[n][i].to] == -1){
a[v[n][i].to] = a[n] ^ v[n][i].tmp;
dfs(v[n][i].to);
}
}
}
int main(){
memset(a, -1, sizeof(a));
a[1] = 1;
int n, m;
cin >> n >> m;
int x, y;
bool z;
for(int i = 1; i <= m; i++){
cin >> x >> y >> z;
node n1, n2;
n1.to = y, n2.to = x, n1.tmp = n2.tmp = z;
v[x].push_back(n1), v[y].push_back(n2);
}
dfs(1);
}for(int i = 1; i <= n; i++){
cout << a[i] << ' ';
}
return 0;
}
这里空空如也
有帮助,赞一个