6041 I Curse Myself (Point double unicom plus set merger to seek K large) more than 2017 school first game

Source: Internet
Author: User

Test instructions

Give a cactus chart and ask his former K-niche to become a tree.

Ideas:

Give the official first

Since the figure is a cactus, it is clear that each ring on the diagram needs to be removed from the ring and an edge is deleted. So the problem is changed.
To have M-sets, each set has a number of numbers to choose from each collection exactly one number plus
To. Ask for all of this and the top K is what. This is a classic question.

Point Double unicom do not say a glance can see that the practice is to shrink point after each ring to take one, and then find the largest K so the difficulty of this problem is here, the procedure of course is not know, read the key and blog to understand. I've done two collections before, and this is a K-merge, and it's like merging two sets, but it feels so bad. It's the first two merges, then the third one, and the fourth one, and then it's ready. Explanation of the complexity of the topic O (KM) ... Then O (KM), anyway, this is really not going to be a good process. At the time of the merger

void merge (Vector<int> &v, vector<int> B) {   priority_queue< opt > Q;   for (int i = 0; i < b.size (); + + i) {       q.push ((opt) {v[0] + b[i], I, 0});   }   W.resize (0);   while (W.size () < K &&! Q.empty ()) {       Auto it = Q.top (); Q.pop ();       W.push_back (IT.W);       if (It.y + 1 < v.size ()) {           + + It.y;           Q.push (opt) {B[it.x] + V[it.y], it.x, it.y});}   }   V = W;}

At the beginning, I haven't read the whole day. Later figured out the feeling is quite simple, and then run in this machine, did not open O2 ran a lifetime ...

The process is actually for each of the merged array, the first to let his largest and B arrays merged, and then for the merged array, if the current

This bit is push into the answer array, then merge the current number of the second-largest and B-arrays of the array first.

The idea is very powerful ~

Code on the labeling process bar ~

#include <bits/stdc++.h>using namespace Std;const int N = 1005;int N, m, k;struct opt {int W, x, y;    BOOL operator < (const opt& R) Const {return W < R.W;    }};vector<int> w;void Merge (vector<int> &v, vector<int> B) {priority_queue< opt > Q;    for (int i = 0; i < b.size (); + + i) {Q.push (opt) {v[0] + b[i], I, 0});    } w.resize (0); while (W.size () < K &&! Q.empty ()) {Auto it = q.top ();        Q.pop ();        W.push_back (IT.W);            if (It.y + 1 < v.size ()) {+ + It.y;        Q.push (opt) {B[it.x] + V[it.y], it.x, it.y}); }} V = W;} int Pre[n], mcnt;struct edge {int x, W, Next,} e[n << 2];vector<int> res;int dfn[n], low[n], Ncnt;sta    ck<int> s;void dfs (int x, int fa) {dfn[x] = low[x] = + + ncnt;        for (int i = pre[x]; ~i; i = e[i].next) {int y = e[i].x;            if (!dfn[y]) {s.push (i); DFS (y, I ^ 1);            Low[x] = min (low[x], low[y]);                if (Low[y] > Dfn[x]) {}//(x, y) is bridge if (Low[y] >= dfn[x]) {int J;                Vector<int> V;                    do {j = s.top ();                    S.pop ();                V.push_back (E[J].W);                } while (J! = i);                    if (V.size () > 1) {//cout << v.size () << Endl; for (auto &x:v) cout << x << ";                    cout << Endl;                Merge (res, V); }}} else if (i! = FA && Dfn[y] < dfn[x]) S.push (i), low[x] = min (low[x], dfn[y    ]);    }}void work () {memset (Pre,-1, sizeof (pre));    mcnt = ncnt = 0;    int sum = 0;        for (int i = 0; i < m; + + i) {int x, y, Z;        scanf ("%d%d%d", &x, &y, &z);        E[MCNT] = (edge) {y, Z, Pre[x]}, pre[x] = mcnt + +; E[mcnt] = (edge) {x, Z, Pre[y]}, pre[y] = mcnt + +;    sum + = Z;    } scanf ("%d", &k);    Res.resize (0);    Res.push_back (0);    memset (DFN, 0, sizeof (DFN));    DFS (1, 0);    int w = 0;    for (int i = 0; i < res.size (); + + i) {w + = (i + 1) * (Sum-res[i]);    } static int CA = 0; printf ("Case #%d:%u\n", + + CA, W);}    int main () {res.reserve (100001);    W.reserve (100001);    while (~SCANF ("%d%d", &n, &m)) {work (); } return 0;}

6041 I Curse Myself (Point double unicom plus set merger to seek K large) more than 2017 school first game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.