F. Graph Without Long Directed Paths

16 篇文章 1 订阅

传送门

题意:对每条边定向,使得图中不存在长度为2的路径。
分析:相当于对每个点颜色,类似于二分图染色。然后再check一下合法性即可。
c o d e : code: code:

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl "\n"
#define ll long long
#define Mp make_pair
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define Pair pair<int,int>
typedef unsigned long long ull;
#define IO ios::sync_with_stdio(false);
const int N = 1e6+10;
const int maxn = 2e5+5;

vector<int>e[maxn];
//#define int long long

int a[maxn];
map<int,map<int,int>>mp;
int ans[maxn];
bool vis[maxn];
void dfs(int x,int fa,int col)
{
    vis[x]=true;
    a[x]=col;
    for(auto y:e[x]){
        if(y==fa||vis[y]) continue;
        dfs(y,x,col==-1?1:-1);
    }
}
bool flag;
void dfs(int x,int fa)
{
    vis[x]=true;
    for(auto y:e[x])
    {
        if(y==fa) continue;
        if(a[x]==a[y]) {flag=true; return;}
        else if(a[x]==1&&a[y]==-1){
            if(mp[x][y]) ans[mp[x][y]]=0;
            else ans[mp[y][x]]=1;
        }
        else if(a[x]==-1&&a[y]==1){
            if(mp[y][x]) ans[mp[y][x]]=0;
            else ans[mp[x][y]]=1;
        }
        if(vis[y]) continue;
        dfs(y,x);
    }
}
void solve()
{
    int n,m; cin>>n>>m;
    for(int i=1;i<=m;i++){
        int u,v; cin>>u>>v;
        e[u].push_back(v);
        e[v].push_back(u);
        mp[u][v]=i;
    }
    dfs(1,0,1);
    memset(vis,false,sizeof vis);
    dfs(1,0);
    if(flag) {cout<<"NO"<<endl;return;}
    cout<<"YES"<<endl;
    for(int i=1;i<=m;i++) cout<<ans[i];
}
signed main()
{
    //    freopen("2.in","r",stdin);
    //     freopen("15.out","w",stdout);
    IO;
    //int T; cin>>T;while (T--)
    solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值