http://codeforces.com/gym/100502/attachments

题意:有n个地点,m条边,每条边有一个边权,0代表两个顶点都染成白色,2代表两个顶点都染成黑色,1代表两个顶点可能尚未染色,但是之后必须一个染成白色一个染成黑色。问是否有可能让这个图成功染色,如果可能输出染成黑色的最少顶点数。

思路:一开始0和2的边是确定的,直接染,如果有矛盾直接false。然后利用边权为1的边建图。先考虑如果图中的某个点已经染色了,那么直接DFS染色,然后这个阶段出现黑色的点是确定的(因为必须染成这个颜色),如果出现矛盾就返回。

再考虑如果图中没有点染色,这个时候随便染一种颜色,用两个计数器a和b,代表这个阶段染成白色的点和黑色的点的数目,这个对答案的贡献为min(a, b),因为点都是不确定的,所以染成白和染成黑都是一样的,所以可以互换,然后出现矛盾就返回。

 #include <bits/stdc++.h>
using namespace std;
#define N 200010
struct Edge {
int v, nxt;
} edge[N*];
int vis[N], head[N], tot, col[N], a, b, ans; void Add(int u, int v) {
edge[tot] = (Edge) {v, head[u]}; head[u] = tot++;
edge[tot] = (Edge) {u, head[v]}; head[v] = tot++;
} bool DFS(int u, int c, int kind) {
if(kind) {
if(col[u] == ) b++; else a++;
} else {
if(col[u] == ) ans++;
}
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(col[v] == c) return false;
if(!vis[v]) { vis[v] = ; if(col[v] == ) col[v] = -c; if(!DFS(v, col[v], kind)) return false; }
}
return true;
} int main() {
int n, m;
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
bool flag = ;
for(int i = ; i < m; i++) {
int u, v, k;
scanf("%d%d%d", &u, &v, &k);
if(k == ) {
if(col[u] == - || col[v] == -) flag = ;
col[u] = col[v] = ;
} else if(k == ) {
if(col[u] == || col[v] == ) flag = ;
col[u] = col[v] = -;
} else Add(u, v);
}
for(int i = ; i <= n; i++)
if(!vis[i] && col[i]) { vis[i] = ; if(!DFS(i, col[i], )) flag = ; }
for(int i = ; i <= n; i++) {
if(vis[i]) continue;
a = ; b = ; vis[i] = ; col[i] = ;
if(!DFS(i, , )) flag = ;
ans += a > b ? b : a;
}
if(!flag) puts("impossible");
else printf("%d\n", ans);
return ;
}

Codeforces Gym100502A:Amanda Lounges(DFS染色)的更多相关文章

  1. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  2. Codeforces 781A:Andryusha and Colored Balloons(DFS染色)

    http://codeforces.com/contest/782/problem/C 题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色. 思路:比 ...

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

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

  4. cf804C(dfs染色)

    题目链接: http://codeforces.com/problemset/problem/804/C 题意: 有一颗含有 n 个顶点的树, 第 i 个顶点上有 k 个冰激凌, 每个冰激凌的种类为 ...

  5. Gym - 100502A Amanda Lounges

    Amanda Lounges Time Limit: 1000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u AMAN ...

  6. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  7. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  8. hdu 4751 Divide Groups(dfs染色 或 2-sat)

    Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration more c ...

  9. 紫书 习题8-9 UVa 1613 (dfs染色+图的性质)

    这道题一开始我没想什么直接开始染, 但是是for循环一个节点一个节点染, 然后就WA 后了看了https://www.cnblogs.com/jerryRey/p/4702323.html 发现原来还 ...

随机推荐

  1. Shell脚本入门学习笔记

    1.shell操作系统和用户之间.负责解释命令行 2./etc/shells 记录登录系统有效支持shell 3./etc/passwd 最后,有人谁可以看到用户的默认shell 4.直接输入shel ...

  2. CF 455A(Boredom-dp)

    A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. MVC基架生成的Index视图

    @model IEnumerable<MyMusicStore.Models.Album> @{     ViewBag.Title = "Index"; } < ...

  4. .Net中使用数据库(sqlite)的大体流程(简单向)

    说来数据库,各种语言各种数据库在操作上大体无异,基本都是连接数据库.操作数据库.关闭数据库连接的流程,不过Sqlite由于是单文件数据库,相比其他服务器的数据库连接更简单,只需要给定数据库文件的路径即 ...

  5. QEventLoop的全部源码也不多,混个脸熟

    /**************************************************************************** ** ** Copyright (C) 20 ...

  6. Linux下如何查看高CPU占用率线程 专题

    Java 系统性能分析 命令 1. cpu分析 top , pidstat(sysstat) pid -p PID -t 1 10 vmstat 1 CPU上下文切换.运行队列.利用率 ps Hh - ...

  7. Linux kernel version dirty

    在我们使用git来管理Linux Kernel的时候,在编译的时候会在你的kernel version加上git commit number 有时候还会出现dirty字样,字面意思是内核被污染的意思. ...

  8. ML:机器学习中常用的Octave语句

    coursera上吴恩达的机器学习课程使用Octave/Matlab实现算法,有必要知道Octave简单的语句.最重要的:在遇到不会的语句,使用'''help '''或者'''doc '''查看官方文 ...

  9. Codility--- NumberOfDiscIntersections

    Task description We draw N discs on a plane. The discs are numbered from 0 to N − 1. A zero-indexed ...

  10. 【工具】Axure 8.0 序列号

    之前用的 Axure 8.0 到期最近了,重找了一个序列号,发现可用,记录一下,分享如下: 授权人:University of Science and Technology of China (CLA ...