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 DEBUGfreopen("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 DEBUGprintf("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;
}

Graph Without Long Directed Paths相关推荐

  1. 有向图php,【小龙的资结演算法秘籍】(8) 有向图(directed graph)及DAG(directed acyclic graph)的详细介绍...

    哈啰~ 大家好, 之前在[小马的资结演算法秘笈](6)超好懂的图(gragh)与树(tree) 的观念介绍介绍过什幺是graph, 那什幺是directed graph呢? 其实很简单,undirec ...

  2. Codeforces Round 550 (Div.3) 题解

    目录 A.DiverseStringsA.\ Diverse\ StringsA. Diverse Strings B.ParityAlternatedDeletionsB.\ Parity\ Alt ...

  3. CodeForces - 1144F搜索+简单图论

    [题目链接]Graph Without Long Directed Paths [题目分析]题目想要讲一个无向图变成一个最长路径不超过1的有向图.假如某个边是从u到v的,那么所有和v相连的都必须是指向 ...

  4. 【CodeForces Round #550】A-F | 模拟 | 贪心 | 高精 | BFS | 二分图 | E

    今年怎么没有愚人节比赛了   CF你看看人家洛谷   唉鸭原来那边还没到愚人节呢- 愚人节比赛还是有的,在今晚 qwq [CodeForces 1144   A-F] Tags:模拟 贪心 BFS 高 ...

  5. 夯实基础项目工程之图论——Uncle Bogdan and Country Happiness,Graph Coloring,How Many Paths?,Array Differentiation

    文章目录 做题情况项目报告 Uncle Bogdan and Country Happiness Graph Coloring How Many Paths? Array Differentiatio ...

  6. 阅读Skeleton-Based Action Recognition with Directed Graph Neural Networks(CVPR2019)

      Skeleton-Based Action Recognition with Directed Graph Neural Networks是Lei Shi等人的另一篇文章,和之前的2s-AGCN以 ...

  7. Graph (discrete mathematics)

    In mathematics, and more specifically in graph theory, a graph is a structure amounting to a set of ...

  8. 解题报告:【kuangbin带你飞】专题九 连通图

    目录 A.POJ 1236 Network of Schools(有向图缩点) B.UVA 315 Network(找割点) C.UVA 796 Critical Links(桥) D.POJ 369 ...

  9. Dijkstra's algorithm (C++)

    这是我发现的难得的比较好文章,介绍Dijkstra 算法的c++程序部署. Dijkstra's algorithm is a graph algorithm that simultaneously ...

最新文章

  1. React router 的 Route 中 component 和 render 属性理解
  2. 第2节 mapreduce深入学习:4, 5
  3. BestCoder 2nd Anniversary
  4. 哲学家就餐(避免死锁)(多进程版)
  5. 介绍几个专门面向中文的命名实体识别和关系抽取工具
  6. Redis配置文件常用配置消息解说--版本5.0.9
  7. cdn需要备案吗_车子贴改色膜需要到车管所备案吗?
  8. [USACO09HOL]假期绘画Holiday Painting
  9. 数据是以什么形式存放在计算机中的?计算机与进制 (进制转换原则)
  10. 使用@AspectJ注解开发Spring AOP
  11. CSDN的私信,手机与电脑发的消息,不能同时显示?
  12. Hash冲突解决方法
  13. html首字母大写,CSS实现英文单词的首字母大写
  14. 测序是测量你的遗传信息
  15. 后缀001,002,003等的文件解压
  16. SpringCloud技术选型
  17. 思科—计算机网络课程设计—第二章静态路由概念测试
  18. 天弘基金回应在支付宝 “无故扣款”,客户可自行取消
  19. matlab 相场法,晶体相场法模拟.pdf
  20. Android简单版天气预报,显示天气预报(第二步)

热门文章

  1. hssfworkbook 设置自适应宽度_「CSS很简单」CSS 实现宽高等比自适应容器
  2. qdebug重定向_每个Qter都该知道的qDebug
  3. 标准化工作导则2020_最新版 GB/T 1.12020 标准化工作导则 第 1 部分:标准化文件的结构和起草规则标准解读...
  4. SpringMVC_3.请求映射与静态资源处理
  5. 主曲率 matlab,基于Matlab的Hertz接触参数和主曲率差函数关系的拟合
  6. java 修改ip_如何用脚本快速修改IP地址(Netsh)
  7. Linux下dislocate命令用法,在 Linux 中遨游手册页的海洋 | Linux 中国
  8. 百度是php写的,百度大秘密,百度也是PHP写的!有证据!千真万确!
  9. oracle手工收集awr报告_WHAT——什么是AWR?
  10. 十八、前端必学Bootstrap美化(上篇)