21211_Coconuts

2022-5-16 18:18| 发布者: Hocassian| 查看: 43| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-3-8950521627700-Problem List-采集的数据-后羿采集器.html

Pro.ID

21211

Title

Coconuts

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21211

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 1500/500 MS (Java/Others)     Memory Limit: 131072/65536 K (Java/Others)
  • 描述

    A group of n castle guards are voting to determine whether African swallows can carry coconuts. While each guard has his own personal opinion on the matter, a guard will often vote contrary to his beliefs in order to avoid disagreeing with the votes of his friends.
    You are given a list of guards who either do or do not believe in the coconut-carrying capacity of African swallows, and a list of all pairs of guards who are friends. Your task is to determine how each guard must vote in order to minimize the sum of the total number of disagreements between friends and the total number of guards who must vote against their own beliefs.

    输入

    The input to this problem will contain multiple test cases. Each test case begins with a single line containing an integer n (where 2 <= n <= 300), the number of guards, and an integer m (where 1 <= m <= n(n-1)/2), the number of pairs of guards who are friends. The second line of the test case contains n integers, where the ith integer is 1 if the ith guard believes in the ability of African swallows to carry coconuts, and 0 otherwise. Finally, the next m lines of the test case each contain two distinct integers i and j (where 1 <= i, j <= n), indicating that guards i and j are friends. Guards within each pair of friends may be listed in any order, but no pair of guards will be repeated. The input is terminated by an invalid test case with n = m = 0, which should not be processed.

    输出

    Description
    A group of n castle guards are voting to determine whether African swallows can carry coconuts. While each guard has his own personal opinion on the matter, a guard will often vote contrary to his beliefs in order to avoid disagreeing with the votes of his friends.
    You are given a list of guards who either do or do not believe in the coconut-carrying capacity of African swallows, and a list of all pairs of guards who are friends. Your task is to determine how each guard must vote in order to minimize the sum of the total number of disagreements between friends and the total number of guards who must vote against their own beliefs.
    Input
    The input to this problem will contain multiple test cases. Each test case begins with a single line containing an integer n (where 2 <= n <= 300), the number of guards, and an integer m (where 1 <= m <= n(n-1)/2), the number of pairs of guards who are friends. The second line of the test case contains n integers, where the ith integer is 1 if the ith guard believes in the ability of African swallows to carry coconuts, and 0 otherwise. Finally, the next m lines of the test case each contain two distinct integers i and j (where 1 <= i, j <= n), indicating that guards i and j are friends. Guards within each pair of friends may be listed in any order, but no pair of guards will be repeated. The input is terminated by an invalid test case with n = m = 0, which should not be processed.
    Output
    For each input test case, print a single line containing the minimum possible sum of the total number of disagreements between all friends plus the total number of guards who must vote against their own beliefs.
    Sample Input
    3 3
    1 0 0
    1 2
    1 3
    3 2
    6 6
    1 1 1 0 0 0
    1 2
    2 3
    4 2
    3 5
    4 5
    5 6
    0 0
    Sample Output
    1
    2
    Hint
    Notes:
    In the first test case, the best result is achieved when all guards vote that African swallows cannot carry coconuts. Here, there is only a penalty of 1 for the first guard voting against his beliefs.
    In the second test case, the best result is achieved when each guard votes for his beliefs. The penalty of 2 arises from the disagreements between guards 2 and 4, and guards 3 and 5.
    Source

    样例输入

    3 3
    1 0 0
    1 2
    1 3
    3 2
    6 6
    1 1 1 0 0 0
    1 2
    2 3
    4 2
    3 5
    4 5
    5 6
    0 0

    样例输出

    1
    2

    提示

    Notes:
    In the first test case, the best result is achieved when all guards vote that African swallows cannot carry coconuts. Here, there is only a penalty of 1 for the first guard voting against his beliefs.
    In the second test case, the best result is achieved when each guard votes for his beliefs. The penalty of 2 arises from the disagreements between guards 2 and 4, and guards 3 and 5.


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部