题意:

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

思路:

对于每个点,要么出度为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. 跟踪LinkedList源码,通过分析双向链表实现原理,自定义一个双向链表

    1.LinkedList实现的基本原理 LinkedList是一个双向链表,它主要有两个表示头尾节点的成员变量first  .last,因其有头尾两个节点,所以从头或从尾操作数据都非常容易快捷.Lin ...

  2. VUE,index key v-for

    列表渲染语法  v-forv-for 循环对象 <article v-for="(item, key, index) of info">{{item}} {{key}} ...

  3. sudo: /etc/sudoers is mode 0777, should be 0440 单用户 sudo不用输入密码的方法

    sudo权限问题考察一题  su -   sudo su -    sudo su - test [root@localhost ~]# su - zzx   #root用户进行切换不需要输入密码 [ ...

  4. openlayers基础用例

    http://weilin.me/ol3-primer/ch03/03-01.html#http://weilin.me/ol3-primer/ //地址http://openlayers.org/ ...

  5. Anaconda 添加清华源与恢复默认源

    1.添加清华源 查看清华大学官方镜像源文档:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ 本文基于Windows平台,首先用命令: conda ...

  6. TCP/IP通信过程

    一.参考网址 1.以太网帧格式.IP数据报格式.TCP段格式+UDP段格式 详解 2. 二.TCP的建立过程 1.例子: 192.168.22.66 telenet到192.168.22.74的tcp ...

  7. Creo 2.0 Toolkit 解锁的问题

    近期开发Creo Toolkit遇到一个问题,在自己本机开发完成后运行并无问题,但是如果拿去给别人的机子运行会报出 提示“creo ToolKit应用程序在分配到您的地址之前未被解锁”,在与PTC 技 ...

  8. tensorflow函数解析:Session.run和Tensor.eval

    原问题链接: http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-sess ...

  9. Struts 2 的常规配置

    Struts 2 的默认配置文件是struts.xml,该文件应该放在Web应用的类加载路径下,通常就是放在WEB-INF/classes路径下. struts.xml文件的最大作用是配置Action ...

  10. java生成6位数所有组合

    for(int i=0;i<=9;i++){ String str=""; str=str+i; String strj=""; for(int j=0; ...