CROC 2016
来源:互联网 发布:台湾传奇网络 编辑:程序博客网 时间:2024/05/29 09:45
You are given an undirected graph that consists of n vertices and m edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, while all blue edges with an endpoint in this vertex change the color to red.
Find the minimum possible number of moves required to make the colors of all edges equal.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the number of vertices and edges, respectively.
The following m lines provide the description of the edges, as the i-th of them contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the indices of the vertices connected by the i-th edge, and a character ci () providing the initial color of this edge. If ciequals 'R', then this edge is initially colored red. Otherwise, ci is equal to 'B' and this edge is initially colored blue. It's guaranteed that there are no self-loops and multiple edges.
If there is no way to make the colors of all edges equal output - 1 in the only line of the output. Otherwise first output k — the minimum number of moves required to achieve the goal, then output k integers a1, a2, ..., ak, where ai is equal to the index of the vertex that should be used at the i-th move.
If there are multiple optimal sequences of moves, output any of them.
3 31 2 B3 1 R3 2 B
12
6 51 3 R2 3 R3 4 B4 5 R4 6 R
23 4
4 51 2 R1 3 R2 3 B3 4 B1 4 B
-1
题意:
给一个n个点m条边的无向图,每个边的颜色'R'(红色)或者'B'(蓝色),然后让你对点进行操作,一次操作把这个点所连的边的颜色都变色,'R'变'B','B'变'R',问你最少多少次能够把图变成一种颜色。不能就输出-1。
题解:
这是官方题解:
对于一个点,最多对其操作一次,所以我们可以把点分为两类--操作一次的和不操作的
我们可以假设最终全是'R'
那么,对于一条红色的边,它的两个顶点要么都操作一次,要么都不操作,所以这两个点一定属于同一类,(同一个阵营),
对于一条蓝色的边,它的两个顶点其一必然需要操作一次,所以这两个点必然不属于一类。
注意原图可能不联通,所以我们可以对原图的所有连通块,判断其能否分成两部分(有点像二分染色),然后将点数小的部分加入答案。
标程:
#include<bits/stdc++.h>using namespace std;const int MX = 100000;int n, vis[MX];vector<pair<int, char>> G[MX];vector<int> part[3];bool dfs(int v, int p, char c) { if (vis[v] != 0) { return vis[v] == p; } vis[v] = p; part[p].push_back(v); for (auto x : G[v]) { if (dfs(x.first, x.second == c ? p : p ^ 3, c) == false) return false; } return true;}vector<int> solve(char c) { memset(vis, 0, sizeof vis); vector<int> ans; for (int i = 0; i < n; i++) if (vis[i] == 0) { part[1].clear(); part[2].clear(); if (dfs(i, 1, c) == false) { for (int j = 0; j < n + 1; j++) ans.push_back(-1); return ans; } int f = 1; if (part[2].size() < part[1].size()) f = 2; ans.insert(ans.end(), part[f].begin(), part[f].end()); } return ans;}int main() { int m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int u, v; char c; scanf("%d %d %c", &u, &v, &c); u--; v--; G[u].emplace_back(v, c); G[v].emplace_back(u, c); } auto f = solve('R'); auto g = solve('B'); if (g.size() < f.size()) f = g; if (f.size() > n) { printf("-1\\n"); return 0; } printf("%d\\n", (int)f.size()); for (int x : f) printf("%d ", x + 1); printf("\\n"); return 0;}
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=1e5+100;int head[maxn];struct edge{ int to,next; char c;}e[maxn*2]; //int tol=0;int n,m;void add(int u,int v,char c){ e[++tol].to=v,e[tol].c=c,e[tol].next=head[u],head[u]=tol;}int vis[maxn];vector<int> part[3];bool dfs(int u,int p,char c){ if(vis[u]) return vis[u]==p; part[p].push_back(u); vis[u]=p; for(int i=head[u];i;i=e[i].next) { int v=e[i].to; if(!dfs(v,e[i].c==c? p:p^3,c)) return false; } return true;}vector<int> solve(char c){ vector<int> ans; memset(vis,0,sizeof(vis)); rep(i,1,n+1) { if(!vis[i]) { part[1].clear(),part[2].clear(); if(!dfs(i,1,c)) { rep(i,0,n+1) ans.push_back(-1); return ans; } int f=1; if(part[1].size()>part[2].size()) f=2; ans.insert(ans.end(),part[f].begin(),part[f].end()); } } return ans;}int main(){ scanf("%d%d",&n,&m); while(m--) { int u,v; char s[10]; scanf("%d%d%s",&u,&v,s); add(u,v,s[0]),add(v,u,s[0]); } vector<int> f=solve('R'); vector<int> g=solve('B'); if(f.size()<g.size()) { if(f.size()>n) return 0*puts("-1"); printf("%d\n",(int)f.size()); for(int i=0;i<f.size();i++) printf("%d ",f[i]); } else { if(g.size()>n) return 0*puts("-1"); printf("%d\n",(int)g.size()); for(int i=0;i<g.size();i++) printf("%d ",g[i]); } puts(""); return 0;}
- CROC 2016
- CROC 2016
- CROC 2016
- 解题报告:CROC 2016
- Codeforces CROC 2016 - Elimination Round
- CROC 2016 - Elimination Round C 二分
- CROC 2016 - Elimination Round(A)bfs
- CROC 2016 - Qualification-B. Processing Queries(队列)
- CROC 2016 - Elimination Round (Rated Unofficial Edition)题解
- CROC 2016 - Elimination Round D 二分+拓扑序+bfs
- 【CROC 2016 - Elimination RoundA】【水题 循环暴力】Amity Assessment 2x2华容道
- 【CROC 2016 - Elimination RoundC】【二分】Enduring Exodus 最小的人看守与羊距离
- 【CROC 2016 — QualificationA】【水题】nm棋盘填人奇偶不相邻
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))A. Amity Assessment(贪心)
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))B. Mischievous Mess Makers(贪心)
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))C. Enduring Exodus(二分)
- CROC 2016 - Elimination Round B 树状数组处理逆序对个数
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
- iOS开发中遇到过的坑
- SVN 的常用命令行
- 伪分布式Hadoop及开发环境搭建(三)
- 算法训练 最短路 spfa
- 如何找到Mysql数据存储的路径
- CROC 2016
- 解决mysql LOCK TABLES 后事务无法回滚的问题
- 召回率 Recall、精确度Precision、准确率Accuracy、虚警、漏警等分类判定指标
- 解绑
- hadoop作业提交源码分析
- spring结合cxf中web.xml异常
- wamPSever,thinkphp的安装配置及简单网页加法器实现
- usaco2.1 hamming(blog 写到一半发现大事不妙
- 历届试题 回文数字