https://codeforces.com/contest/1144/problem/F

题意:给定一个无向图,要求添加方向使得图变为有向图并且不存在长度大于1的路径

C++版本一

题解:二分匹配

对于一个点的所有有关系的边中只能有指向它的或者背向它的;

所有二分匹配,标记这个点是什么情况。然后枚举边的出发点,输出出发点的情况就行了

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=200000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int color[N];
int a[N];
char str;
vector<int>G[N];
void  dfs(int u,int c){
    color[u]=c;
    for(int i=0,j=G[u].size();i<j;i++){
        int v=G[u][i];
        if(color[u]==color[v]){
            flag=0;
        }
        if(color[v]==-1)
           dfs(v,!c);
    }
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
        a[i]=u;
    }
    memset(color,-1,sizeof(color));
    flag=1;
    for(int i=1;i<=n;i++){
        if(color[i]==-1)
            dfs(i,0);
    }
    if(flag){
        cout<<"YES"<<endl;
        for(int i=1;i<=m;i++){
            cout<<!color[a[i]];
        }
    }else{
        cout<<"No"<<endl;
    }
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

#include<bits/stdc++.h>
#define fi first
#define sf scanf
#define se second
#define pf printf
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(x,y) memset((x),(y),sizeof(x))
#define fup(i,x,y) for(int i=(x);i<=(y);++i)
#define fdn(i,x,y) for(int i=(x);i>=(y);--i)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
using namespace std;

const int __=2e5+5;

struct node
{
    int x,c,id;
};

vector<node>G[__];
int col[__],ans[__];

void dfs(int x,int fa=-1,int c=0)
{
    if(col[x])
    {
        if(col[x]!=c)
        {
            puts("NO");
            exit(0);
        }
        return;
    }
    col[x]=c;
    for(int i=0;i<sz(G[x]);++i)
    {
        node y=G[x][i];
        if(y.x!=fa)
        {
            ans[y.id]=y.c^c;
            dfs(y.x,x,1-c);
        }
    }
}

int main()
{
    int n,m;sf("%d%d",&n,&m);
    fup(i,1,m)
    {
        int x,y;sf("%d%d",&x,&y);
        G[x].pb({y,1,i});
        G[y].pb({x,0,i});
    }
    dfs(1);
    puts("YES");
    fup(i,1,m)
        pf("%d",ans[i]);
    return 0;
}