Decorating The Pastures

链接:https://ac.nowcoder.com/acm/contest/3888/G
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Farmer John has N (1 <= N <= 50,000) pastures, conveniently numbered 1...N,
connected by M (1 <= M <= 100,000) bidirectional paths. Path i connects
pasture A_i (1 <= A_i <= N) to pasture B_i (1 <= B_i <= N) with A_i != B_i.
It is possible for two paths to connect between the same pair of pastures.

Bessie has decided to decorate the pastures for FJ's birthday. She wants to
put a large sign in each pasture containing either the letter 'F' or 'J',
but in order not to confuse FJ, she wants to be sure that two pastures are
decorated by different letters if they are connected by a path.

The sign company insists on charging Bessie more money for an 'F' sign than
a 'J' sign, so Bessie wants to maximize the number of 'J' signs that she
uses. Please determine this number, or output -1 if there is no valid way
to arrange the signs.

输入描述:

* Line 1: Two integers N and M.

* Lines 2..M+1: Two integers, A_i and B_i indicating that there is a
bidirectional path from A_i to B_i.

输出描述:

* Line 1: A single integer indicating the maximum number of 'J' signs
that Bessie can use.  If there is no valid solution for
arranging the signs, output -1.

示例1

输入

4 4
1 2
2 3
3 4
4 1

输出

2

分析:给定一个无向图,用 'F' 和 'J' 进行 01 染色,求max(0节点数量, 1节点数量)

遍历图中每一个点,对未进行染色的点定为起点设定一种颜色,然后从该点开始进行 dfs 染色,每个子图的 dfs 结束后对子图的最大染色数(F、J中的最大一种)进行累加。

注意奇数环,如 1 -- 2 --- 3 -- 1 ,这种就是 -1 的情况,因此需要在 dfs 是判断下一个点的颜色是否跟当前点相同,相同则为奇数环。

代码:

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.regex.Pattern;

public class Main {
    public static Scanner in = new Scanner(System.in);
    public static PrintWriter out = new PrintWriter(System.out);
    public static int t, n, m, ans, k, u, v, sumF, sumJ;
    public static int[] color = new int[50010];
    public static List<Integer>[] to = new ArrayList[50010];

    public static void main(String args[]) {
        n = in.nextInt();
        m = in.nextInt();
        for (int i = 1; i <= n; i++) {
            to[i] = new ArrayList();
            color[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            u = in.nextInt();
            v = in.nextInt();
            to[u].add(v);
            to[v].add(u);
        }
        ans = 0;
        for (int i = 1; i <= n; i++) {
            if (color[i] != 0) {
                continue;
            }
            color[i] = 1;
            sumF = 1;
            sumJ = 0;
            dfs(i);
            if (sumF == -1 || sumJ == -1) {
                ans = -1;
                break;
            }
            ans += Math.max(sumF, sumJ);
        }
        out.println(ans);
        out.flush();
        out.close();
    }

    static void dfs(int p) {
        int len = to[p].size();
        for (int i = 0; i < len; i++) {
            if (color[to[p].get(i)] == color[p]) {
                sumF = -1;
                return;
            }
            if (color[to[p].get(i)] == 0) {
                if (sumF == -1) {
                    return;
                }
                if (color[p] == 1) {
                    color[to[p].get(i)] = 2;
                    sumJ++;
                } else {
                    color[to[p].get(i)] = 1;
                    sumF++;
                }
                dfs(to[p].get(i));
            }
        }
    }
}

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值