题意:

给出一个无向联通图,要求你给出每条边的方向,使得无论从哪个点出发最多只能走一条边;

思路:

对于每个点,要么出度为0,要么入度为0即可。所以这就是一个判断二分图。

  • 二分图

    #include "cstdio"
    #include "cstring"
    #include "iostream"
    #include "vector"
    using namespace std;
    typedef pair<int, int> PII;
    const int MAXN = 2e5 + ;
    int n, m;
    PII edge[MAXN];
    // v[i]记录和i点相连的点
    vector<int> v[MAXN];
    // 记录每个点的状态,-1表示未访问,0表示只有出度,1表示只有入度
    int arr[MAXN];
    bool ok = true;
    void dfs(int n, int k) {
    if (arr[n] != -) {
    if (arr[n] != k) {
    ok = false;
    }
    return;
    }
    arr[n] = k;
    for (int i = ; i < v[n].size(); i++) {
    dfs(v[n][i], !k);
    }
    }
    int main() {
    int a, b;
    memset(arr, -, sizeof(arr));
    scanf("%d%d", &n, &m);
    for (int i = ; i <= m; i++) {
    scanf("%d%d", &a, &b);
    // 存边,但是其实后面不用b了,存下a点就够了
    edge[i] = make_pair(a, b);
    v[a].push_back(b);
    v[b].push_back(a);
    }
    dfs(, );
    if (ok) {
    puts("YES");
    for (int i = ; i <= m; i++) {
    printf("%d", arr[edge[i].first]);
    }
    puts("");
    } else {
    puts("NO");
    }
    return ;
    }

CF-1144F-Graph Without Long Directed Paths的更多相关文章

  1. Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)

    <题目链接> 题目大意:给定一个无向图,该无向图不含自环,且无重边.现在要你将这个无向图定向,使得不存在任何一条路径长度大于等于2.然后根输入边的顺序,输出构造的有向图.如果构造的边与输入 ...

  2. Codeforces 1144F Graph Without Long Directed Paths DFS染色

    题意: 输入一张有向图,无自回路和重边,判断能否将它变为有向图,使得图中任意一条路径长度都小于2. 如果可以,按照输入的边的顺序输出构造的每条边的方向,构造的边与输入的方向一致就输出1,否则输出0. ...

  3. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths

            F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 ...

  4. F. Graph Without Long Directed Paths Codeforces Round #550 (Div. 3)

    F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 megabyt ...

  5. Graph Without Long Directed Paths CodeForces - 1144F (dfs染色)

    You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self ...

  6. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)

    题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...

  7. CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))

    C. Graph Reconstruction time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

  8. CF 990D Graph And Its Complement 第十八 构造、思维

    Graph And Its Complement time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. E CF R 85 div2 1334E. Divisor Paths

    LINK:Divisor Paths 考试的时候已经想到结论了 可是质因数分解想法错了 导致自闭. 一张图 一共有D个节点 每个节点x会向y连边 当且仅当y|x,x/y是一个质数. 设f(d)表示d的 ...

随机推荐

  1. 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A题

    A - ^&^ Bit operation is a common computing method in computer science ,Now we have two positive ...

  2. Python7DAY-基础语法.md

    # python简介 python交互器的作用:主要是是为了调试代码.

  3. C# 基础Array

    一.Array的作用 连续定义多个相同类型的变量,比如我定义1000个学生的学生年龄,int[] age = new int[1000];不需要慢慢的一个一个变量的定义,数组是不是很方便. 需要注意的 ...

  4. 黑马_10 Lucene:全文检索

    10 Lucene:01.全文检索基本介绍 10 Lucene:02.创建索引库和查询索引 10 Lucene:03.中文分析器 10 Lucene:04.索引库维护CURD

  5. 基于Flask框架搭建视频网站的学习日志(六)之数据库

    使用Flask-SQLSlchemy管理数据库(1)--初步安装调试 一.介绍: Flask-SQLSlchemy是一个Flask扩展,简化了Flask中对sql的操作,是一个高层的框架,可以避免直接 ...

  6. 非线性支持向量机SVM

    非线性支持向量机SVM 对于线性不可分的数据集, 我们引入了核(参考:核方法·核技巧·核函数) 线性支持向量机的算法如下: 将线性支持向量机转换成非线性支持向量机只需要将变为核函数即可: 非线性支持向 ...

  7. 使用iTextSharp來合併PDF檔

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. 5.windows-oracle实战第五课 --事务、函数

    什么是事务        事务用于保证数据的一致性,它由一组相关的dml语句组成,该组的dml语句要么全部成功,要么全部失败. 事务和锁        当执行一个事务dml的时候,oracle会被作用 ...

  9. springboot的linux-docker部署

    将springboot jar应用打包成镜像并在docker运行成容器 https://blog.csdn.net/keepd/article/details/80569797 Docker安装(De ...

  10. 回顾vim,ftp

    常用服务器 ftp,ssh FTP是文件传输协议的简称,文传协议,用于internet上控制文件的双向传输 它也是一个应用程序,,基于不同的操作系统有不同的FTP应用程序,都遵循同一种协议以传输文件. ...