個人博客鏈接:https://blog.nuoyanli.com/2020/04/22/cf1144f/
2020NYIST個人積分賽第九場F題
原題鏈接
https://codeforces.com/problemset/problem/1144/F
題意
給你一個無向圖,讓你把所有邊標記方向,並使其沒有距離的邊,問你是否存在並輸出方案。
思路
距離大於等於2,可不就是分成兩個點集, 和 ,使得只存在從 到 B的邊,不存在回來的邊,顯然就是二分圖(Orz),還是圖論做少了,就很的二分圖染色。
枚舉點,染色黑白點之間隨便定一個方向,可以黑到白也可以白到黑。
即可。
參考代碼
// nuoyanli
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <iostream>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define kB kush_back
#define FI first
#define SE second
//#define in
#define RI register
#define m_k(a, b) make_pair(a, b)
#define PB(a) push_back(a)
#define debug(a) cout << "---" << a << "---" << endl
#define debug_(a, b) cout << a << "---" << b << endl
#define bl(x) setiosflags(ios::fixed) << setprecision(x)
const double pi = acos(-1.0);
const double eps = 1e-9;
typedef long long LL;
const int N = 1e6 + 10;
const int M = 1e3 + 10;
const LL mod = 1e9 + 7;
const LL inf = 1e18;
const double f = 0.32349;
vector <int> G[N];
int n, m, col[N];
bool vs = 0;
void dfs(int u, int c) {
col[u] = c;
for (auto v:G[u]) {
if (col[v] == -1) {//沒有染過色
dfs(v, c ^ 1);//染上與現在不同的顏色
} else if (col[v] == c) { //有一個點已經染色且與當前點顏色相同
vs =1;
return;
}
}
}
vector <pair <int, int> >Edges;
int main() {
IOS;
cin >> n >> m;
for(int i=0; i<m; i++) {
int u, v;
cin >> u >> v;
--u, --v;//從0開始編號
G[u].PB(v);
G[v].PB(u);
Edges.PB(m_k(u, v));
}
memset(col, -1, sizeof(col));//-1表示沒有染過色
dfs(0, 0);
if(vs) {
cout<<"NO"<<endl;
} else {
cout << "YES" << endl;
for (auto i:Edges) {
if (col[i.FI] == 0)cout << 1;
else cout << 0;
}
}
return 0;
}