CROC 2016 - Final Round [Private, For Onsite Finalists Only] B(二分染色? 好题)

B. Graph Coloring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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 ≤ nui ≠ 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.

Output

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.

Examples
input
3 3
1 2 B
3 1 R
3 2 B
output
1
2 
input
6 5
1 3 R
2 3 R
3 4 B
4 5 R
4 6 R
output
2
3 4 
input
4 5
1 2 R
1 3 R
2 3 B
3 4 B
1 4 B
output
-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 second
typedef 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值