Coloring Edges 【拓扑判环】
题目链接:https://vjudge.net/contest/330119#problem/A
题目大意:
1.给出一张有向图,给该图涂色,要求同一个环里的边不可以全部都为同一种颜色。问最少需要多少颜色,并输出各边的涂色。
解题思路:
1.多画几张图就发现,颜色种类只会是1或者2。当不存在环的时候,全部涂1。当存在环的时候,环中可以分成两种边(小节点指向大节点涂1,大节点指向小节点涂2),就会发现所有的环颜色一定不会全部相同。
2.思考过1就发现这道题只需要判断是否存在环即可。可以用拓扑判断。原理为:在拓扑的过程中,入度为0的点会入队,但由于环上各点入度不可能为0.因此无法入队。所以在拓扑结束后,还存在没有入队的点,即存在环。
#include<stdio.h>
#include<string.h>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
const int MAXM = ;
using namespace std; int n, m;
int head[MAXN], cnt, in[MAXN], out[MAXN], tot;
queue<int> Q; struct Edge
{
int from, to, next;
}edge[MAXM]; void add(int a, int b)
{
cnt ++;
edge[cnt].from = a;
edge[cnt].to = b;
edge[cnt].next = head[a];
head[a] = cnt;
} int topo()
{
for(int i = ; i <= n; i ++)
{
if(!in[i])
{
Q.push(i);
tot ++;
}
}
while(!Q.empty())
{
int temp = Q.front();
Q.pop();
for(int i = head[temp]; i != -; i = edge[i].next)
{
int to = edge[i].to;
in[to] --;
if(!in[to])
{
Q.push(to);
tot ++;
}
}
}
if(tot != n) //存在 点 没有入队
return ;
else
return ;
} int main()
{
scanf("%d%d", &n, &m);
mem(head, -);
for(int i = ; i <= m; i ++)
{
int a, b;
scanf("%d%d", &a, &b);
in[b] ++, out[a] ++;
add(a, b);
}
if(topo()) //判是否有环存在
{
printf("2\n");
int flag = ;
for(int i = ; i <= cnt; i ++)
{
int a = edge[i].from, b = edge[i].to;
if(flag)
{
if(a < b)
printf("");
else
printf("");
flag = ;
}
else
{
if(a < b)
printf("");
else
printf("");
}
}
printf("\n");
}
else
{
printf("1\n1");
for(int i = ; i < m; i ++)
printf("");
printf("\n");
}
return ;
}
Coloring Edges 【拓扑判环】的更多相关文章
- E. Andrew and Taxi(二分+拓扑判环)
题目链接:http://codeforces.com/contest/1100/problem/E 题目大意:给你n和m,n代表有n个城市,m代表有m条边,然后m行输入三个数,起点,终点,花费.,每一 ...
- hdu 4324 Triangle LOVE(拓扑判环)
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 【建图+拓扑判环】BZOJ3953: [WF2013]Self-Assembly
Description 自动化学制造(Automatic Chemical Manufacturing,简称ACM)正在对一个叫自组装(self-assembly)的过程进行实验.在这个过程中,有着天 ...
- 牛客寒假算法基础集训营4 F(二分+拓扑判环)
题目链接 题目的输出:对于每次提问,输出一行"Yes"表示大家都遵守了群规,反之输出"No". 那么输出的就是一连串的yes和no了,二分一下无环的最大提问位置 ...
- Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)
题意:https://codeforc.es/contest/1217/problem/D 给你一个有向图,要求一个循环里不能有相同颜色的边,问你最小要几种颜色染色,怎么染色? 思路: 如果没有环,那 ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- LightOJ1003---Drunk(拓扑排序判环)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
随机推荐
- php 中秒杀
控制器层 2 //秒杀 首先要判断库存 其次高并发 然后入库 3 public function goods_do() 4 { 5 $gid=input("get.gid"); 6 ...
- 基本react-native模板
import React, { Component } from 'react'; import { Text } from 'react-native'; export default class ...
- 二进制学习——Blob,ArrayBuffer、File、FileReader和FormData的区别
前言: Blob.ArrayBuffer.File.fileReader.formData这些名词总是经常看到,知道一点又好像不知道,像是同一个东西好像又不是,总是模模糊糊,最近终于下决心要弄清楚. ...
- I Hate It (HDU 1754)
Problem 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师 ...
- Ubuntu 14.04 下安装redis后运行redis-cli 报出redis Connection refused错误【已解决】
在运行redis-cli运行后爆出错误,看了网上的都没有用例如:改ip,注释bind 127.0.0.1,或者是先运行./redis-server redis.conf,都没有用 只需要: 找到red ...
- flask + nginx + uwsgi + ubuntu18.04部署python restful接口
目录 参考链接 效果展示 一.准备工作 1.1 可运行的python demo: 1.2 更新系统环境 二.创建python虚拟环境 三.设置flask应用程序 四.配置uWSGI 五.设置系统启动 ...
- 下板不动, 上板匀速平板间流动(Crank-Nicolson格式)【转载】
摘自<FLUENT流体工程仿真计算实例与分析>,程序略有修改 两个间距为1cm水平平板,如下图所示: 上板匀速平板间流动(Crank-Nicolson格式)[转载]"> 充 ...
- Flask-login 原理
1 login_required 内部原理,主要是判断当前用户是否已经授权访问,如果没被授权就调用current_app.login_manager.unauthorized() current_us ...
- 冲刺阶段——Day2
[今日进展] 完成黄金点游戏的算法与代码架构. 使用文字界面完成任务 码云链接:https://gitee.com/jxxydwt1999/20175215-java/blob/master/Gold ...
- 学习ArrayList的扩容机制
基于jdk8 1.首先我们看new ArrayList中 public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDA ...