题意:

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

思路:

对于每个点,要么出度为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. Linux-线程同步之互斥锁

    1.互斥锁又叫互斥量(mutex) 2.相关函数:pthread_mutex_init  pthread_mutex_destroy   pthread_mutex_lock pthread_mute ...

  2. spring容器抽象的具体实现

    1.BeanFactory 接口与 ApplicationContext 接口 (1)spring 提供了两种类型的IOC容器实现.BeanFactory 和 ApplicationContext ( ...

  3. 微信小程序官方示例 官方weui-wxss下载于安装 详解

    1.小程序示例源码:https://github.com/wechat-miniprogram/miniprogram-demo 2.微信 weui下载地址:https://github.com/we ...

  4. 104. HttpRequest对象详解

    WSGIRequest 对象常用的属性和方法: WSGIRequest对象常用的属性: WSGIRequest对象大部分属性都是只读的,因为这些属性是从客户端上传上来的,没必要做任何的修改.以下对一些 ...

  5. PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]

    题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, ...

  6. Python语言学习:homework2

    三级菜单 # Author:Crystal data = { '北京':{ "昌平":{ "沙河":["oldbay","test ...

  7. py02_02:pyc的解释

    1. Python是一门解释型语言吗? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存在.如果是解释型语言 ...

  8. Mysql 环境部署

    1.Window 1.1 下载软件: https://dev.mysql.com/downloads/mysql/ 依次点击上图 红色框中按钮 1.2 安装软件 1.2.1 解压软件 正常解压即可  ...

  9. Chrome使用频率最高的快捷键

    标签 ctrl+T 打开新标签  ——— ctrl+W 关闭标签 ctrl+shift+T 打开上衣个被关闭的标签 ctrl+tab 标签向右切换 —— ctrl+shift+tab 标签向左切换 c ...

  10. Python笔记_第三篇_面向对象_9.Python中的"get"和"set"方法(@property和@.setter)

    1. 限制访问的问题: 如果学过C# 语言的我们可以知道C# 语言有get和set方法.我们之前想要获取父类中的私有变量,只能通过写一个set和get的函数进行访问或者通过类生成的新关键字来访问私有属 ...