Codeforces 663C Graph Coloring(图染色dfs)
来源:互联网 发布:kali linux 切换xfce 编辑:程序博客网 时间:2024/05/29 08:33
题意:
给出一张图,每条边都有一个颜色(要么是红色要么是蓝色),如果翻转一个顶点,那么这个顶点相邻的边都会变色(红变蓝,蓝变红),求使得整个图变成同色的最小操作数,并且输出要翻转哪些顶点。
思路:最小操作数一定是全部变成红色和全部变成蓝色操作数的最小值,我们以全部变成红色举例:如果一条边是红色,那么这条边的两个顶点必然是:要么同时不翻转要么同时翻转,如果一条边是蓝色,那么这条边的两个顶点翻转的情况肯定是不一致的,我们不妨将翻转情况一致的放在同一个集合, 那么就会有两个集合,一个表示翻转的点一个表示不翻转的点。那么这个问题就变成图的染色问题,dfs或者bfs染色即可,因为集合可以通过翻转互补,一个连通块的最小操作数就是最小那个集合的尺寸。
代码:
#include <bits/stdc++.h>using namespace std;const int maxn = 1e5 + 5;struct Edge{ int to, col, next;}E[maxn << 1];int head[maxn];int vis[maxn], top, n, m;vector < int > g[2], st[2];int cnt=0; void add_edge(int u, int v, int col){ E[cnt].to = v; E[cnt].col = col; E[cnt].next = head[u]; head[u] = cnt++;} bool dfs(int u, int f, int col){ //f的取值为1表示翻,0表示不翻 vis[u] = f; st[f].push_back(u); for(int i = head[u]; ~i; i = E[i].next){ int v = E[i].to; if(vis[v] != -1){ if((vis[u] ^ vis[v]) != (col ^ E[i].col)) return false; continue; } if(!dfs(v, col ^ E[i].col ^ vis[u], col)) return false; } return true;} int solve(int col){ memset(vis, -1, sizeof vis); for(int i = 1; i <= n; i++){ if(vis[i] == -1){ st[0].clear(); st[1].clear(); if(!dfs(i, 0, col)){ return n + 1; } int tmp = st[0].size() < st[1].size() ? 0 : 1; g[col].insert(g[col].end(), st[tmp].begin(), st[tmp].end()); } } return g[col].size();} int main(){ int u, v; char col; scanf("%d%d", &n, &m); memset(head, -1, sizeof head); for(int i = 1; i <= m; i++){ scanf("%d %d %c", &u, &v, &col); add_edge(u, v, col == 'R' ? 1 : 0); add_edge(v, u, col == 'R' ? 1 : 0); } int sz0 = solve(0); int sz1 = solve(1); if(sz0 == n + 1 && sz1 == n + 1){ puts("-1"); return 0; } int tmp = sz0 < sz1 ? 0 : 1; printf("%d\n", g[tmp].size()); for(int i = 0; i < g[tmp].size(); i++){ printf("%d ", g[tmp][i]); }return 0;}
阅读全文
0 0
- Codeforces 663C Graph Coloring(图染色dfs)
- UVA 193 Graph Coloring 图染色 DFS 数据
- uva 193 Graph Coloring( 图染色 ) DFS+回溯
- [杂题]Codeforces 663C. Graph Coloring
- Codeforces Round #411 E.Ice cream coloring (dfs染色)
- 193 - Graph Coloring(DFS)
- CodeForces 624C Graph and String(二分图染色)
- 01染色 codeforces663C Graph Coloring
- POJ (DFS+回溯) Graph Coloring
- UVA Graph Coloring(DFS+回溯)
- Codeforces 624C:Graph and String 二分图染色
- CodeForces - 711C Coloring Trees【给树染色】
- E. Ice cream coloring (图论 染色 DFS)
- poj 1419 Graph Coloring uva 193 - Graph Coloring (图着色问题,dfs)
- uva 193 Graph Coloring(DFS)
- Codeforces Round #360 (Div. 2) -- C. NP-Hard Problem (DFS二分图染色法)
- Codeforces Round #360 (Div. 2) -- C. NP-Hard Problem (DFS二分图染色法)
- 【CodeForces 149D】 【dp+dfs好题】D. Coloring Brackets【在限制条件下括号染色问题】
- LR参数和变量
- 学习(二)(@NonNull的作用)
- mysql 索引篇
- 汽车电子零部件检测技术服务机构
- 配置Action
- Codeforces 663C Graph Coloring(图染色dfs)
- (二)redis常用数据类型 String
- nvm淘宝镜像配置与npm下载
- 设计模式之_简单工厂模式、工厂方法模式、抽象工厂模式、策略模式、策略与工厂的区别(转) 的区别(转)
- spring的注解事务与try catch 异常处理
- Annotation--深入了解Annotation
- Python编写串口测试小工具
- linux脚本统计,日志查询相关,统计时间段日志
- FDMA 和 OFDMA 的区别是什么?