Codeforces Global Round 6

Good play ah, finished, only ABC.

A first wanted to complex should begin to think where the prime factor decomposition.

B complex also want to read the map, look obviously.

C played too wrong question, be careful single separate.

D should look at the list of matches do not arbitrary, should look at the list, a circle of people in front spike D, D should not be complicated FIG, and why the ring and elimination.

Consumers ring DAG indeed, this half wave loss.

priority_queue<pii> G[200005];

void AddEdge(int u, int v, int w) {
    if(u == v || w == 0)
        return;
    if(!G[v].empty()) {
        pii tmp = G[v].top();
        if(tmp.second == u) {
            G[v].pop();
            if(w >= tmp.first) {
                w -= tmp.first;
                AddEdge(u, v, w);
                return;
            } else {
                tmp.first -= w;
                G[v].push(tmp);
                return;
            }
        }
        if(w >= tmp.first) {
            G[v].pop();
            AddEdge(u, tmp.second, tmp.first);
            AddEdge(u, v, w - tmp.first);
            return;
        } else {
            G[v].pop();
            tmp.first -= w;
            G[v].push(tmp);
            AddEdge(u, tmp.second, w);
            return;
        }
    }
    if(w)
        G[u].push({w, v});
}

vector<pii> G2[200005];
vector<int> AG2[200005];
queue<int> Q;

priority_queue<pii> G4[200005];

void AddEdge2(int u, int v, int w) {
    if(u == v || w == 0)
        return;
    if(!G4[v].empty()) {
        pii tmp = G4[v].top();
        if(tmp.second == u) {
            G4[v].pop();
            if(w >= tmp.first) {
                w -= tmp.first;
                AddEdge2(u, v, w);
                return;
            } else {
                tmp.first -= w;
                G4[v].push(tmp);
                return;
            }
        }
        if(w >= tmp.first) {
            G4[v].pop();
            AddEdge2(u, tmp.second, tmp.first);
            AddEdge2(u, v, w - tmp.first);
            return;
        } else {
            G4[v].pop();
            tmp.first -= w;
            G4[v].push(tmp);
            AddEdge2(u, tmp.second, w);
            return;
        }
    }
    if(w)
        G4[u].push({w, v});
}

int outdeg[200005];

map<int, ll> G5[200005];

void test_case() {
    int n, m;
    scanf("%d%d", &n, &m);
    while(m--) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        AddEdge(u, v, w);
    }
    for(int i = 1; i <= n; ++i) {
        while(!G[i].empty()) {
            pii tmp = G[i].top();
            G[i].pop();
            int v = tmp.second, w = tmp.first;
            G2[i].push_back({v, w});
            AG2[v].push_back(i);
        }
    }
    for(int i = 1; i <= n; ++i) {
        outdeg[i] = G2[i].size();
        if(outdeg[i] == 0)
            Q.push(i);
    }
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for(auto &e : G2[u])
            AddEdge2(u, e.first, e.second);
        for(auto &v : AG2[u]) {
            --outdeg[v];
            if(outdeg[v] == 0)
                Q.push(v);
        }
    }
    for(int i = 1; i <= n; ++i) {
        while(!G4[i].empty()) {
            pii tmp = G4[i].top();
            G4[i].pop();
            int u = i, w = tmp.first, v = tmp.second;
            G5[u][v] += w;
        }
    }
    int sum=0;
    for(int i = 1; i <= n; ++i)
        sum += G5[i].size();
    printf("%d\n", sum);
    for(int i = 1; i <= n; ++i) {
        for(auto &e : G5[i])
            printf("%d %d %lld\n", i, e.first, e.second);
    }
}

Guess you like

Origin www.cnblogs.com/KisekiPurin2019/p/12057840.html