RQNOJ 600: Criminals

RQNOJ 600: Criminals

Original Problem Statement

题目描述
S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N。他们之间的关系自然也极不和谐。很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突。我们用“怨气值”(一个正整数值)来表示某两名罪犯之间的仇恨程度,怨气值越大,则这两名罪犯之间的积怨越多。如果两名怨气值为c 的罪犯被关押在同一监狱,他们俩之间会发生摩擦,并造成影响力为c 的冲突事件。
每年年末,警察局会将本年内监狱中的所有冲突事件按影响力从大到小排成一个列表,然后上报到S 城Z 市长那里。公务繁忙的Z 市长只会去看列表中的第一个事件的影响力,如果影响很坏,他就会考虑撤换警察局长。
在详细考察了N 名罪犯间的矛盾关系后,警察局长觉得压力巨大。他准备将罪犯们在两座监狱内重新分配,以求产生的冲突事件影响力都较小,从而保住自己的乌纱帽。假设只要处于同一监狱内的某两个罪犯间有仇恨,那么他们一定会在每年的某个时候发生摩擦。那么,应如何分配罪犯,才能使Z 市长看到的那个冲突事件的影响力最小?这个最小值是多少?
【输入输出样例说明】
罪犯之间的怨气值如下面左图所示,右图所示为罪犯的分配方法,市长看到的冲突事件影响力是3512(由2 号和3 号罪犯引发)。其他任何分法都不会比这个分法更优。
【数据范围】
对于30%的数据有N≤ 15。
对于70%的数据有N≤ 2000,M≤ 50000。
对于100%的数据有N≤ 20000,M≤ 100000。
输入格式
输入文件的每行中两个数之间用一个空格隔开。
第一行为两个正整数N 和M,分别表示罪犯的数目以及存在仇恨的罪犯对数。
接下来的M 行每行为三个正整数aj,bj,cj,表示aj 号和bj 号罪犯之间存在仇恨,其怨
气值为cj。数据保证1<aj=<=bj<=N ,0 < cj≤ 1,000,000,000,且每对罪犯组合只出现一
次。
输出格式
共1 行,为Z 市长看到的那个冲突事件的影响力。如果本年内监狱中未发生任何冲突事件,请输出0。

My Interpretation

N people are to be separated into two groups. However, some pairs people hate other people. When they are in the same group, a degree K incident happens. There are M pairs of these people out of N people. Given these pairs of people, what is the largest incident that can happen when all conflicts are sought to be minimized? This assumes we use optimal separation. 

How We Should Think Of This Problem

    Since we want the largest incident possible, why not sort the possible incidents by degree? After sorting them that way, we remove the one with largest degree and separate the two. Wait - since we're removing them in sorted order, why not simply put all of them in a priority queue? We can. 
    Furthermore, since this problem asks to keep track of which people are in which group, we can organize it with a disjoint set.
So, for each element in the priority queue, we do the following:
  • take the element and pop it;
  • check if the pair are in the same set of a disjoint set;
  • if so, check if the sum of their distance to the root mod 2 is 0 - this prevents over-counting. If so, then we can't separate them, so return the degree of this element (it's the largest);
  • else, merge the two elements together.

Code

#include <iostream>
#include <queue>
using namespace std;

struct hate { int x, y, s; bool operator<(const hate& rhs) const { return s < rhs.s; } };

int rt[20005], rela[20005];
int root(int n)
{
    if(rt[n] == n) return n;
    int p = rt[n]; rt[n] = root(p); rela[n] = (rela[n] + rela[p]) % 2;
    return rt[n];
}
void merge(int m, int n)
{
    int rm = root(m), rn = root(n);
    rt[rn] = rm;
    rela[rn] = (rela[m] + rela[n] + 1) % 2;
}
int main()
{
    ios_base::sync_with_stdio(false); int N, M; cin >> N >> M;
    priority_queue<hate> que;
    for(int i=0; i<N; i++) rt[i] = i;
    for(int i=0; i<M; i++) { int x, y, s; cin >> x >> y >> s; que.push({x, y, s}); }
    while(!que.empty())
    {
        hate gr = que.top(); que.pop();
        if(root(gr.x) == root(gr.y)) { if((rela[gr.x] + rela[gr.y])%2 == 0) { cout << gr.s << endl; return 0; } }
        else merge(gr.y, gr.x);
    }
    cout << 0 << endl;
}

Comments

Popular posts from this blog

USACO Training "subset": Subset Sums

USACO 2018 Open: Talent Show

UVA 787 and Big Number Multiplication